The collection is actually because I have to study generics, I haven't learned it as a premise, so I will first come.
Java provides several ways to hold objects, including language built-in Array, as a container classs, also known as a Collection ClassES. The collection is very important in Java. Before discussing, first look at the classic issues in several interviews.
1 COLLECTION and COLLECTIONS.
2 LIST, SET, MAP inherits from the Collection interface.
3 ARRAYLIST and VECTOR differences.
4 HashMap and Hashtable distinction.
There is an answer from the tail, we start the topic.
Collection Collection interface
--Collection is any object group, and the elements are independent, usually have the same set of rules. Set List is derived from it.
Basic operation Add Element Add (Object Obj); Addall (Collection C);
Delete Element Remove (Object Obj); RemoveAll (Collection C);
Just intend to return RetainAll (Collection C);
A good way to access / travers the collection element is to use the Iterator interface (iterator is used to replace the enumeration)
Public interface itrator {
Public Boolean Hasnext (0;
Public Object Next (0;
Public void remove (0;
}
Set
- Collection without repeating items
There are three specific types of sets available
Hashset- Based on the set of lashes, adding the elements of the hash table to implement the Hashcode () method.
LINKEDHASHSET - Returns elements in addition to the sequence
Treeset - Data structure based on the tree
List list
- Location collection. Advance the elements of the list can be added to a specific location in the list or add to the end.
There are two specific versions
ArrayList - Similar to Vector, it is used to scales an array maintenance collection. the difference:
I. Synchronization: Vector is a thread safe, that is to say is synchronized, and ArrayList is unsafe, not synchronized. Data growth: When you need to grow, Vector default growth to the original one, and arraylist It is half of the original
LinkedList (Link list) - is a two-way linked list, each node has two pointers pointing to a node and the next node.
Used in FIFO, add Element RemoveFirst () with addList () to remove the element with addfirst () / removelast ()
Listiterator provides two-way traversal next () previous (), delete, replace, adding elements
Map table MAP
- Used for keywords / numeric pairs, like Dictionary
Three collection of MAP
Keyword set Keyset ()
Value set value ()
Project set enryset ()
Four specific versions
General mapping table for the HashMap-hash table
LINKEDHASHMAP- Extended HashMap, maintain insertion order when it is iterated for returns
WeakhashMap-Based on the mapping table of the weak reference hash, if you do not maintain a reference to the keywords outside the map, the memory recycling program reclaims it.
TreeMap-mapping table based on balanced tree
Collectes class, used for synchronization set, can also change the class of read read-only mode
E.g .:
Map mp = new hashmap ()
MP = collections.synchronizedmap (mp); // Generate a mapping table for thread security
MP = collections.unmodifiablemap (mp); // Generate readmap table comparable Natural sequence sort class Comparator-oriented collection class
Container TaxOmy
Collection interface: Collection List set; Map Iterator ListIterator.
Abstract class: AbstractCollection AbstractList AbstractSet AbstractMap AbstractSequentialist. Collection type in the old version
Vector class
Vector, is vector. A heterogeneous mixture can dynamically increase capacity. Simpree is as follows
For example, we have a vector: vector myvec = new vector (a_array.length)
Get the length of the vector: myVec.size ();
Assignment: set (int position, object obj) / setElementat (Object obj, int position) - Dynamic growth
Add (Object Obj) / addElement (Object Obj) Adds an object at the end of Vector
E.g.:myVec.add(new A_Array [0]);
Take out element: get (int position) / getElement (int position)
Stack class
It is a subclass of a vector. It is the stack whose data structure (this word can be referred to, don't be confusing with Heap- Heap). The next step is the way.
Stack () Texture
EMPTY () rebellion
Search () Check if the stack has an element
Peek () gets the top elements of the stack
POP () Sport
Push () into the stack
ENUMERATION interface
Dictionary class
dictionary. Key words / numeric way access data, if the mapping does not have this keyword, retrieve the NULL.
Hashtable class
It is the specific implementation of the Dictionary structure.
Interview answer answer
The difference between Collection and Collectes.
Collections is a class under java.util that contains a variety of static methods related to the collection.
Collection is an interface under java.util, which is a parent interface of a variety of collections.
List, SET, MAP inherits from the Collection interface? List, SET is MAP is not
ARRAYLIST and VECTOR differences.
I. Synchronization: Vector is a thread safe, that is to say is synchronized, and ArrayList is unsafe, not synchronized. Data growth: When you need to grow, Vector default growth to the original one, and arraylist It is half of the original
Differences between HashMap and HashTable
I. Historical reasons: HashTable is an implementation of the MAP interface introduced by the Java 1.2 based on the old Dictionary class.
II. Synchronization: Hashtable is a thread safe, that is, HashMap is unsafe, not synchronous
Three. Values: Only HashMap allows you to take null values as a key or value of a table
Refer to "Mastering Java2 J2se1.4" by Zukowshi "Thinking in Java" by Bruce Eckel