The top-level understanding of the collection (analysis of the data structure in JAVA)

set

Why not take a look at these two appetizers first, here are two fresh ingredients that I have compiled:
Collection details: Collection details
Map details: Map details
Insert picture description here

Collections and arrays

The difference between collection and array

  • Arrays can store basic data types and reference data types. Basic data types store values, and reference data types store address values.
  • The collection can only store reference data types (objects). Collections can also store basic data types (a bit contradictory, see the latter sentence), but they will be automatically boxed into objects when they are stored.
  • The length of the array is fixed and cannot grow automatically.
  • The length of the collection is variable and can grow according to the growth of the elements.
  • The same type of elements are stored in the array, and basic data type values ​​can be stored. All collections store objects. And the types of objects can be inconsistent. In development, when there are many objects, collections are used for storage.

Collection and array selection

  • If the number of elements is fixed, an array is recommended for high efficiency.
  • If the number of elements is not fixed, it is recommended to use a set.

Classification of collections

Three high-frequency use interfaces

Collection interface Map interface Iterator interface

All kinds

Collection接口:  									   单列集合的顶层接口	
	List接口:    									   有序的子接口  是元素有序、元素可重复。
			 Vetor集合(数组结构:单线程,都慢)
			 ArrayList集合(数组结构:查询快,增删慢),
			 LinkLIst集合(链表结构:查询慢,增删快)
			 Set集合: 	   无序的子接口  特点是元素无序,而且不可重复
				TreeSet集合,
				HashSet集合(哈希表结构:查询快)
				LinkedHashSet
	Map     											双列集合的顶层接口
		HashMap 										哈希表存储
		LikedHashMap

Insert picture description here

collecions class

Common methods:

public static <T> boolean addAll(Collection<T> c, T... elements) :往集合中添加一
些元素。
public static void shuffle(List<?> list) 打乱顺序 :打乱集合顺序。
public static <T> void sort(List<T> list) :将集合中元素按照默认规则排序。
public static <T> void sort(List<T> list,Comparator<? super T> ) :将集合中元素按
照指定规则排序。

achieve:

public static void main(String[] args) {
    
    
        ArrayList<Integer> arrayList =new ArrayList<>();
        /**
         * 原来写法
         */
        arrayList.add(12);
        arrayList.add(13);
        arrayList.add(14);
        Collections.addAll(arrayList,1,2,3,4);
        System.out.println(arrayList);
        //排序
        Collections.sort(arrayList);
        System.out.println(arrayList);
    }

Insert picture description here
Nonsense time:

If the customer thinks that the food is appropriate, can you give a free like! Thank you! Slow walker officer! It is recommended to pack and collect and come again next time. Shop Xiaoer QQ: 309021573, welcome to harass!

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113174739