Learning Java programming ideas (2)-Java container

Use of Java common containers

The commonly used containers are
probably

-Subclasses of List

ArraryList和LinkedList

-Subclasses of Map

HashMap, TreeMap, LinkedHashMap plus a ConcurrentHashMap (concurrent security)

-Subclasses of Set

HashSet,TreeSet

-Queue and subclasses (less used in other places except concurrency)

Having said so many why these containers are widely used, the first point must bedata structures reason.

From the data structure point of view,
ArrayList is a linear-like table (generalized), and LinkedList is a linked list.
Map is a key-value pair.
Set is an unordered collection without repeating elements.
Queue

So why is there no Stack?
The answer is that there is a stack. In the early version, there is a Vector-based Stack. Knowing this, you know that the
creator directly inherits the Vector-based Stack for simplicity. For the stack, frequent insertion and deletion operations are undoubtedly for arrays It is a disaster, and it contains an intervening method, which is inconsistent with the data structure itself.
and soNot recommended for use Stack and Vector
For the stack, LinedList can fully meet all the needs of the stack. LinkedList can also completely replace Queue.

Stealing a picture is still very clear
Java container diagram

Container printing

Container printing calls toString () method by default. It can be rewritten if needed, but it is not recommended to rewrite directly, use adapter mode to rewrite.
Very simple

public class Test {
    public static void main(String[] args){
        Random random = new Random(11);
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new TreeMap<>();
        Set<Integer> set = new LinkedHashSet<>();
        for(int i=0;i<5;i++){
            list.add(random.nextInt(10));
            map.put(i,random.nextInt(10));
            set.add(random.nextInt(10));
        }
        System.out.println(list.toString());
        System.out.println(map);
        System.out.println(set);
    }
}

result:

[8, 5, 0, 7, 7]
{0=8, 1=3, 2=3, 3=4, 4=9}
[1, 7, 3, 4, 2]

Add and delete containers

For classes under the Collection interface

boolean add(E e);

boolean remove(Object o);

boolean addAll(Collection<? extends E> c);

boolean removeAll(Collection<?> c);

Map

void putAll(Map<? extends K, ? extends V> m);

void clear();

V put(K key, V value);

default boolean remove(Object key, Object value) ;

Container iteration or traversal

1. Iterator traversal

For Collection containers only need to call iterator () method. Just get the iterator object.
For Map objects, only the entry object has an iterator method

 public static void main(String[] args){
        Random random = new Random(11);
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new TreeMap<>();
        Set<Integer> set = new LinkedHashSet<>();
        for(int i = 0;i < 5;i++){
            list.add(random.nextInt(10));
            map.put(i,random.nextInt(10));
            set.add(random.nextInt(10));
        }
        // 获取迭代器对象
        Iterator<Integer>  iterator1 = list.iterator();
        Iterator<Integer> iterator2 = set.iterator();
        Iterator<Map.Entry<Integer,Integer>> iterator3= map.entrySet().iterator();
        
        while (iterator1.hasNext()){
            System.out.print(iterator1.next()+" ");
        }
        System.out.println();
        while (iterator2.hasNext()){
            System.out.print(iterator2.next()+" ");
        }
        System.out.println();
        while (iterator3.hasNext()){
            Map.Entry entry = iterator3.next();
            System.out.print(entry.getKey()+":"+entry.getValue()+" ");
        }
        System.out.println();
    }
8 5 0 7 7 
1 7 3 4 2 
0:8 1:3 2:3 3:4 4:9

2. The Iterable interface can be traversed with for each

public static void main(String[] args){
        Random random = new Random(11);
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new TreeMap<>();
        Set<Integer> set = new LinkedHashSet<>();
        for(int i=0;i<5;i++){
            list.add(random.nextInt(10));
            map.put(i,random.nextInt(10));
            set.add(random.nextInt(10));
        }
        for(Integer num: list){
            System.out.print(num+" ");
        }
        System.out.println();
        for(Integer num: set){
            System.out.print(num+" ");
        }
        System.out.println();
        for(Map.Entry entry :map.entrySet()){
            System.out.print(entry.getKey()+":"+entry.getValue()+" ");
        }
        System.out.println();
    }
8 5 0 7 7 
1 7 3 4 2 
0:8 1:3 2:3 3:4 4:9 

Comparison of containers

Published 22 original articles · Likes2 · Visits 881

Guess you like

Origin blog.csdn.net/weixin_41685373/article/details/96740134