——Java training, Android training, iOS training, .Net training, looking forward to communicating with you! ——
/strong>
There are many collections in Java, also called containers. The following figure shows the composition and classification of the collection framework.
1. Why do collection classes appear?
Object-oriented languages embody things in the form of objects, so in order to facilitate the operation of multiple objects, objects are stored. Collections are the most commonly used way to store objects.
2. Arrays and collections are both containers. What are the differences?
Although arrays can also store objects, their length is fixed; collection lengths are variable. Basic data types can be stored in arrays, while collections can only store objects.
3. Characteristics of collection classes
Collections are only used to store objects. The length of the collection is variable, and the collection can store different types of objects.
Collection
Collection is a common interface in the collection framework. There are two sub-interfaces under it: List and Set.
Affiliation:
Collection
|–List//Elements are ordered and elements can be repeated. Because the collection system has indexes.
|–Set//The elements are unordered and the elements cannot be repeated.
The following is an overview of Collection:
1 import java.util.*; 2 /* 3 Collection defines the common functions of the collection framework. 4 5 1 Add 6 add(e); 7 addAll(collention); 8 2 Delete 9 remove(e) 10 removeAll(collection); 11 clear(); 12 3 Judgment 13 contains(e) 14 isEmpty(); 15 4 Get 16 iterator() iterator iterator() 17 size(); 18 5 Get intersection 19 retainAll() 20 6 set variable array 21 toArray() 22 23 1 The parameter type of the add method is Object. To facilitate receiving any type of object 24 2 All objects stored in the collection are references (addresses) 25 26 Iterator 27 28 What is an iterator? 29 In fact, it is the way to remove elements from a set 30 Like the clip in the doll game machine 31 32 Iterator is a removal method and will directly access the elements in the collection 33 So the iterator is described in the form of an inner class 34 Obtain the object of the inner class through the container's Iterator() method 35 2. Notes on iteration 36 37 The iterator is common in the Collection interface, which replaces the Enumeration in the Vector class. 38 The next method of the iterator automatically retrieves elements downwards and avoids NoSuchElementException. 39 The return value type of the iterator's next method is Object, so remember to type conversion. 40 41 */ 42 class CollectionDemo 43 { 44 public static void main(String[] args) 45 { 46 method_get(); 47 } 48 public static void sop(Object obj)// Print all objects 49 { public int getAge()//Return age 55 { 56 return age; 57 } 58 }
2) The second method: comparator
1 import java.util.*; 2 /* 3 When the element itself does not have comparative properties, or the comparative properties are not required 4 At this time, the container itself needs to be comparative 5 The comparator is defined and the comparator object is passed as a parameter to the constructor of the TreeSet collection. 6 7 When both sorts exist, the comparator takes precedence. 8 9 Define a class, implement the Comparator interface, and override the compare method 10 11 */ 12 class TreeSetDemo2 13 { 14 public static void main(String[] args) 15 { 16 TreeSet ts = new TreeSet(); 17 18 ts.add(new Student("lisi02",22)); 19 ts.add(new Student("lisi02",21)); 20 ts.add(new Student("lisi007",20)); 21 ts.add(new Student("lisi09",19)); 22 ts.add(new Student("lisi06",18)); 23 ts.add(new Student("lisi06",18)); 24 ts.add(new Student("lisi007",29)); 25 //ts.add(new Student("lisi007",20)); 26 //ts.add(new Student("lisi01",40)); 27 28 Iterator it = ts.iterator();//Call the iterator in the collection for traversal 29 while(it.hasNext())//Traverse the collection, and judge this statement every time it.next is called 30 { 31 Student stu = (Student)it.next(); 32 System.out.println(stu.getName()+"..."+stu.getAge()); 33 } 34 } 35 } 36 class Student implements Comparable//This interface allows students to be comparative /span> 37 { 38 private String name; 39 private int age; 40 Student(String name,intage) 41 { 42 this.name=name; 43 this.age=age; 44 } 45 public String getName() 46 { 47 return name; 48 } 49 public int getAge() 50 { 51 return age; 52 } 53 public int compareTo(Object obj)//Override the compareTo method, define Your own way of comparison 54 { 55 if(!(obj instanceof Student)) 56 throw new RuntimeException("Not a student object" ); 57 Student s=(Student)obj; 58 if(this.age>s.age) 59 return 1; 60 if(this.age==s.age) 61 { 62 return this.name.compareTo(s.name); 63 } 64 return -1; 65 } 66 } 67 68 class MyCompare implements Comparator//Create a comparator and implement the Comparator interface 69 { 70 public int compare(Object o1,Object o2) 71 { 72 Student s1=(Student)o1; 73 Student s2=(Student)o2; 74 int num = s1.getName().compareTo(s2.getName());//Compare Name 75 if(num==0) 76 { 77 return new Integer(s1.getAge()).compareTo(new Integer(s2 .getAge()));//Compare ages 78 } 79 return num; 80 } 81 82 } 83 class StrLenComparator implements Comparator//Create a length comparator and implement the Comparator interface 84 { 85 public int compare(Object obj1,Object obj2) 86 { 87 String s1=(String)obj1; 88 String s2=(String)obj2; 89 if(s1.length()>s2.length())//Compare lengths 90 return 1; 91 if(s1.length()>s2.length()) 92 { 93 return s1.compareTo(s2); 94 } 95 return -1; 96 } 97 }
Self summary:
The List interface has three implementation classes: LinkedList, ArrayList, and Vector
LinkedList: The bottom layer is implemented based on a linked list. The linked list memory is scattered. Each element stores its own memory address and also stores the address of the next element. Linked lists are fast to add and delete, but slow to search
The difference between ArrayList and Vector: ArrayList is non-thread-safe and has high efficiency; Vector is thread-safe and has low efficiency
List is used to process sequences, while set is used to process sets.
Dark Horse Programmer – Java Basics—Collection (1)—Collection, set, list
69 {
70 public int compare(Object o1,Object o2)
71 {
72 Student s1=(Student)o1;
73 Student s2=(Student)o2;
74 int num = s1.getName().compareTo(s2.getName());//Compare Name
75 if(num==0)
76 {
77 return new Integer(s1.getAge()).compareTo(new Integer(s2 .getAge()));//Compare ages
78 }
79 return num;
80 }
81
82 }
83 class StrLenComparator implements Comparator//Create a length comparator and implement the Comparator interface
84 {
85 public int compare(Object obj1,Object obj2)
86 {
87 String s1=(String)obj1;
88 String s2=(String)obj2;
89 if(s1.length()>s2.length())//Compare lengths
90 return 1;
91 if(s1.length()>s2.length())
92 {
93 return s1.compareTo(s2);
94 }
95 return -1;
96 }
97 }
Self summary:
The List interface has three implementation classes: LinkedList, ArrayList, and Vector
LinkedList: The bottom layer is implemented based on a linked list. The linked list memory is scattered. Each element stores its own memory address and also stores the address of the next element. Linked lists are fast to add and delete, but slow to search
The difference between ArrayList and Vector: ArrayList is non-thread-safe and has high efficiency; Vector is thread-safe and has low efficiency
List is used to process sequences, while set is used to process sets.
Dark Horse Programmer – Java Basics—Collection (1)—Collection, set, list