Overview of collections in Java
Map interface
Summarize
Java Collection Learning
1 How data is stored in Java 1 array (base data type + reference data type). 2 Collections (objects)
The disadvantage of storing data in Arrays 1: Once an array is created, the length is fixed at 2: The array cannot directly determine the number of valid elements
Overview of collections in Java:
Java Collection interface: Collection interface and Map interface
(The collection interface means that the collection of objects is not stored in the order in which they are added, and the elements within the collection can repeat the unordered, repeatable collection, which is list,set,queue.) Interface's parent interface)
Collection interface
|–list interface (stored ordered, can be repeated elements, equals ()
|–arraylist (Primary implementation class, thread insecure, high efficiency
|–linkedlist (for situations that need to be deleted or added frequently
|–vector (old list implementation class, thread safe
|–set interfaces: unordered, non-repeating elements, hashcode (), Equals ()
|–hashset |–linkedhashset |–treeset
Map interface (with a collection of mapping relationship "key-value" pairs (key-value pairs)
|–hashmap, Linkedhashmap,treemap, Hashtable (subclass
Properties)
Collection Useful method
1 Add (Object obj): Adds obj to the current collection
AddAll (Connection c) adds elements from the C collection to the current collection
Size () returns the number of elements in the collection
IsEmpty () determines whether the current collection is empty
2 Remove (Object obj) deletes the obj element, calling the Equals method of the element class first
Determines whether the current collection contains an OBJ element.
Judging criteria: Call the Equals () method of the class where the obj element is located.
Requirement: If the obj element is an object of our custom class, then a custom class is required to override Equals ()!
RemoveAll (Connection con) "Difference set" removes the intersection of the current set in the Con collection
Retainall (Collection con): "Intersection": Returns the common elements of the current collection and the Con collection, and returns a collection of this element to the current collection
Equals (Object obj): Determines whether the current collection and obj elements are equals ()
To return True, you must also have obj as a collection, and the element is the same as the element in the current collection.
Contains (Object obj) to determine whether the current collection contains an OBJ element
Containsall (Connection con) determines whether all elements in the Coll collection are contained in the current collection.
Hashcode ()
3 iterator (): Returns an object that implements the class of the iterator interface. (The object used to traverse the elements of the collection collection)
Iterator.hasnext () that returns whether the next position of the current position is null.
Iterator.next (): Position moves down, and then returns the element after the move down.
ToArray () Converts the collection to an array type.
List List = Arrays.aslist (new objcet[]{...,...});
List Interface Useful method
The list interface stores ordered, repeatable elements, "dynamic" arrays
Increment, add (Objctect obj)
Delete, remove (int index)
change, set (int index,object obj)
Check, get (int index)
Plug: add (int index,object obj)
Traversal: Iterator ()
Set interface
The Set interface stores unordered, non-repeatable elements, without adding an abstract method in the parent interface collection.
|–hashset (Primary implementation Class) can add null
|–linkedhashset, which is a subclass of HashSet, can be used when traversing in the order of adding time, for frequent traversal.
|–treeset
Set interface set:
1 unordered! = randomness, the real disorder is that the element in the underlying storage position is unordered, the HashSet collection is an array, but its elements are not in accordance with the order of additions to the collection, but by the value of the hash code of the element to determine the location
2 Non-repeatable, the subsequent element cannot be added when the same element is added to the Set collection.
In order to guarantee the above 2 points, ask:
The type of the element that is added into the Set collection must override the Equals () and Hashcode () methods, and ensure that the above two methods remain consistent.
How are the elements in the set set stored?
The set collection uses a hashing algorithm. First call the Hashcode () method of the class where the element is located, specify where the element is stored based on the hash code value, and, in the case of the same hash code value, use the Equals () method of the class that contains the element, and if the element is the same, you cannot add a collection of set types.
The Hashcolde method of a class of "digression" is consistent with the Equals method. A is equal to the B object, so the hash code value returned by the Hashcode method is the same, which requires that we rewrite the above method to be robust
In addition, when adding elements to the set set, if the code value of the element is the same, but the Equals method returns False, the later element can also be added.
Similarly, if both elements of the Equals method return True, but the code value returned by the Hashcode method is different, then the incoming element can also be stored.
If so, we're going to rewrite the robust Equals method and the Hashcode method.
HashSet class Collection element features: unordered and non-repeatable
Linkedhashset class Collection elements: Use a list to maintain the order in which elements are added to the collection, so that when the collection is traversed, it is also in the order in which the elements are added into the collection.
The main implementation class of the set interface is HashSet:
The 1,hashset class is a typical implementation class for a set interface, most of which use the HashSet class when using the set interface.
The 2,hashset class stores elements according to the hash algorithm, so it has good access and lookup performance.
Features of the 3,hashset class:
A: The ordering of elements cannot be guaranteed
B:hashset is not thread-safe
C: The collection element can be null
Summary HashSet
When an element is deposited into the HashSet collection, HashSet calls the object's Hashcode () method to get the Hashcode value of the object, and then determines where the object is stored in hashcode based on the HashSet value. The HashSet set determines the criteria for equality of two elements: two objects are compared by the hashcode () method, and the Equals () method return value of two objects is also equal
Linkedhashset is a subclass of HashSet Linkedhashset determines where an element is stored based on the hashcode value of the element, but it also maintains the order of the elements using the linked list, which makes the elements appear to be saved in the order in which they were inserted. Linkedhashset Insert performance is slightly lower than HashSet, but has good performance when iterating through all the elements in the Set. Linkedhashset does not allow collection elements to be duplicated. TreeSet class
1, adding an element to a collection of type TreeSet must be of the same type
2, can be arranged in the order specified by the elements, such as string and basic data types in order from small to large
3, when adding a custom class object to the collection, the custom class needs to implement the unique abstract method (CompareTo (Objcet obj)) method of the comparable interface.
That is, sort by the CompareTo method that is overridden in the custom class. (Natural sort)
The TreeSet class sorts in 1 natural sort, 2 specifies sort.
1: Natural Sort:
1: Class A WHERE element a is added to the TreeSet collection, the interface comparable interface needs to be implemented
2: Class A overrides the abstract method of the comparable interface CompareTo (Object o)
3: Ensure that the CompareTo method is consistent with the hashcode and the Equals method.
Understand:
When you add an element to a collection of type TreeSet, the first call to its element's CompareTo () method is compared, and once you return 0, although only one property value of the element object is the same, the program considers the two objects to be the same, Therefore, the latter element cannot be added to the TreeSet type collection.
We should therefore ensure that the CompareTo () method of the element type Hashcode () and the Equals () method are consistent!!!!
TreeSet type collection elements of the Sort method two
1: Create an object com that implements the class of the comparator interface, naturally overriding its abstract method compare (object O1,object O2).
2: The constructor that passes the above object COM as a parameter to the TreeSet
3: Add an object to the TreeSet type collection that implements the class that is involved in the comparator interface class compare (object O1, Object O2) method.
Ibid., the Compare method must also be consistent with the hashcode and equals methods.
There are two kinds of sorting methods for the TreeSet collection elements above:
When you can manipulate the type of a collection element, you can use the first sort method, allowing the element type to implement the comparable interface.
If you cannot manipulate element types, you can use the second sort method. The TreeSet constructor is used. Its parameter type is comparator, generally in the method uses inner class, constructs implements the concrete class of this interface.
Map interface
Data storage features in map
The data stored in 1,map is a Key-value key value pair
All keys in the 2,map are stored by set, and all keys are different from each other.
The custom class in key needs to override the Hashcode and Equals methods
All values in the 3,map are stored by collection.
The custom class in values needs to override the Equals method.
4, a Key-value key value pair constitutes a entry.
All entry are stored by the set. The storage location of the entry is determined by the location of the key in the entry.
|–hashmap, the main implementation class, thread insecure, can store null
|–linkedhashmap maintains the number of elements in the map collection with a linked list
|–treemap, sorted by the specified property of the key entry added to the Map collection. Require key to be an object of the same class (natural sort & Specify Sort)
|–hashtable old implementation class, thread safe, can not store null is not recommended for use
|–properties: is a subclass of the Hashtable class, typically used to handle property files, both key and value are string types.
Common methods in Map:
Object put (object Key,object value): Adds an element to the map, and if key exists in the map to add the entry key, the added entry replaces the already existing entry, and returns the value object in the previous entry.
Object remove (Object key): Delete this key-value according to the specified key
void Putall (Map t)
void Clear (): Empty
Object get (Object key): Gets the value of the specified key. If this key is not, NULL is returned
Boolean ContainsKey (Object key): Whether the specified key is included
Boolean Containsvalue (Object value): whether the specified value is included
int size (): Returns the length of the collection
Boolean IsEmpty ()
Boolean equals (Object obj)
Set KeySet () returns all keys in the map, type set
Collection values (), returns all the value in the map, type Collecton
Set EntrySet () returns all the entry in the map. Type is set
Set EntrySet = Map.entryset ();//returns all key-value pairs
for (Object O:entryset) {
Map.entry Entry = (map.entry) o;//in the useless generics, strong turn!!
System.out.println (Entry.getkey () + "--" + entry.getvalue ());
}
Using generics to iterate over a map
Map
An overview of the Java Advanced Features collection