Java containers Refer to these classes as List,set,map. Because of the problem of translation, ask the collection, collection these refer to them are a few.
List
ArrayList Random Access Fast
LinkedList Insert Delete Fast
This is a good understanding, array is arrays, random access fast. Link is the list, of course, the insertion is deleted quickly.
Set each element can only be placed once
HashSet using hashes
TreeSet uses a red-black tree, it sorts the elements, and the interface is SortedSet. The implementation of the comparator object can be passed as a sort function when initializing TreeSet
Linkedhashset also uses hashing, but uses the list to maintain the insertion order
Access Order of Set
HashSet according to Hashcode order
TreeSet There are two ways to sort elements: 1, object implements comparable. 2, new TreeSet incoming parameter T extends Comparator.
Easy-to-mistake: two don't get it wrong. You can not initialize any sorting methods when creating TreeSet, and no error will be made at this time. But add the object in the time will be an error, prompting cannot be cast to java.lang.Comparable
Linkedhashset displayed in the order in which they were inserted
About Hashcode
Hashcode is an int value, and the same object requires the hashcode of the get and put to be the same. The hashcode of different objects need not be different, as long as the Equals method can be separated.
A practical hashcode should be based on the object content, and the distributed average
Map the same key can only be placed once. This key uses the equals of the key object to determine whether to repeat
HashMap similar to HashSet, stored as key hashcode
Linkedhashmap can be stored in the order in which they were inserted, or at least recently in LRU order. Slower than HashMap, but faster iteration access
TreeMap similar to TreeSet, based on red-black tree sorting
In addition to Weakhashmap, Concurrenthashmap, Identityhashmap
Traversal of various containers:
1. Traversal of the list collection
list<integer> list = new arraylist<integer> ();
Use iterator traversal. The disadvantage is that it cannot be accessed randomly
Iterator iter = List.iterator ();
while (Iter.hasnext ())
{
Integer i = (integer) iter.next ();
}
This is better. Note that when you call get (i), you should never go beyond the list's length range, especially after the delete operation!
for (int i=0; i<list.size (); i++)
{
Integer n = list.get (i);
}
for (Integer i:list)
{
Can operate directly on I
}
The combination of the foreach and lambda expressions of the java8 is the most bullish. Suitable for traversing input or operation.
List.foreach (S-System.out.println);
2. Traversal of Set Set
hashset<integer> Iset = new hashset<integer> ();
Iset.add (3);
Iset.add (8);
Iset.add (7);
Iset.add (11);
The simplest must be it
Iset.foreach (i-System.out.println (i));
Can't use for, because there is no random access to the GET function!
Iterator<integer> iter = Iset.iterator ();
while (Iter.hasnext ())
{
Iter.next ();
}
3. The traversal of the map collection
map<string, string> map = new hashmap<string, string> ();
Map.put ("111", "one");
Map.put ("zz", "zzzzzzzz");
Map.put ("A", "Apple");
For EntrySet iterations
Iterator iter = Map.entryset (). Iterator ();
while (Iter.hasnext ())
{
Entry Entry = (Entry) iter.next ();
System.out.println (Entry.getkey () + ":" + entry.getvalue ());
}
Set iteration on key
Iterator iter2 = Map.keyset (). Iterator ();
while (Iter2.hasnext ())
{
String key = (string) iter2.next ();
SYSTEM.OUT.PRINTLN (key + ":" + map.get (key));
}
Summary of various collection containers in Java