Set, List, Map Traversal method, setmap

Source: Internet
Author: User

Set, List, Map Traversal method, setmap

 

 

1 package com. shellway. javase; 2 import java. util. arrayList; 3 import java. util. collection; 4 import java. util. hashSet; 5 import java. util. iterator; 6 import java. util. list; 7 import java. util. map; 8 import java. util. set; 9 import java. util. treeMap; 10 11 import org. junit. test; 12 13 public class TestCollection {14 15 public static void print (Collection <? Extends Object> c) {16 Iterator <? Extends Object> it = c. iterator (); 17 while (it. hasNext () {18 Object object = (Object) it. next (); 19 System. out. println (object); 20} 21} 22 23 @ Test24 public void demo1 () {25 Set <String> set = new HashSet <String> (); 26 set. add ("AAA"); 27 set. add ("BBB"); 28 set. add ("CCC"); 29 print (set); 30 31 // The first Traversal method of the Set: Use Iterator32 Iterator <String> it1 = set. iterator (); 33 for (String ss: set) {34 System. out. println (ss); 35} 36 // Set's first Traversal method: Use foreach37 for (String sss: set) {38 System. out. println (sss); 39} 40 41 42 List <String> list = new ArrayList <String> (); 43 list. add ("DDDDD"); 44 list. add ("EEEEE"); 45 list. add ("FFFFF"); 46 print (list); 47 48 // The first Traversal method of the List: because the list is ordered, the size () and get () method To obtain 49 for (int I = 0; I <list. size (); I ++) {50 System. out. println (list. get (I); 51} 52 // The second Traversal method of List: Use Iterator53 Iterator <String> it = list. iterator (); 54 while (it. hasNext () {55 System. out. println (it. next (); 56} 57 // The third Traversal method of List: Use foreach58 for (String s2: list) {59 System. out. println (s2); 60} 61 62 63 Map <String, String> map = new TreeMap <String, String> (); 64 map. put ("Jerry", "10000"); 65 map. put ("shellway", "20000"); 66 map. put ("Kizi", "30000"); 67 print (map. entrySet (); 68 // The first Traversal method of Map: Obtain the key first, and then obtain the value value69 Set <String> sett = map. keySet (); 70 for (String s: sett) {71 System. out. println (s + ":" + map. get (s); 72} 73 // The second Traversal method of Map: get the key-Value Pair 74 for (Map. entry <String, String> entry: map. entrySet () {75 System. out. println (entry. getKey () + ":" + entry. getValue (); 76} 77} 78}Perform type security check and traversal on collection Objects Using Generics

 


Different collections (List, Set, and Map) in JAVA

Set accepts each object only once and uses its own internal sorting method (generally, you only care about whether an element belongs to the Set but not its order-otherwise, you should use the List ). Map also saves a copy of each element, but this is based on the "key". Map also has built-in sorting, so it does not care about the order of element addition. If the order of adding elements is very important to you, you should use javashashset or javashashmap. Summary: The List has sequences, duplicates, and no duplicates, and the map key is the same as the set. If you want to insert elements in the same order as List, use javashashset or javashashmap. In fact, there are two functional methods of List: one is the basic ArrayList, which has the advantages of random access elements and the other is the more powerful sort List, it is not designed for fast random access, but a more general method. List: Order is the most important feature of List: it ensures that the specific order of elements is maintained. List adds many methods to the Collection so that elements can be inserted and removed from the List (this is only recommended for using the List .) A List can generate ListIterator, which can be used to traverse the List in two directions, or to insert and remove elements from the List. ArrayList: List implemented by arrays. Quick and Random Access to elements is allowed, but it is slow to insert and remove elements into the List. ListIterator should only be used to traverse the ArrayList from the forward, rather than to insert and remove elements. This is much higher than the overhead of the shortlist. Optimize List: the sequential access is optimized, and the overhead of insert and delete to the List is not large. Random Access is relatively slow. (Use ArrayList instead .) The following methods are available: addFirst (), addLast (), getFirst (), getLast (), removeFirst (), and removeLast (), these methods (not defined in any interface or base class) allow the consumer list to be used as a stack, queue, and bidirectional queue. Set: Each element stored in the Set must be unique because the Set does not store duplicate elements. The equals () method must be defined for the element added to the Set to ensure the uniqueness of the object. Set has the same interface as Collection. The Set interface does not guarantee the order of maintenance elements. HashSet: A Set designed for quick search. The object stored in the HashSet must define hashCode (). TreeSet: The Set that stores the order. The bottom layer is the tree structure. It can be used to extract ordered sequences from the Set. LinkedHashSet: query speed with a HashSet, and internal use of the linked list to maintain the order of elements (insert order ). When you use the iterator to traverse the Set, the result is displayed in the order of element insertion. Map function method put (Object key, Object value) Add a "value" (desired) and a "key" associated with the "value) (use it for search ). The get (Object key) method returns the "value" associated with the given "key ". You can use containsKey () and containsValue () to test whether a Map contains a "key" or "value ". The standard Java class library contains several different maps: HashMap, TreeMap, LinkedHashMap, WeakHashMap, and IdentityHashMap. They all have the same basic interface Map, but the behavior, efficiency, sorting policy, saving object lifecycle and determining "key" equivalent policies are different. Execution efficiency is a big problem of Map. Let's take a look at ...... the remaining full text>

How to traverse the map set stored in the list

<%
List <Map> list = new ArrayList <Map> ();
Map <String, String> map1 = new HashMap <String, String> ();
Map1.put ("testaa", "mytestaa ");
Map1.put ("testbb", "mytestbb ");
Map <String, String> map2 = new HashMap <String, String> ();
Map2.put ("testcc", "mytestcc ");
Map2.put ("testdd", "mytestdd ");
List. add (map1 );
List. add (map2 );
PageContext. setAttribute ("list", list );
%>

Assume that the jstl label is used for output:
<C: forEach items = "$ {list}" var = "temp">
<C: forEach items = "$ {temp}" var = "map">
$ {Map. key} ---- >$ {map. value} <br>
</C: forEach>
</C: forEach>

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.