Collections are actually a very important concept, yeah, very important, used a lot. Now, do you pass data? You either use an object or you use a collection. Well, collections themselves are objects, and basically what you upload are properties of the object, the entire data set.
1, collections,
Collection object is a container object in memory, used to store batch data conveniently, the data in the collection is not persisted, after the program runs, the data will disappear. In the Java language, design collections fall into three main categories (to master)
Set Set: The Set class that implements the Set interface is called a Set Set;
List collections: The collection classes that implement the List interface are called List collections;
Map collection: The collection class that implements the Map interface is called a Map collection
Java interfaces and classes for collection design are in the java.util package (we need to import the corresponding collection class or interface when we use collections).
Only reference data can be placed in a collection, not primitive data (use the wrapper type for primitive data if necessary)
2
The Set does not contain duplicate elements. The equals method is used to determine whether two elements are the same element.
Set extends Collection extends Iterable Collection: The Iterable interface defines the behavior of iteratorsCopy the code
Set interface implementation class: HashSet,TreeSet
HashSet: Based on the hash algorithm to determine the location of elements in the set, HashSet storage and retrieval efficiency is very high;
Familiar with the basic operation apis of sets (add, delete, get elements, Set size, empty Set, etc.)
For collection elements: * * * * transfer data (toArray (), then use the way of data access to elements) | iterators
Iterator: A recursive method of obtaining collection elements that implements the Iterable interface (which defines methods for iterating access to collection elements).
An iterator is a pointer object that points to the first element of the collection by default, and can be moved by calling the hashNext method
Iterable Iterator<T> Iterator () : Iterable <T> Iterator () : Iterable <T> Iterator () : Iterable <T> Iterator () : Iterable <T> Iterator () : Iterable <T> Iterator () : Iterable <T> Iterator () Returns true. E next() returns the next element of the iteration. Void Remove () removes the last element returned by the iterator from the collection pointed to by the iterator (optional operation).Copy the code
For a Set, if the object in the Set has one property or two properties or… Property is the same as the same object, how to deal with? The default is to call the equals method of the object to compare, which is no longer sufficient for our business needs.
Solution: Rewrite the comparison rules (and rewrite hashCode) for the new business – no handwriting required, automatically generated using Eclispe;
An Iterator object is an object that implements an interface (Iterator interface).
TreeSet: the second implementation of the Set interface, TreeSet compared to HashSet, a sorting function;
Sorting methods: Sort elements using their natural order;
What is the natural order of elements? : This element implements the Comparable interface; (When elements are placed into a TreeSet collection, the TreeSet collection compares elements with the comparison rules defined by the Ann Comparable interface.)
Public interface Comparable{int compareTo(T o)} Public interface Comparable{int compareTo(T o)} Public interface Comparable{int compareTo(T o)}Copy the code
In the JDK, the wrapper types and strings for the eight basic data types implement this interface (Comparable interface) —- each provides a compareTo method for comparison
If you place other types of elements in a collection, you must manually implement the Comparable interface (the compareTo comparison method defined in the interface)
3. Internal and external comparators
Implementing the Comparable interface in a class, called implementing comparison rules within the class, called internal comparators; (The class that implements the Comparable interface defines an internal comparator.)
External comparator: write a separate class to implement a new interface: java.util.Comparator; External comparators are typically used in the collection utility class: Collections;
Collections mainly serve Collections of sets and lists. Methods in the Collections utility class are generally static methods.
How is an internal comparator different from an external comparator?
The Comparable interface is implemented to define internal comparators, and the Comparator interface is implemented to define external comparators
4, List collection
Features: The Set that implements the List interface is called a List Set. Compared to a Set, the elements of a List Set are ordered (each element has a subscript starting from 0), and the List Set can hold repeated data
List the main implement collection: ArrayList, Vector, LinkedList; (The apis of the three implementation classes are basically the same.)
public interface List extends Collection extends Iterable{
}
Copy the code
The main differences between ArrayList and Vector:
Both are collections of lists. ArrayList is thread-non-case and Vector is thread-safe.
Key differences between ArrayList and LinkedList:
ArrayList uses arrays to store data internally. LinkedList uses linked lists to store data. ArrayList is more efficient for adding, obtaining and modifying data, while LinkedList is more efficient for deleting and modifying data.
ArrayList class: also known as dynamic array; An ArrayList uses arrays to store data internally; When the container is full, it is automatically expanded.
transient Object[] elementData; ArrayList Uses arrays to store data; int newCapacity = oldCapacity + (oldCapacity >> 1); Each expansion is 1.5 times the original size;Copy the code
5. Map collection
Set Set and List Set belong to the Set of singletons. Map Set is a Set of double-column values (each element in the Set is a key-value object). The value of the corresponding key can be extracted according to the key.
Key — value is called an Entry object
Map collections of main implementation: HashMap | TreeMap | Hashtable | Properties (on IO flow section)
HashMap: The key in the collection can be null, and the value can be null(only one key can be null); HashMap is not thread-safe;
The main differences between a Map Set and a List and a Set are:
Map sets are two-column values, List and Set sets are single-column values.
How to traverse a Map collection through map.entry;
Map<String,String> map = HashMap<String,String>();
for(Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() +" - " + entry.getValue());
}
Copy the code
The main differences between HashMap and TreeMap:
TreeMap has a sorting capability over HashMap
The main differences between HashMap and Hashtable:
HashMap: The key in the collection can be null, and the value can be null(only one key can be null); HashMap is not thread-safe;
Hashtable: A Hashtable is thread-safe;
Map collection data:
Get individual elements: get values based on keys; Map<String,String> map =new HashMap<String,String>(); map.get(key); Get all keys; Set<String> keys = map.keySet(); Get all values: Collection<String> values = map.values(); Obtain all the data in the Map set: obtain all the keys first, and then obtain the values based on the keys. Set<String> keys = map.keySet(); Iterator<String> it = keys.iterator(); while (it.hasNext()) { String key =it.next(); String val = map.get(key); System.out.println(key +":" + val); } The elements in the Map collection are map. Entry objects, which can be directly iterated through map. Entry to get the elements; for(Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() +" - " + entry.getValue()); }Copy the code
6. Generic Types
Generics: A type to be determined (type placeholder). Generics are used in design, but when used, they need to be given a defined type (Object if not explicitly specified).
Use generics in collections: generic collections;
Generics are used in common classes: generic classes;
Methods use generics: generic methods;
Using generics in interfaces: Generic interfaces
Advantages of generics: Syntax checks are used to avoid adding data of the wrong type.
7. External comparator
Comparator: used when sorting collections;
The internal comparator is used in the TreeSet implementation class of the Set (elements placed in the TreeSet are required to implement the Comparable interface)
Internal comparators: Implements the Comparable interface to provide a compareTo comparison method inside a class, called an internal comparator;
External Comparator: Implements the Comparator interface. You can define any Comparator you want called an external Comparator. External comparators are mainly used in the Collections utility class
For example, lists do not have sorting capabilities. If you want to sort the elements of a List, you can use the Collections utility class and provide an external comparator.
Collection is different from Collection
Collection is the parent interface of List and Set; The Collections collection utility class