Preliminary summary of java collection (summary of basic java of dark horse programmer)

aggregate

In java foundation, collections can be roughly divided into the following categories

Student object case code (to be used later)

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Collection single column

Collection collection

Preface: the reason why a Collection is called a single column set is that compared with a Map set, each element exists separately and there is no pairing relationship

Collection overview

Add element

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

        //Boolean add (E): add element
        System.out.println(c.add("hello"));
        System.out.println(c.add("world"));

Removing Elements

 Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");

        //boolean remove(Object o): removes the specified element
        System.out.println(c.remove("world"));

Determine whether the specified element exists in the collection

Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");
     
        //boolean contains(Object o): judge whether the specified element exists in the collection
        System.out.println(c.contains("world"));

Empty collection elements

Collection<String> c=new ArrayList<String>();        
        c.add("hello");

        //void clear(): empty the elements in the collection
        c.clear();

Determine whether there are elements

 Collection<String> c=new ArrayList<String>();
        c.add("hello");

        //boolean isEmpty(): judge whether the collection is empty
        System.out.println(c.isEmpty());

Set length

//int size(): set length, number of elements in the set
        System.out.println(c.size());

Traversal set

public class Collection Traversal element {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();
        c.add("hello");
        c.add("world");
        c.add("java");

        //iterator 
        Iterator<String> it = c.iterator();

        //it.hasNext indicates whether there are elements in the set
        //it.next() indicates the output element
        while(it.hasNext()){
            String s = it.next();
            System.out.println(s);
        }}}

Set method

Sequential arrangement

Collection.sort(list);

Reverse order

Collection.reverse(list);

Random arrangement

Collections.shuffle(list);

List collection

list set features

   1.Order: the storage elements are consistent with the extraction order

   2.Repeatable: elements can be repeated

Collection overview

Add element

List<String> list=new ArrayList<String>();
         //Add element
         list.add("hello");
         list.add("world");
         list.add("java");

Insert element at specified location

List<String> list=new ArrayList<String>();
         list.add("hello");
         list.add("world");
         list.add("java");

         //void add(int index,E element): insert the element at the specified position of the collection here
         list.add(1,"javaee");

Delete the specified element (return the deleted element)

List<String> list=new ArrayList<String>();
         list.add("hello");
         list.add("world");
         list.add("java");

         //E remove(int index): deletes the specified element and returns the deleted element
         System.out.println(list.remove(1));

Modify the element at the specified index (return the modified element)

List<String> list=new ArrayList<String>();
         list.add("hello");
         list.add("world");
         list.add("java");

         //E set(int index,E element): modify the element at the specified index and return the modified element
         System.out.println(list.set(1,"javaee"));

Output list
System.out.println(list);

ergodic

Three traversal methods (combined with student object cases)
The enhanced for loop is the most commonly used

List<Student> list=new ArrayList<Student>();

Student s1=new Student("Betty","30");
Student s2=new Student("Btty","30");
Student s3=new Student("Bety","30");

list.add(s1);
list.add(s2);
list.add(s3);

//Iterators: Collection specific traversal
Iterator<Student> it=list.iterator();
while(it.hasNext()){
    Student s=it.next();
    System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");

//Ordinary for: traversal mode with index, index i
for(int i=0;i< list.size();i++){
    Student s = list.get(i);
    System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");

//Enhanced for: the most convenient traversal method
for(Student s:list){
    System.out.println(s.getName()+","+s.getAge());
}

ArrayList collection

ArrayList is a collection with array as the bottom layer. It is slow to add or delete and fast to find

Linked is a set based on linked list. It is fast to add and delete and slow to find

Collection overview

Deletes the specified element (returns a Boolean value)

 ArrayList<String> array=new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");
//        public boolean remove(Object o) deletes the specified element and returns unsuccessful
        System.out.println(array.remove("world"));

Deletes the element at the specified index

 ArrayList<String> array=new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");

//        public E remove(int index) deletes the element at the specified index
        System.out.println(array.remove(0));

Modify the element at the specified index

ArrayList<String> array=new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");

//        public E set(int index,E element) modifies the element at the specified index
        System.out.println(array.set(1,"javaee"));

Outputs the element at the specified index

ArrayList<String> array=new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");

//        public E get(int index) outputs the element at the specified index
        System.out.println(array.get(1));

Returns the number of elements in the collection

ArrayList<String> array=new ArrayList<>();
        array.add("hello");
        array.add("world");
        array.add("java");

//        public int size() returns the number of elements in the collection
        System.out.println(array.size());

Set traversal

Three traversal methods (combined with student object cases)
The enhanced for loop is the most commonly used

ArrayList<Student> array=new ArrayList<Student>();

Student s1=new Student("Betty","30");
Student s2=new Student("Btty","30");
Student s3=new Student("Bety","30");

array.add(s1);
array.add(s2);
array.add(s3);

//Iterators: Collection specific traversal
Iterator<Student> it=array.iterator();
while(it.hasNext()){
    Student s = it.next();
    System.out.println(s);
}
System.out.println("--------");

//Normal for: traversal mode with index
for (int i=0;i< array.size();i++){
    Student s = array.get(i);
    System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");

//Enhanced for: the most convenient way
for (Student s:array){
    System.out.println(s.getName()+","+s.getAge());
}

LinkedList collection

Collection overview

Add elements at the beginning and end of the list

 LinkedList<String> linkedList=new LinkedList<String>();
        linkedList.add("hello");
        linkedList.add("world");
        linkedList.add("java");

        //Public void addfirst (E): inserts the specified element at the beginning of the list
        //Public void addlast (E): appends the specified element to the end of the list here
        linkedList.addFirst("javaee");
        linkedList.addLast("hi");

Get elements at the beginning and end of the list

        LinkedList<String> linkedList=new LinkedList<String>();
        linkedList.add("hello");
        linkedList.add("world");
        linkedList.add("java");
    
        //public E getFirst(): returns the first element of the list
        //public E getLast(): returns the last element of the list
        System.out.println(linkedList.getFirst());
        System.out.println(linkedList.getLast());

Remove elements at the beginning and end of the list

        LinkedList<String> linkedList=new LinkedList<String>();
        linkedList.add("hello");
        linkedList.add("world");
        linkedList.add("java");

        //public E removeFirst(): deletes from the list and returns the first element
        //public E removeLast(): deletes from the list and returns the last element
        System.out.println(linkedList.removeFirst());
        System.out.println(linkedList.removeLast());

Set set

Collection overview

1. Disorder

2. Cannot use for loop traversal

3. No duplicate elements

Set<String> set=new HashSet<String>();

set.add("hello");
set.add("world");
set.add("java");

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

HashSet set

Collection overview and traversal

1. The underlying structure is hash table

2. No guarantee of sequence

3. If there is no index method, ordinary for cannot be used

4. Because it is a Set, there are no duplicate elements

//Create collection object
HashSet<String> hs=new HashSet<String>();
hs.add("hello");
hs.add("world");
hs.add("java");

//Enhanced for traversal
for(String s:hs){
    System.out.println(s);
}

TreeSet set

Collection overview and traversal

1. The elements are in order, according to the method

2. If there is no index method, ordinary for cannot be used

TreeSet (): sort according to the natural order of elements

TreeSet (comparator): sort according to the specified comparator

3. Because it is a Set, there are no duplicate elements

TreeSet<Integer> ts=new TreeSet<Integer>();

//Add element
ts.add(10);
ts.add(40);
ts.add(30);
ts.add(50);
ts.add(20);

for(Integer i:ts){
    System.out.println(i);
}

Map double column

The Map set is called a double column set. Suppose that there are three elements 1, 2 and 3 in the set. In a single element, such as element 1, there are combinations of keys and values. That is, element 1 – a (key) and b (value). The console can access the key to get a specific value

Map collection

Collection overview

Delete the specified element according to the key (return value)

Map<String,String> map=new HashMap<String, String>();

        map.put("zhang wuji","Zhao Min");
        map.put("Guo Jing","Rong Yang");
        map.put("Guo Yang","little dragon maiden");

        //V remove(Object key): deletes the specified element according to the key and returns the deletion value
        System.out.println(map.remove("zhang wuji"));

Determines whether the specified key exists in the collection (returns a Boolean value)

        Map<String,String> map=new HashMap<String, String>();

        map.put("zhang wuji","Zhao Min");
        map.put("Guo Jing","Rong Yang");
        map.put("Guo Yang","little dragon maiden");

        //boolean containKey(Object key): determines whether the collection contains the specified key
        System.out.println(map.containsKey("Guo Jing"));

Empty collection

map.clear();

Judge whether it is empty

boolean isEmpty();

Set traversal

1. Get the value according to the key
2. Get the set of all keys
3. Get the set of all values

public static void main(String[] args) {
        Map<String,String> map=new HashMap<String, String>();

        map.put("zhang wuji","Zhao Min");
        map.put("Guo Jing","Rong Yang");
        map.put("Guo Yang","little dragon maiden");

        //V get(Object key): get the value according to the key
        System.out.println(map.get("zhang wuji"));

        //Set < k > keyset(): get the set of all keys
        Set<String> keySet=map.keySet();
        for(String key:keySet){
           System.out.println(key);
        }

        //Collection < V > values(): get the collection of all values
        Collection<String> values=map.values();
        for(String value:values){
            System.out.println(value);
        }
    }

HashedMap

Set traversal

public static void main(String[] args) {
        //Create a HashMap collection object
        HashMap<String,Student> hm=new HashMap<String,Student>();

        //Create student object
        Student s1= new Student("Betty",20);
        Student s2= new Student("Peter",21);
        Student s3= new Student("Jimmy",22);

        //Add students to collection
        hm.put("001",s1);
        hm.put("002",s2);
        hm.put("003",s3);

        //Method 1 key value finding
        Set<String> keySet=hm.keySet();
        for(String key:keySet){
            Student value=hm.get(key);
            System.out.println(key+","+value.getName()+","+value.getAge());

        }
        System.out.println("---------");

        //Method 2 new an object and use the object to find the key and value
        Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
        for (Map.Entry<String, Student> me:entrySet){
            String key = me.getKey();
            Student value = me.getValue();
            System.out.println(key+","+value.getName()+","+value.getAge());

        }    }

HashedMap set does not duplicate

First, rewrite the equals() HashedCode() method inside the Student

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

Then write in the main program

public static void main(String[] args) {
        HashMap<Student,String> hm=new HashMap<Student,String>();

        //Create student object
       Student s1= new Student("Betty",20);
       Student s2= new Student("Peter",21);
       Student s3= new Student("Jimmy",22);

       //Add students to the set, where keys and key values are placed
        hm.put(s1,"Xi'an");
        hm.put(s2,"Wuhan");
        hm.put(s3,"Zhengzhou");

        //Traversal set
        //Get the set of keys
        Set<Student> keySet=hm.keySet();
        for (Student key:keySet){
            String value = hm.get(key);
            System.out.println(key.getName()+","+key.getAge()+","+value);
        }
    }

Composite set

ArrayList nested HashMap

 public static void main(String[] args) {
        //To create an ArrayList collection, the element is HashMap < string, string >
        ArrayList<HashMap<String,String>> array=new ArrayList<HashMap<String,String>>();

        //Create a HashMap collection and add key value pair elements
        HashMap<String,String> hm1=new HashMap<String,String>();
        hm1.put("Sun CE","Bridge");
        hm1.put("Zhou Yu","Little Joe");
        //Add HashMap as an element to the Arraylist collection
        array.add(hm1);

        HashMap<String,String> hm2=new HashMap<String,String>();
        hm2.put("22","33");
        hm2.put("44","55");
        //Add HashMap as an element to the Arraylist collection
        array.add(hm2);

        HashMap<String,String> hm3=new HashMap<String,String>();
        hm3.put("66","77");
        hm3.put("88","99");
        //Add HashMap as an element to the Arraylist collection
        array.add(hm3);

        //Traverse the ArrayList collection
        for(HashMap<String,String> hm:array){
            Set<String> keySet=hm.keySet();
            for(String key: keySet){
                String value=hm.get(key);
                System.out.println(key+","+value);

            }
        }
    }

HashedMap nested ArrayList

public static void main(String[] args) {
        //The elements are String and ArrayList, where the ArrayList element is String
        HashMap<String, ArrayList<String>> hm=new HashMap<String, ArrayList<String>>();

        //Create an Arraylist collection and add elements
        ArrayList<String> sgyy=new ArrayList<String>();
        sgyy.add("Zhao Yun");
        sgyy.add("Zhuge Liang");
        //Add Arraylist as an element to the HashMap collection
        hm.put("Romance of the Three Kingdoms",sgyy);

        ArrayList<String> xyj=new ArrayList<String>();
        xyj.add("Tang Monk");
        xyj.add("Sun WuKong");
        //Add Arraylist as an element to the HashMap collection
        hm.put("Journey to the West",xyj);

        ArrayList<String> shz=new ArrayList<String>();
        shz.add("Wu Song");
        shz.add("lu zhishen");
        //Add Arraylist as an element to the HashMap collection
        hm.put("Water Margin",shz);

        //Traverse HashMap collection
        Set<String> keySet=hm.keySet();
        for(String key:keySet){
            System.out.println(key);
            ArrayList<String> value=hm.get(key);
            for(String s:value){
                System.out.println("\t"+s);
            }
        }
    }

LinkedHashSet

Set characteristics
1. Hash value and linked list implement set header, with predictable iteration order
2. The linked list ensures the order of elements, and the storage and extraction of elements are consistent
3. The hash value represents the unique element, and there are no duplicate elements

public static void main(String[] args) {
        //Create collection object
        LinkedHashSet<String> linkedHashSet=new LinkedHashSet<String>();
        linkedHashSet.add("hello");
        linkedHashSet.add("world");
        linkedHashSet.add("java");

        //Traversal set
        for(String s:linkedHashSet){
            System.out.println(s);
        }

Keywords: Java Back-end

Added by simjay on Sun, 30 Jan 2022 17:01:58 +0200