List of three implementations: ArrayList (Array) LinkedList (linked list) Vector (thread safe)
List Collection Traversal methods:
list<string> list = new arraylist<string> ();
List.add ("AAA");
List.add ("BBB");
List.add ("CCC");
/*
* List traversal is available in three ways: normal for iterator enhancement for
*
*/
Using normal for traversal
for (int i = 0; i < list.size (); i++) {
System.out.println (List.get (i));
}
System.out.println ("----------");
Using Enhanced for traversal
for (String str:list) {
System.out.println (str);
}
System.out.println ("=============");
Using iterators to traverse
Iterator<string> it = List.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
Three implementations of set: HashSet (hash storage) TreeSet (ordered storage) SortedSet (primarily for sorting operations)
set<string> set = new hashset<string> ();
Set.add ("www");
Set.add ("QQQ");
Set.add ("zzz");
/*
* There are two ways to traverse the Set collection: Enhanced for iterator
* Set Set has a feature: Cannot add the same element, output or unordered
*/
Using Enhanced for traversal
for (String Str:set) {
System.out.println (str);
}
System.out.println ("---------");
Using iterators to traverse
Iterator<string> it = Set.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
Three implementations of map: HashMap (unordered) Hashtable (unordered) TreeMap (sortable, sorted by key) the key in the map collection is not allowed to repeat
map<string, string> map = new hashmap<string, string> ();
Map.put ("111", "AAA");
Map.put ("222", "BBB");
Map.put ("333", "CCC");
/*
* There are two common ways to traverse a map collection:
* 1. Get all keys, get value by key using Get method
* 2. Get the relationship between key and value
*/
1. Get all keys, get value by key and use the Get method
set<string> keys = Map.keyset ();
for (String Str:keys) {
Value is obtained by key
String value = Map.get (str);
System.out.println (str+ ":" +value);
}
System.out.println ("----------");
2. Get the relationship between key and value
Set<entry<string, string>> set = Map.entryset ();
For (entry<string, string> entry:set) {
String keyv = Entry.getkey ();
String Valuev = Entry.getvalue ();
System.out.println (Keyv + ":" +valuev);
}
Traversal methods for List,set,map collections