Java obtains all key and value values of Map.

Source: Internet
Author: User

1. Student. java

Package bo; public class Student {private String id; public Student (String id) {this. id = id ;}@ Overridepublic String toString () {return "Student:" + id ;}}

2. ClassRoom. java

Package bo; public class ClassRoom {private String id; public ClassRoom (String id) {this. id = id ;}@ Overridepublic String toString () {return "class:" + id ;}}

3. Test. java

Package bo; import java. util. arrayList; import java. util. collection; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. map. entry; public class Test {public static void main (String [] args) {// class 1 Collection
 
  
C1 = new ArrayList
  
   
(); ClassRoom classroom_1 = new ClassRoom ("Class 1"); Student s1 = new Student ("James"); Student s2 = new Student ("Xiaohong "); student s3 = new Student ("Xiao Guang"); c1.add (s1); c1.add (s2); c1.add (s3); // Class 2 Collection
   
    
C2 = new ArrayList
    
     
(); ClassRoom classroom_2 = new ClassRoom ("Class 2"); Student t1 = new Student (""); Student t2 = new Student ("Xiao Huang "); student t3 = new Student ("Xiaomi"); c2.add (t1); c2.add (t2); c2.add (t3); Map
     
      
> M = new HashMap
      
        > (); M. put (classroom_1, c1); m. put (classroom_2, c2); // Method 1: // convert to Set
       
         >>, And then convert to Iterator
        
          > Iterator = m. entrySet (). iterator (); while (iterator. hasNext () {Entry
         
           > Entry = iterator. next (); System. out. println (entry. getKey (); // get the key and output ClassRoom // System. out. println (entry. getValue (); // get value and output Collection
          
            > For (Student s: entry. getValue () {System. out. println (s) ;}} System. out. println ("="); // Method 2: Iterator it = m. keySet (). iterator (); // a Set of key values of the Set type and converts them to the iterator while (it. hasNext () {ClassRoom key = (ClassRoom) it. next (); // obtain the key value, ClassRoomSystem. out. println (key); for (Student s: m. get (key) {// get the value, Collection (Student) System. out. println (s) ;}}/* output result: Class 1 student: Xiao Ming Student: Xiao Hong Student: Xiao Guang class: Class 2 student: Xiao Student: Xiao Huang Student: xiaomi */}}
          
         
        
       
      
     
    
   
  
 


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.