Collection classes in Java

Keywords: Java

Collection classes in Java

First: Introduce the collections commonly used in the collection:

    1]Collection

    2]Set

    3]List

    4]Queue

2. The use case diagram is given below.

Note: Collection UML use case diagram

Third: In Java, the comparison between sets and arrays:

1) In Java, the generation of collections can greatly facilitate the operation of a set of collections of the same data type.

2) The length of the set can be changed dynamically easily, while the length of the array can hardly be changed dynamically.

Arrays in Java can only store basic data types, but not any custom objects, such as data encapsulation classes.

Fourth: Characteristic comparison of Set, Map and List sets:

(1) Set set:

Set set is like a jar. It is an unordered set, in which the data elements are scattered and discharged.

Set sets are not stored in the order in which the elements are placed.

(3) When using Set sets, there are no two duplicate values required to be placed;

(4) How to judge whether two objects are equal in Set:

1 Degree: First, determine whether the two values are equal.

2 Degree: Determine whether the hashCode values of the two data in the set set set are the same;

The object in the two Set s is equal only if the above two situations are satisfied.

_: A Null data can be inserted into the Set collection;

(2) Map collection:

(1) Map sets, the most important feature is that they are stored in the form of Key-Value;

(2) Like Set Set, it is also disorderly in the layout structure.

(3) List collection:

List sets are a bit like arrays, which can remember the order in which elements are put.

(2) List sets are flexible in length, which is better than arrays;

Five: Here are some common collection classes:

(1)Connection:

1.1 Common methods:

  • add (Object) Adding an object to the collection returns the Boolean type
  • AddiAll (Collection) adds a complete collection to the collection and returns the Boolean type
  • contains(Object Obj) to see if there is an Obj object in the collection that returns a Boolean type
  • iterator() is used to traverse and return an Iterator object

Following 1.2, the commonly used methods of Collection are posted:

public static void main(String[] args){
		Collection<String> c=new ArrayList<String>();
		c.add(new String("Software 1522 Class"));
		c.add(new String("I am Monkey King"));
		c.add(new String("Java Course"));
		//Print out: [Software 1522 class, I am Monkey King, Java tutorial]
		System.out.println(c);
		boolean b=c.contains(new String("Java Course"));
		//Print out:true
		System.out.println(b);
		Collection<String> c2=new ArrayList<String>();
		c2.add(new String("I am Monkey King"));
		c.removeAll(c2);
		//Print out: [Software 1522 Class, Java Tutorial]
		System.out.println(c);
	}

(2)Set:

2.1 Common Method: Similar to Collection

2.2 As mentioned above, there is no duplicate value in the Set set. This is to prevent confusion in the Set. Here is a set of code to illustrate:

package njcc.java.collection;

import java.util.HashSet;
import java.util.Iterator;

public class SetCollection {
	//Write an internal class first
	static class SetColl{
		private int count;
		
		public SetColl(){}
		
		public SetColl(int count){
			this.count=count;
		}
		
		public int getCount() {
			return count;
		}

		public void setCount(int count) {
			this.count = count;
		}
		//Rewrite the ToString method
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "{"+count+"}";
		}
		@Override
		public int hashCode() {
			// TODO Auto-generated method stub
			return this.count;
		}
		
		@Override
		public boolean equals(Object obj) {
			// TODO Auto-generated method stub
			if(this==obj) return true;
			SetColl s=(SetColl) obj;
			if(s.count==this.count) return true;
			return false;
		}
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashSet<SetColl> hash=new HashSet<SetColl>();
		//Storage not according to size
		hash.add(new SetColl(5));
		hash.add(new SetColl(-5));
		hash.add(new SetColl(9));
		hash.add(new SetColl(10));
		//Print out: [{5}, {5}, {9}, {10}]
		System.out.println(hash);        //Thus, it can be explained that the order in which objects are stored is not remembered in the set set set.
		Iterator it=hash.iterator();
		//Because it.next() returns an Object object, you need to cast it
		SetColl set=(SetColl)it.next();
		//Change the first data to 5
		set.count=9;
		//Print out: [{9}, {5}, {9}, {10}]
		System.out.println(hash);
		boolean b1=hash.contains(new SetColl(9));
		//Output true as to which 9 the output is, then it must be the second 9.
		/*
		 * Initially, the first convenience is [{-5}, {5}, {9}, {10}]
		 * Its HashCode hypothesis is: 01 02 03 04
		 * When the first data-5 becomes 9, it becomes [{9}, {5}, {9}, {10}]
		 * But its HashCode is still: 01 02 03 04
		 * When comparing Set objects with the same values, HashCode must be the same, because the output is the second 9.
		 */
		System.out.println(b1);
		
	}

}

2.3 There is another implementation class in Set: TreeSet, which is described above as HashSet:

(1) Comparison of TreeSet and HashSet:

There are similarities between the two: once the data is set, try not to change one data, which will make the Set set very messy;

The difference between the two is that in TreeSet, the Comparable interface must be implemented for sorting.

2.4 The following code describes TreeSet:

package njcc.java.collection;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetCollection {
	
	static class Tree implements Comparable{
		private int count;
		private String name;
		public Tree(){};
		public Tree(int count,String name){
			this.count=count;
			this.name=name;
		}
		public int getCount() {
			return count;
		}
		public void setCount(int count) {
			this.count = count;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		//Rewrite toString method
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "{"+count+","+name+"}";
		}
		
		public int compareTo(Object o) {
			// TODO Auto-generated method stub
			Tree tree=(Tree) o;
			return count-tree.count;
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeSet<Tree> set=new TreeSet<Tree>();
		set.add(new Tree(5,"Zhang San"));
		set.add(new Tree(1,"Li Si"));
		set.add(new Tree(3,"Wang Wu"));
		set.add(new Tree(2,"Zhao Liu"));
		set.add(new Tree(4,"Xiao Wu"));
		//Output: [{1, Li 4}, {2, Zhao 6}, {3, Wang 5}, {4, Xiao 5}, {5, Zhang 3}]
		System.out.println(set);
		Iterator<Tree> in=set.iterator();
		in.next().count=10;
		//Output: [{10, Li 4}, {2, Zhao 6}, {3, Wang 5}, {4, Xiao 5}, {5, Zhang 3}]
		System.out.println(set);
		/*
		 *   Although TreeSet implements Compare sorting here, it later changes the value of the first element.
		 * TreeSet will not be rearranged because of changes in values.
		 * Even if an element in the TreeSet is deleted, the TreeSet will only change the index of its data, but it will not.
		 * Rearrange data elements
		 */
		System.out.println(set);
	}
}

(3) List: List is divided into ArrayList and Vector

3.1 Comparison of the two:

Both are implementation classes of List.

(2) Both are extensions to arrays in Java;

Compared with arrays, they can expand their own space freely.

In thread security, Vector itself is synchronous and thread-safe.

(4)Collections:

4.1 Differences between Collections and Collections:

Collection is essentially an interface, as can be seen from the illustrations in this article.

Collections are classes in the java.util package that contain various static methods for collecting operations.

4.2 The sort sorting method commonly used in Collections is described below, where the first parameter of sort must be List type:

​

package njcc.java.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

public class Compare {
	
	static class Tree implements Comparable<Tree>{
		private int count;
		private String name;
		public Tree(){};
		public Tree(int count,String name){
			this.count=count;
			this.name=name;
		}
		public int getCount() {
			return count;
		}
		public void setCount(int count) {
			this.count = count;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		//Rewrite toString method
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "{"+count+","+name+"}";
		}
		
		public int compareTo(Tree o) {
			// TODO Auto-generated method stub
			return count-o.count;
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Tree> list=new ArrayList<Tree>();
		list.add(new Tree(5,"Zhang San"));
		list.add(new Tree(1,"Li Si"));
		list.add(new Tree(3,"Wang Wu"));
		list.add(new Tree(2,"Zhao Liu"));
		list.add(new Tree(4,"Xiao Wu"));
		Collections.sort(list);
		for(Tree obj:list){
			System.out.println(obj);
		}
	}
}
​

In this way, we can see that in the data encapsulation layer of Java, the interface of Comparable is implemented, so the method of sort (arg0) can be directly selected.

Now compare the next way of writing:

package njcc.java.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;

public class Compare2 {
	
	static class Tree{
		private int count;
		private String name;
		public Tree(){};
		public Tree(int count,String name){
			this.count=count;
			this.name=name;
		}
		public int getCount() {
			return count;
		}
		public void setCount(int count) {
			this.count = count;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		
		//Rewrite toString method
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "{"+count+","+name+"}";
		}
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Tree> list=new ArrayList<Tree>();
		list.add(new Tree(5,"Zhang San"));
		list.add(new Tree(1,"Li Si"));
		list.add(new Tree(3,"Wang Wu"));
		list.add(new Tree(2,"Zhao Liu"));
		list.add(new Tree(4,"Xiao Wu"));
		//In the data encapsulation layer, the comparable interface is not implemented, but the method with two parameters in sort is selected. The first parameter is the object that needs to be sorted.
		//The second parameter is a custom comparator
		Collections.sort(list,new Comparator<Tree>(){

			@Override
			public int compare(Tree arg0, Tree arg1) {
				// TODO Auto-generated method stub
				return arg0.count-arg1.count;
			}
			
		});
		for(Tree obj:list){
			System.out.println(obj);
		}
	}
}

In this way, we can see that there is no time for Comparable interface in Java data encapsulation layer, but a direct selection of sort (arg0,arg1) method.

The first parameter is the List collection that needs to be compared. There are many objects in Java encapsulation layer. The second parameter is a self-defined comparator, which is sorted in ascending order.

(5) Summary:

After learning the above set, you will find that List is very convenient for finding, because it has a chain structure similar to array.

Set sets are arranged according to the structure of the tree, so inserting and deleting nodes is very convenient, but reading data is difficult, especially for finding after changing a certain value.

Terrible.

Therefore, in the future use of collections, specific problems should be analyzed, and then the best collections should be selected as storage containers.

Posted by super_man on Fri, 19 Apr 2019 16:30:34 -0700