Summary of the traversal of Set, List, and map

Source: Internet
Author: User

This evening I would like to see the generic, and later see the collection class here, because the collection class has been a bit of shadow, always confused, simply review the collection of the class of several traversal way bar, by the way to take notes, in case later look back.

In short, the Java Collection class implements the collection interface

Collection<--list<--vector

Collection<--list<--arraylist

Collection<--list<--linkedlist

Collection <--set<--hashset

collection<--set<--hashset<--linkedhashset

Collection<--set<--sortedset<--treeset

Set (SET): (HashSet) unordered and non-repeating

List (ArrayList and LinkedList) (lists): Unordered and repeatable

Map (map, key-value pair): (HASHMAP) ordered key value is not duplicated

Set Summary:

1. Set implementation is based on map (HASHMAP);

2. The elements in set cannot be duplicated, and if an existing object is added using the Add (Object obj) method, the previous object is overwritten;

There are two methods of set traversal:

<span style= "FONT-SIZE:12PX;" >public static void Testset () {//unordered do not repeat set<string> set = new Hashset<string> () Set.add ("I am haha"); Set.add (" I am haha "), Set.add (" I am the Curtain Burn "), Set.add (" I Am the Curtain Burns 2 ");//Traversal method one iteratoriterator<string> it = Set.iterator (); while ( It.hasnext ()) {String str = It.next (). toString (); System.out.println (str);} Traversal method Two forfor (String str:set) {System.out.println (str);}} </span>

List Summary:

1. All lists can contain only a single table of different types of objects, not key-value key-value pairs. For example: [Tom,1,c];

2. All lists can have the same elements, such as vector can have [tom,koo,too,koo];

3. All lists can have null elements, for example [tom,null,1];

4. Array-based list (vector,arraylist) is suitable for querying, while LinkedList (linked list) is suitable for adding, deleting operations.

Three ways to list traversal:

LinkedList:

<span style= "FONT-SIZE:12PX;" >public static void Testlinkedlist () {//ordered repeatable list<string> list = new linkedlist<string> (); List.add (" I am haha "), List.add (" I am haha "), List.add (" I Am the Curtain Burns "), List.add (" I Am the Curtain Burns 2 ");//Traversal of the first  getfor (int i = 0; i < list.size (); i++) { System.out.println (List.get (i)); Traversal of the second iteratoriterator<string> it = List.iterator (); while (It.hasnext ()) {String str = (string) it.next (); System.out.println (str);} Traversal of the third forfor (String str:list) {System.out.println (str);}} </span>

ArrayList

public static void Testarraylist () {//arraylist ordered allows repeating list<string> list = new arraylist<string> (); List.add ( "I am a small Yan"); List.add ("I am a Little Yan"); List.add ("I Am the Curtain"); List.add ("I Am the Curtain Burns 2");//traverse the first  getfor (int i = 0; i < list.size (); i++) { System.out.println (List.get (i)); Traversal of the second iteratoriterator<string> it = List.iterator (); while (It.hasnext ()) {String str = (string) it.next (); System.out.println (str);} Traversal of the third forfor (String str:list) {System.out.println (str);}}
Map traversal in five ways:

public static void TestMap () {//map unordered key value does not repeat map<string, string> Map = new hashmap<string, string> (); Map.put ( "Zhangsan", "I am Zhang San"), Map.put ("Devin", "I am DW"), Map.put ("haha", "I am haha");//Traverse Method one Map.keyset + forfor (String key: Map.keyset ()) {System.out.println ("key=" + key + ", value=" + map.get (key));} Traverse method Two Map.keyset +iteratoriterator<string> iterator = Map.keyset (). iterator (); while (Iterator.hasnext ()) { String key1 = Iterator.next (). toString (); System.out.println ("key=" + Key1 + ", value=" + map.get (Key1));} Traversal mode three map.valuesfor (String key2:map.values ()) {System.out.println ("value=" + Key2);} Traverse mode four Map.entryset + forfor (map.entry<string,string> entry:map.entrySet ()) {System.out.println ("key=" + Entry.getkey () + ", value=" + entry.getvalue ());} Traverse mode five map.entryset () + iteratoriterator <Map.Entry<String,String>> Entry = Map.entryset (). iterator (); while (Entry.hasnext ()) {map.entry<string, string> ent = Entry.next (); System.out.println ("key=" + ent.getkey () +", value=" + ent.getvalue ());}} 

in ArrayList and vectors, it takes the same amount of time to find the data from a specified location (through an index) or to add or remove an element at the end of the collection, as shown in O (1). However, if an element is added or removed elsewhere in the collection, the time spent will grow linearly: O (n-i), where n represents the number of elements in the collection, and I represents the index position at which the element is incremented or removed. Why is that? It is assumed that all elements after the first and second elements of the collection will perform the operation of the displacement. What does all this mean? This means that you just look for elements in a particular location or only add and remove elements at the end of the collection, so you can use either a vector or a ArrayList. If this is another operation, you might want to choose a different collection operation class. For example, the Linklist collection class takes the same amount of time to add or remove elements from any position in the collection as-O (1), but it is slow to use an element in the index-O (i), Where I is the location of the index. It's also easy to use ArrayList because you can simply use an index instead of creating an iterator object. Linklist also creates objects for each inserted element, all of which you have to understand that it also brings additional overhead. Finally, in the book Practical Java, Peter Haggar suggests using a simple array instead of a vector or ArrayList. This is especially true for programs with high efficiency requirements. Because the use of arrays (array) avoids synchronization, additional method calls, and unnecessary reallocation of space operations.

Summary of the traversal of Set, List, and map

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.