Traversal methods for List,set,map collections

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.