Nesting is not a new set of knowledge, only the contents of the collection is a collection, such as a collection of nested Collection, a collection Map Collection set of nested within each other, Map collection of nesting.
L nested ArrayList ArrayList
ArrayList< ArrayList<String>>
Collection< ArrayList<Integer>>
l Map nested ArrayList
HashMap<String, ArrayList<Person>>
ArrayList< HashMap<String, String>>
l Map nested set
HashMap<String, HashMap<String,String>>
HashMap<String, HashMap<Person,String>>
Set of nested traversal:
package com.oracle.xunhuan; import java.util.HashMap; import java.util.Iterator; import java.util.Set; import com.oracle.Demo01.Person; public class workf_1 { public static void main(String[] args) { //外内 HashMap<String,HashMap<Person,String>> k =new HashMap<String,HashMap<Person,String>>(); //添加数据 先添加值 HashMap<Person,String> n1=new HashMap<Person,String> (); HashMap<Person,String> n2=newThe HashMap <the Person, String> (); n1.put ( new new the Person ( "chicken roll", 25), ". 1" ); n1.put ( new new the Person ( "chicken curry", 14), "2" ); N2 .put ( new new the Person ( "Burger King", 23 is), ". 3" ); n2.put ( new new the Person ( "beef package" 10), "4" ); // add add key data values. k.put ( "chicken" , N1); k.put ( "hamburger" , N2); // traverse reinforcement for keyset + // Get the outermost Key the Set <String> KA = k.keySet (); Acquiring key set with an outer layer of the inner String Keyw = S; // use the key value of the outer layer, to obtain a set of custom inside the HashMap <the Person, String> valueN = k.get (Keyw); // Get an inner layer key value of the Set <the Person> WA = valuen.keySet (); for (the Person T: WA) { System.out.println (Keyw . "" + ".." + T + + valuen.get (T)); } } / * iterator print loop * / / * Get the set of elements * iteger loop through * / // 1. Get the value of the outer loop to print key 2. It is worth to use the outer key value (the set of inner layer) 3 / / 1. Get the value of the outer key, iterator print cycle SET <String> key2 = k.keySet (); // iterator the Iterator <String> = DED key2.iterator (); // loop to print the while (ded.hasNext ()) { // get every key value String key3 = ded.next (); // use the key value of the set value is obtained within the HashMap <the Person, String> value2 = k.get (key3); // key within the set of values to obtain the set <the Person> KEY4 = value2.keySet ( ); / * loop through obtain iterator * / the iterator <the Person> ded1 = key4.iterator (); / * loop to print* / The while (ded1.hasNext ()) { // for each key within the set of values to obtain the Person NKey = ded1.next (); System.out.println (key3 + "..." NKey + + "..." + value2.get (NKey)); } } / * enhanced FOR + entry entry is a pair of keys. Not isolated * / // Get value larger key value and set the Set <HashMap.Entry <String, the HashMap <the Person, String >>> SR = k.entrySet (); // circulating the key, value for (the HashMap. the Entry <String, HashMap <the Person, String >> the p-: =p.getKey(); /*获得value值*/ HashMap<Person,String> entry_value=p.getValue(); /*获得内层集合的key,value值*/ Set<HashMap.Entry<Person,String>> sc=entry_value.entrySet(); for(HashMap.Entry<Person,String> c:sc) { Person entry_key2=c.getKey(); String entry_value2=c.getValue(); System.out.println(entry_key+". ."+entry_key2+".."+entry_value2); } } /*+ Entry traverse iterator * / / * Get large collection of key value * / the Set <HashMap.Entry <String, the HashMap <the Person, String >>> ST = k.entrySet (); / * Get large collection iterator * / the Iterator <HashMap.Entry <String, HashMap <the Person, String >>> STD = st.iterator (); the while (std.hasNext ()) { // get a large collection of key values HashMap.Entry <String, HashMap < the Person, String >> Iterator_ded = std.next (); // extract a key value String Iterator_key = Iterator_ded.getKey (); // extract value values HashMap <Person, String>Iterator_value=Iterator_ded.getValue (); / * use of the key value to obtain a large collection of the inner set of key, value value * / the Set <HashMap.Entry <the Person, String >> STC = Iterator_value.entrySet (); / * Get the inner set iterator * / the iterator <HashMap.Entry <Person, String >> Nd = stc.iterator (); the while (nd.hasNext ()) { // for each key value obtained inner layer HashMap.Entry <Person, String > Iterator_nd = nd.next (); // extracted inner key value of the Person nckey = Iterator_nd.getKey (); // extract a value can rub value String ncvalue =Iterator_nd.getValue(); System.out.println(Iterator_key+"__"+nckey+"--"+ncvalue); } } } }