First, set class
The origin of collection: Object oriented language to things are reflected in the form of objects, in order to facilitate the operation of multiple objects, it is necessary to store objects, collection is the most common way to store objects. Collection features: 1. A container for storing objects. (The container itself is an object, stored in the heap memory, which holds the address of the object) 2, the length of the collection is variable. 3. Primitive data type values cannot be stored in collections. (Only store objects)
Quick question: What if you want to use collections to store basic data types?
Case and unbox. Example: al. Add (5); Add (new Integer(5));
The difference between collections and arrays: although arrays can also store objects, but the length is fixed, collection length is variable. Arrays can store primitive data types; collections can store objects only. Composition and classification of set framework :(dotted lines are interfaces)
Here are some of the top-level interfaces in the collection framework.
2. Collection interface
Collection subinterface and common implementation classes:
Collection interface | – the List interface: orderly (deposit and take out the order of the same), element has index (Angle), element can be repeated. | – Vector, the internal number is * * * * * * * * data structure that is synchronous. Add delete, query is very slow! 100% extension (almost no) | – ArrayList: is internal
(The query starts from the first element in the container, because the memory space of the array is continuous, so the query is fast; In addition and deletion, the memory address of all elements must be changed, so the addition and deletion are slow.
| – LinkedList: internal is
Elements can be added and removed very quickly.
| – Set interface: disorderly, elements cannot be repeated. The methods in the Set interface are the same as Collection.
| – HashSet: is the internal data structure
Hash table
Is out of sync.
| – LinkedHashSet: internal data structure is a hash table and linked list, there is a sequence of HashSet.
| – TreeSet: the internal data structure is ordered
Binary tree,
The List interface:
The difference between a Set and a List
The essential difference
ArryList and Vector are variable-length arrays:
ArryList is thread-safe, Vector is thread-safe:
The HashSet overrides the hashCode and equals methods to ensure element uniqueness
How do you ensure that the elements of a HashSet are unique? Object uniqueness is achieved by using the hashCode and equals methods of the objects: -> If the hashCode values of the objects are different, then the hashCode values are directly stored in the hash table without checking the equals method. -> If the hashCode of the objects is the same, then check whether the equals method of the objects is true. If false, it is treated as a different element and stored.
Remember: If an object is to be stored in a HashSet collection, the object must override the hashCode and equals methods. In general, if you define a class that produces many objects, such as people, students, and books, you will need to override equals and hashCode to establish whether the objects are the same or not.
TreeSet’s Two ways to judge element uniqueness (How to order)
By default, TreeSet determines the uniqueness of elements by whether the return result of conpareTo, the comparison method of Conpare interface, is 0. If it is 0, it means that the same element does not exist.
Here, we present two ways of customizing the uniqueness of elements. Method 1: Compare elements by their attributes. Elements are required to do this
Method two: (development use this, master the use of the comparator) let the collection itself have the comparison function. Write your own
Think about:
How to achieve fifO and FIFO in this way?Let the comparator return either 1 or -1.