Collection collection system

Collection collection system introduced

Collection set of map:

  

List collection:

   Ordered set, reference ArrayList and LinkedList comparison article,

Set Collection:

   Unordered set, there is no concept of index, index access does not pass.

HashSet:

  HashSet the elements can not be repeated, is determined based on the first comparison target hashCode values ​​are equal if they are equal then the comparison target address.

Equals and hashCode method can override comparison rules redefine classes:

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}

TreeSet

  Features: You can sort the natural elements of the collection, if you want to specify a collation, the Comparable interface can be inherited, or create a TreeSet time, specify a class object Comparable interface

 1 package set;
 2 
 3 import java.util.Comparator;
 4 import java.util.Set;
 5 import java.util.TreeSet;
 6 
 7 public class TestTreeSet {
 8     public static void main(String[] args) {
28         Set<Person> pSet2 = new TreeSet<Person>(new Comparator<Person>() {
29             public int compare(Person o2, Person o1) {
30                 int result = (o1.name+"").compareTo(o2.name+"");
31                 IF (Result == 0) { // represents two strings are equal 
32                      return o2.age - o1.age;
 33 is                  }
 34 is                  return Result;
 35              }
 36          });
 37 [          pSet2.add ( new new the Person ( "John Doe" , 20 is)); // zhangsan 
38 is          pSet2.add ( new new the Person ( "John Doe", 30)); // Lisi 
39          pSet2.add ( new new the Person ( "John Doe", 25)); // Lisi 
40          pset2 .add ( new new the Person ( "Cong", 30)); // licong
41         pSet2.add(new Person("周瑞发", 30)); // zhourunfa
42         for (Person person : pSet2) {
43             System.out.println(person);
44         }
45     }
46 }
47 
48 class Person /*implements Comparable<Person>*/{
49     String name;
50     int age;
51     
52     public Person(String name, int age) {
53         super();
54         this= .name name;
 55          the this .age = Age;
 56 is      }
 57 is      
58      / * public int compareTo (the Person O) {
 59          // String object implements compareTo method, sorted according to the natural ordering rules
 60          int Result = (+ o.name "") .compareTo (this.name + "");
 61 is          IF (Result == 0) {// represents two strings are equal
 62 is              return this.age - o.age;
 63 is          }
 64          return Result;
 65      } * / 
66  
67      public String toString () {
 68          return "the Person [name =" + name + ", Age =" + Age + "]" ;
69     }
70 }

 

LinkedHashSet:

  Features: orderly, unrepeatable container

  

Map collection system:

  

Unimportant, self-Baidu

Guess you like

Origin www.cnblogs.com/aqiu-jiang/p/11201563.html