Java single-column collection super detailed explanation

Single-column sets are divided into repeatable data in the set (List) and non-repeatable data in the set (Set).

List collection is commonly divided into: ArrayList and LinkedList.

Set collections are commonly divided into: HashSet and TreeSet.

Collection

Features:

Collection collection is the top-level interface for single-column collections. It is an interface!

JDK does not provide any direct instance of this interface, it provides specific sub-interface implementations, such as (Set and List) implementations.

So if we use it, we must use polymorphism! The specific implementation class ArrayList.

Collection collection commonly used methods:

public static void main(String[] args){
    //创建集合
    Collection<String> c = new ArratList<String>();
    //添加方法, boolean add();
    c.add("hello");

    //移除方法,boolean remove();
    c.remove("hello");

    //清空集合所有元素 void Clear(); 
    c.clear();

    //判断集合是否存在指定元素 boolean contains();
    c.contains("hello");

    //判断集合是否为空 boolean isEmpty();
    c.isEmpty();

    //判断集合的长度,也就是集合中元素的个数 int size();
    c.size();
}

Collection collection traversal method:

Iterator method
 

Collection<String> c = new ArratList<String>();

//返回集合中元素的迭代器,通过集合的iterator()方法得到 
Iterator<String> it=c.iterator();

//boolean hasNext();如果有迭代有更多元素,返回true
while (it.hasNext()){
//只要为true,就不停的打印集合中的元素。
    System.out.printIn(it.next());
}

List collection

We have two commonly used subclasses of list collection: ArrayList (fast query, slow addition and deletion) and LinkedList (slow query, fast addition and deletion).

Features:

Ordered: The order of stored and removed elements is the same. Repeatable: The stored element can be repeated.

List collection specific methods:

The List collection inherits the Collection collection, so the methods of the Collection List have.

    List<String> list = new ArrayList<String>();
    list.add("hello");
    list.add("word");
    
//在指定的位置插入指定的元素 void add(int index,E element);
    list.add(1,"java");

//删除指定索引处的元素,返回被删除的元素 E remove(int index);
    list.remove(1); //返回word

//修改指定索引出的元素,返回修改的元素 E set(int index,E element);
    list.set(0,"javaa");  //返回hellow

//返回指定索引位置的元素
    list.get(1);

List collection traversal:

for loop traversal

//遍历并打印到控制台
for(int i=0; i<list.size(); i++){
    String s=list.get(i);
    System.out.printIn(s);
}

Of course, the List collection can also be traversed with an iterator. Same as above, no more details here.

Another method is to enhance the for loop traversal.

for(String s : list){
    System.out.printIn(s);
}

ArrayList I won't say much here, basically all collections have it.

Let's talk about LinkedList, its bottom layer is stored by a linked list.

The unique functions of the LinkedList collection:

I will not put the code, it is troublesome. Tired panic, haha. If you want to use it, you can directly create a Lined List collection and call it.

Set collection

The set collection also inherits the Collection collection, and some of its set collections have.

Set collection features:

A collection that does not contain repeated elements has no indexing method, so it cannot be traversed by a normal for loop. It can be traversed by enhancing the for loop.

Because Set is also an interface, it must be polymorphic, using its implementation class, HashSet.

Set<String> set = new HashSet<String>();
set.add("hello");
set.add("word");

for(String s : set){
    System.out.printIn(s);
}

HashSet collection

HashSet collection features

The underlying data structure is a hash table, and there is no guarantee for the iteration order of the collection, that is, the storage order and the retrieval order may not be the same.

There is no method with index, and it cannot be traversed by a normal for loop. Do not include repeated elements.

Question: When we use HashSet collections, when storing data, different objects, but the values ​​of all properties are the same, how to maintain uniqueness.

To put it bluntly, for example: Both students are named Xiao Wang, 18 years old. Then use HashSet collection, it can't be saved, but it is saved.

Find the student class and press alt+insert to automatically add equals() and hashCode(). Directly Next automatically generates the thing.

TreeSet collection

TreeSet collection features

The elements are in order, which is not the order of storage and retrieval, but refers to sorting according to certain rules, and the specific method depends on the construction method.

There is no method with index, and it cannot be traversed by a normal for loop. Do not include repeated elements.

Case:

TreeSet<Integer> ts = new TreeSet<Integer>();
    ts.add(10);
    ts.add(30);
    ts.add(20);
    ts.add(50);
    ts.add(40);
    for(Integer i : ts){
        System.out.printIn(i);
    }

//输出结果
10
20
30
40
50

If we want to make a special sorting method, we have to implement the Comparable interface and override the compareTo method

Guess you like

Origin blog.csdn.net/weixin_44126152/article/details/106365302