A large collection of learning notes and tomb stealing notes
Summary of the Collection framework:
1 Collection interface: 2 Set interface: 3 HashSet // The hashCode method must be implemented for the object. The elements have no order. The efficiency is higher than that of the HashSet. 4. HashSet // is an extension of HashSet, supports sorting of elements in the rule set, and extracts 5 TreeSet by element insertion sequence. // ensures that the elements in the set are ordered. 6 List interfaces are opposite to HashSet: 7 ArrayList 8 LinkedList 9 Vector10 Stack // derived from Vector, Stack 11 Map interface: 12 HashMap // High Efficiency 13 LinkedHashMap // Add sequential storage, 14 TreeMap sorting can be obtained in the order of Addition
1 Collection Interface
1. This interface is the Root Interface for processing object sets. It implements several methods: 2 add (); 3 size (); 4 iterator (); 5 Iterator interface: 6 next (); 7 hasNext (); 8 remove ();
2 Set Interface
2.1 HashSet specific classes (hash set)A hash is a collection of elements without repeated elements. HASH lists are implemented using linked list arrays. Each list is called a bucket. You can use the add method to add elements. The contains method is used to quickly check whether an element already exists in the set. You only need to search for the element in the bucket and do not need to view all the elements in the set. The hash iterator accesses all buckets in sequence. The access order is random because the hash distributes elements in various locations of the table. HashSet should be used only when the sequence of elements in the set is not concerned.
HashSetTest. java1 package General; 2 import java. util. *; 3 import java. io. *; 4 public class hashSetTest {5 public static void main (String [] args) throws FileNotFoundException {6 Set <String> words = new HashSet <String> (); 7 // Method 1: This method does not need to throw an exception. 8 // InputStream inStream = SetTest. class. getResourceAsStream ("C:/Users/Maria/Desktop/file/SDN.txt"); 9 // Method 2: This method needs to throw an exception 10 InputStream inStream = new FileInputStream ("C: /Users/Maria/Desktop/file/SDN.txt "); 11 bytes in = new streams (inStream); 12 while (in. hasNext () 13 words. add (in. next (); 14 Iterator <String> iter2 = words. iterator (); 15 for (int I = 0; I <words. size () & iter2.hasNext (); I ++) 16 System. out. print (iter2.next () + ""); 17 System. out. println (); 18 System. out. println (words. size (); 19 System. out. println (words); // use toString to output the set 20} 21}HashSetTest. java1 output result: 2 3 tyjk ikkl tyujy tukyukuil wse ~uyku ~gteg 4 95 [tyjk, ikkl, tyujy, tukyukuil, wse, Hangzhou, uyku, Hangzhou, gteg]View Code
2.2 TreeSet specific classes (tree set) Similar to a hash set. A tree set is an ordered set. You can insert elements to the set in any order. When the set is repeated, each value is automatically displayed in the sorted order (this is opposite to the hash set ). For example:
1 SortedSet<String>sorter=new TreeSet<String>();2 sorter.add("Bob");3 sorter.add("Alice");4 sorter.add("Carl");5 for(String s:sorter)6 System.out.println(s+" ");
1 Result: 2 Bob Alice Carl
But how does TreeSet know how to sort elements? The first method assumes that the inserted elements of the tree set implement the Comparable interface. This interface defines a method.
1 public interface Comparable <T> 2 int compareTo (T other); // compare this object (this) with another object (other, if this is before other, a negative value is returned. If the two objects are in the same position in the sorting order, 0 is returned. If this is after other, a positive value is returned.
The second method is to pass the Comparator object to the TreeSet constructor to tell the tree set to use different comparison methods.
1 public interface Comparable<T>2 int compareTo(T a,T b);
3 List Interface 3.1 ArrayList specific classes (array List) ArrayList is a subclass of List. It is opposite to HashSet. It allows repeated elements to be stored and therefore ordered. The order in which the set elements are accessed depends on the type of the set. If you access ArrayList, The iterator starts from index 0 and Adds 1 to the index value of each iteration. However, if you access elements in a HashSet, each element appears in a random order.
ArrayListSetTest. java1 package General; 2 import java. util. *; 3 import java. io. *; 4 public class ArrayListSetTest {5 public static void main (String [] args) throws FileNotFoundException {6 Set <String> set = new HashSet <String> (); 7 set. add ("a"); 8 set. add ("B"); 9 set. add ("c"); 10 set. add ("c"); 11 set. add ("d"); 12 Iterator <String> iter1 = set. iterator (); 13 while (iter1.hasNext () 14 System. out. print (iter1.next () + ""); // use Iterator to output the 15 System. out. println (); 16 for (String e: set) 17 System. out. print (e + ""); // use the For Each output in combination with 18 System. out. println (); 19 System. out. println (set); // use toString to output the set 20}The output result of ArrayListSetTest. java1 is: 2 a B c d 3 a B c d 4 [a, B, c, d]View Code
3.2 category Lis (Linked List)A sort list is an ordered sequence that allows efficient insert and delete operations at any location.
LinkedListTest. java1 package General; 2 import java. util. *; 3 public class implements listtest {4 public static void main (String [] args) {5 List <String> a = new ArrayList <String> (); 6. add ("a"); 7. add ("B"); 8. add ("c"); 9 System. out. println (a); 10 11 List <String> B = new ArrayList <String> (); 12 B. add ("d"); 13 B. add ("e"); 14 B. add ("f"); 15 B. add ("g"); 16 System. out. println (B); 17 18 // added the add (), previous (), and hasPrevious () Methods 19 ListIterator <String> aIter =. listIterator (); 20 // a normal Iterator has only three methods: hasNext (), next (), and remove () 21 Iterator <String> bIter = B. iterator (); 22 23 // B is merged into a, and the elements in B are inserted at intervals of 24 while (bIter. hasNext () 25 {26 if (aIter. hasNext () 27 aIter. next (); 28 aIter. add (bIter. next (); 29} 30 System. out. println (a); 31 32 // delete a 33 bIter = B every two elements in B. iterator (); 34 35 while (bIter. hasNext () 36 {37 bIter. next (); 38 if (bIter. hasNext () 39 {40 bIter. next (); // remove and next appear in pairs. remove always deletes the 41 bIter in the previous order. remove (); 42} 43} 44 System. out. println (B); 45 46 // Delete the element 47. removeAll (B); 48 System. out. println (a); 49} 50}Listtest. java1 result: 2 [a, B, c] 3 [d, e, f, g] 4 [a, d, B, e, c, f, g] 5 [d, f] 6 [a, B, e, c, g]View Code
3.3 specific Vector class Vector: Array-based List encapsulates some functions not available in Array for our convenience, and it cannot be restricted by Array. The performance cannot exceed Array. Therefore, we need to use Array more whenever possible. Another important point is the Vector "sychrovized", which is the only difference between Vector and ArroyList. It implements a table similar to an array and automatically increases the capacity to accommodate the elements you need. Use the following table to store and retrieve objects as if they were in a standard array. You can also use an iterator to retrieve objects from a Vector. Vector is the only synchronization container class. When two or more threads access it at the same time, the performance is also good.
3.4 Stack specific classes This class is derived from a Vector and adds methods to implement stack operations. It is a post-import, first-out storage structure.
4 Map interface 4.1 HashMap class (hash ing) For more information, see the HashMap implementation principle.
4.2 LinkedHashMap class (link ing table) The difference between connecting a hash ing table and a hash ing table is that the link hash ing table uses the access sequence instead of the insert sequence to iterate the ing table entries.
Comparison of various classes: Collection class framework: