浅谈Java中Map和Set之间的关系(及Map.Entry)

  package Demo;

  import java.util.*;

  import java.util.Map.*;

  public class DemoMap {

  public static void main(String[] args) {

  text1();

  System.out.println("=========================================================");

  text2();

  }

  public static void text1(){

  Map DemoMap=new HashMap();

  DemoMap.put(4, "dddd");

  DemoMap.put(1, "a");

  DemoMap.put(3, "ccc");

  DemoMap.put(2, "bb");

  Collection> set=DemoMap.entrySet();

  System.out.println("set=="+set);

  Iterator> it=set.iterator();

  Map.Entry entry;

  while(it.hasNext()){

  entry=it.next();

  System.out.println("en.getKey()=="+entry.getKey());

  System.out.println("en.getValue()=="+entry.getValue());

  }

  }

  public static void text2(){

  Map DemoMap=new LinkedHashMap();

  DemoMap.put(4, "dddd");

  DemoMap.put(1, "a");

  DemoMap.put(3, "ccc");

  DemoMap.put(2, "bb");

  Iterator> set=DemoMap.entrySet().iterator();

  Entry temp;

  while(set.hasNext()){

  temp=set.next();

  System.out.println("getKey()=="+temp.getKey());

  System.out.println("getValue()=="+temp.getValue());

  }

  }

  }