Java Collection List,map,set differences and traversal

Source: Internet
Author: User

1.1 List, Set, map basic differences
11,List,set are inherited collection interfaces, map is not . 2 32, List:linkedlist, ArrayList, Vector4 5 Set:hashset, Linkedhashset, TreeSet6 7 Map:hashmap, HashTable, TreeMap8 93, the list element is placed in order and can be repeated . (Support for loop, iterator traversal)Ten  One The set element is unordered and unique , and cannot be duplicated if the repeating element is overwritten. (for loops are not supported and iterations are supported.) Cause: element unordered, determined by the hashcode of the element, relatively fixed) A  -4, List: dynamic growth, query efficiency is high, and deletion efficiency is low (reason: Because of order, so the addition and deletion of elements will cause other elements to change position) -  the Set: query efficiency is low, add and remove high efficiency (reason: Because of disorder, so the addition and deletion of elements will not cause other element position change) -  -5:, Thread safety: Vector, HashTable -  +Thread insecure: LinkedList, ArrayList, HashSet, HashMap, TreeMap
1.2 List

A list is characterized by its elements being stored in a linear fashion, in which duplicate objects can be stored in the collection.

    • ArrayList (): Represents the length can be changed by the group. Elements can be randomly accessed, and it is slow to insert and delete elements into ArrayList ().
    • LinkedList (): A linked list data structure is used in the implementation. Fast insertion and deletion, and slow access times.

1. Adding elements

1 list<string> list=new arraylist<string>(); 2 List.add ("one element");

2. Deleting elements

 1  for  ( int  i = 0; I < list.size (); I++)  {  3  User result = List.get (i);  4  if  (result.getid () = 3)  5   {   List.remove (result);  7  System.err.println ("ID:" + result.getid () + "\tname:" + Result.getname ());  8   9  }

3. The most basic method of retrieving all objects in a collection:

11, for Loop2  for(inti=0; I<list.size (); i++){        3 System.out.println (List.get (i)); 4 }5 //can also6  for(String str:list) {7 System.out.println (str);8 }92, Iterators (Iterator)TenIterator it=List.iterator ();  One      while(It.hashnext ()) { A System.out.println (It.next ());  -}
1.3 Map

Map is a collection of key object and value object mappings, each of which contains a pair of key objects and value objects.

Map does not inherit from the collection interface, and when retrieving an element from the map collection, the corresponding value object is returned whenever the key object is given.

1, adding and deleting elements operation

1 New Hashmap<string, string>(); 2 map.put ("1", "a");        // add an element with a key of 1 and a value of a 3 map.remove ("1");          // Remove all elements of key 1

2. Loop traversal

1  //first: Traversing key and value via Map.keyset, universally used, two-time value2  for(String key:map.keySet ()) {3System.out.println ("key=" + key + "and value=" +Map.get (key));4 }5 6 //The second type: Use iterator to traverse key and value via Map.entryset:7Iterator<map.entry<string, string>> it =Map.entryset (). iterator ();8  while(It.hasnext ()) {9map.entry<string, string> Entry =It.next ();TenSystem.out.println ("key=" + entry.getkey () + "and value=" +Entry.getvalue ()); One } A  - //The third type: Traversing key and value via Map.entryset, recommended, especially when the capacity is large -  for(Map.entry<string, string>Entry:map.entrySet ()) { theSystem.out.println ("key=" + entry.getkey () + "and value=" +Entry.getvalue ()); - } - //Fourth: Traverse all value through Map.values (), but cannot traverse key -  for(String v:map.values ()) { +System.out.println ("value=" +v); -}
1.4 Set

Set is the simplest kind of collection. The objects in the collection are not sorted in a particular way, and there are no duplicate objects. The set interface mainly implements two implementation classes:

      • The Hashset:hashset class accesses objects in the collection according to the hashing algorithm, and accesses it faster. The object that is deposited into the hashset must be defined hashcode ()
      • The Treeset:treeset class implements the SortedSet interface and is able to sort the objects in the collection. The set of the save order, with the underlying tree structure. Use it to extract ordered sequences from the set.
      • Linkedhashset: has hashset query speed, and internally uses the chain list to maintain the order of elements (the Order of insertions). The result is displayed in the order in which the elements are inserted when iterating through the set using Iterators.
 1  Set set=new   HashSet ();  2  string S1=new  string ("Hello" Span style= "color: #000000;"  >);   3  String s2=S1;  4  string S3=new  string ("World" Span style= "color: #000000;"  >);   5  set.add (S1);   6  set.add (S2);   7  set.add (S3);     8  System.out.println (Set.size ()); // 

How does the

Set's Add () method determine if an object is already stored in the collection?  

1 Boolean isexists=false;   2 Iterator iterator=set.iterator ();   3  while (It.hasnext ())           {   4     String oldstr=it.next ();   5     if (Newstr.equals (oldstr)) {  6         isexists=true;   7     }  8

function method of Set

The set has exactly the same interface as the collection, so there is no additional functionality, unlike the previous two different lists. In fact set is collection, but behaves differently. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently.) Set does not save duplicate elements (more responsible for how the elements are judged)

Set: Each element that is stored in the set must be unique because set does not save duplicate elements. The element that joins the set must define the Equals () method to ensure the uniqueness of the object. The set has exactly the same interface as the collection. The set interface does not guarantee the order in which elements are maintained.

Java Collection List,map,set differences and traversal

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.