Generally speaking, there are 3 types of collections: Set (Set), list (listing), and map (map). ,
First, collection interface
Collection is the most basic collection interface. The subordinate structure of the collection, the sub-interfaces derived by the collection interface have list and set.
The collection supports iterator operations and queries.
Iterator iter = Collection.iterator (); Get an iteration child
while (Iter.hasnext ()) {//To determine when there is the next element
Object obj = Iter.next (); Get the next element
}
-
- list ()
- linkedlist ()
- ArrayList ( array-based list , is not synchronized, performance is better than vector, For query operations )
- Vector ( synchronous synchronized, thread-safe, is the old historical collection class. )
list<string> list = new linkedlist<string> (), for (int i = 0; I <= 5; i++) {List.add ("a" +i);} System.out.println ("Set list=" +list); Collections.shuffle (list); Randomly arranged System.out.println ("randomly arranged list=" +list); Collections.sort (list); Sort System.out.println ("list= after sorting" +list); Collections.reverse (list); Reverse order System.out.println ("list=" in reverse order +list); SYSTEM.OUT.PRINTLN ("Binary Find ordered collection mode A3 position is" +collections.binarysearch (list, "A3");//binary search
Result is:
-
- Set, which represents the concept of a set in mathematical sense, does not contain duplicate elements, that is, there is no case where two objects a.equals (b) is true. The set set itself is unordered. )
- TreeSet ( can implement an ordered fetch element from set )
- HashSet ( storage efficiency is high in the collection, but objects that need to be added need to be implemented in such a way that the
HashSet appropriate hash code is allocated hashCode() .) )
1 PackageJavabasic;2 3 ImportJava.util.HashSet;4 ImportJava.util.Set;5 ImportJava.util.TreeSet;6 7 Public classSetexample8 {9 Public Static voidMain (string[] args) {TenSet<string> set =NewHashset<string>() ; OneSet.add ("Zhangsan") ; ASet.add ("LiSi") ; -Set.add ("Wyao") ; -Set.add ("Zhaoliu") ; theSet.add ("Wyao") ; - - System.out.println (set); - +Set<string> TreeSet =NewTreeset<string>(set); - System.out.println (treeSet); + } A}
Result:
The result shows that the repeating element "Wyao" appears only once, and the output is ordered after sorting with TreeSet, sorted alphabetically by alphabetical order.
Second, MAP interface
1. Map interface is not Collection an interface inheritance. Provides a key-to-value mapping. keys are unique and do not allow repetition, but values can have multiple values, separated by commas, and can be repeated.
Map (Key-value map)
-hashmap ( Map Inserting, deleting, and locating elements in) HashMap is the best choice. HashMap allows null values as key and value. It is more efficient to use the fast failure mechanism in a different step. )
-hashtable ( is one of the original collection classes, also known as a Legacy class.) Instead, Hashtable cannot be a null value as key and value. Synchronized,)
-weakhashmap
2. Comparison of TreeMap and HashMap
There are two common ways to implement map: HashMap and TreeMap. Decide which method to use, as needed. Among them, HashMap uses the fast failure mechanism to insert, delete, and query elements quickly in the map. If the key is traversed sequentially, the TreeMap method is used.
will be faster. HashMapthe implementation is explicitly defined using the key class that you want to add hashCode() . With the TreeMap implementation, the elements added to the map must be sortable.
Part of the content reference http://blog.csdn.net/u014136713/article/details/52089156
Collection of Java 1