Questions:
1. What is a collection
2. What kinds of collections are reused?
3. Storage sample of HashSet in set
4, the collection of traversal methods
Collection: A collection container that stores object data
Singleton Collection
--–| Collection the root interface of a singleton collection
————-| List If it is a collection class that implements the list interface, features: orderly, repeatable
—————-| ArrayList ArrayList layer is implemented using an object array, features: Fast query speed, adding and deleting slow
—————-| LinkedList LinkedList Bottom is the use of linked list data structure to achieve, features: Slow query, adding and deleting fast
—————-| The Vector is implemented using an object array, which is consistent with ArrayList, but is thread-safe and inefficient.
————-| Set If it is a set interface implementation of the collection class, with the characteristics: unordered, non-repeatable
—————-| The HashSet is implemented using a hash table and features: fast access speed
HashSet The principle of storage elements
When adding elements to HashSet, the Hashcode method of the element is called first to get the hash code value of the element, and then the hash code value is calculated to calculate the position of the element in the hash table.
Scenario 1: If the calculated position does not currently have any elements, then the element can be added directly to the hash table
Scenario 2: If another element is already present in the calculated position, then the Equals method of the element is also called to compare with the element at that position
If the Equals method returns True, the element is treated as a repeating element and is not allowed to be added, and if equals returns False, the element can also be added
—————-| The TreeSet layer is implemented using a red-black tree (binary tree) data structure, with the following features: Sorting the elements in the collection
TreeSet things to be aware of
1, when adding elements to TreeSet, if the elements have natural order characteristics, then the treeset will be sorted according to the natural order of the elements of the storage
2, when adding elements to TreeSet, if the element does not have the natural order of the characteristics, then the element belongs to the class must implement the comparable interface, the comparison of the rules defined in the CompareTo method
3, when adding elements to TreeSet, if the element does not have the natural order of the characteristics, then the element belongs to the class also does not implement the comparable interface, then when creating the TreeSet object must be passed to the comparator object
比较器的定义格式: class 类名 implements Comparator{ }
two-column collection
——— | the data stored by MAP exists in the form of a key-value pair, the keys can be duplicated, and the values can be repeated
———— | the bottom of the HASHMAP is also implemented using a hash table
———— | The TreeMap layer is also implemented using the red-black tree data structure, by default the elements are naturally sorted (String), and if two objects return a value of 0 at the time of comparison, the element repeats
———— | HashTable also uses a hash table to maintain, access to read fast, storage elements are unordered
Description of the HashSet sample
Class person{intId String name; Public Person(intId,string name) {super (); This. Id=id; This. name=name; } PublicStringtoString(){return "ID is:"+ This. id+"name is:"+ This. Name; }} Public classCollectiondemo { Public Static void Main(string[] args) {hashset<person> hs=NewHashset<person> (); Hs.add (NewPerson123,"Zhang San")); Hs.add (NewPerson123,"Zhang San")); System. out. println ("Elements of a collection"+HS); }}
The result of the run is
The element of the collection [ID is: 123 name is: Zhang San, id is: 123 name is: Zhang San]
If you add the Hashcode method that you implemented in the person class
publicinthashCode(){ returnthis.id; }
The result of the run is
The element of the collection [ID is: 123 name is: Zhang San, id is: 123 name is: Zhang San]
If you add your own implementation of the Hashcode method and the Equals method in the person class
publicinthashCode(){ returnthis.id; } publicbooleanequals(Object obj){ person p=(person)obj; returnthis.id==p.id; }
The result of the run is
The element of the collection [ID is: 123 name is: Zhang San]
The traversal method of the set has the usual method of the for loop iterator EntrySet, etc.
Package CN. Xlucas. List;Import Java. Util. ArrayList;Import Java. Util. HashMap;Import Java. Util. HashSet;Import Java. Util. Iterator;Import Java. Util. Map. Entry;Import Java. Util. Set;public class CollectionDemo1 {public static void main (string[] args) {System. out. println("======get Way traversal =========");Arraylist<string> ls=new arraylist<string> ();Ls. Add("Zhang San");Ls. Add("John Doe");Ls. Add("Harry");for (int i=0; I<ls.size (); i++) {System. out. println(LS. Get(i));}//Using iterators Note: The iterator cannot use the collection object to modify the number of elements in the collection during the iteration. If you need to modify the method to use an iterator, the System. out. println("====== iterator Way traversal =========");Hashset<string> hs=new hashset<string> ();Hs. Add("Zhang San");Hs. Add("John Doe");Hs. Add("Harry");Iterator<string> IT=HS. Iterator();while (it. Hasnext()) {System. out. println(It. Next());} System. out. println("====== iterator mode for enhanced traversal =========");for (String item:hs) {System. out. println(item);} System. out. println("======entryset Way traversal =========");Hashmap<string,string> mp=new hashmap<string,string> ();Mp. Put("1","Zhang San");Mp. Put("2","John Doe");Mp. Put("3","Harry"); Set<entry<string, string>> entrys=mp. EntrySet();for (entry<string,string> Entry:entrys) {System. out. println("Keying:"+entry. GetKey()+"value is:"+entry. GetValue());} }}
result is
======get Way Traversal =========
Tom
John doe
Harry
====== iterator Way Traversal =========
Tom
John doe
Harry
====== iterator mode for enhanced traversal =========
Tom
John doe
Harry
======entryset Way Traversal =========
Keying: 3 Value: Harry
Keying: 2 Value: John Doe
Keying: 1 Value: Zhang San
Explanation of collections in Java