锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

集合框架(Collection)——Map和例题

时间:2023-09-12 22:37:02 t110电容

目录

1.Map

2.案例

案例一

案例二

案例三


1.Map

1. Map
1.1 特点:元素以键值对的形式无序添加,键不能重复,值可以重复
它没有继承Collection接口
1.2 遍历
1.2.1 先取出保存的所有键Set,再遍历Set即可(2种)
1.2.2 先取出保存的一切Entry的Set,再遍历此Set即可 (重要点)

2.HashMap与HashTable之间的区别
同步既排队 线程安全的 hashtable 键不可以为null,值也不能为null
异步 非安全的 hashmap 键可以为null,值也可以为null

2.案例

案例一

Demo01.java

package Ktdm;  import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set;  public class Demo01 {  @SuppressWarnings("unlikely-arg-type")  public static void main(String[] args) {      //map集合的特点 无序,键值对,键不能重复,值可以重复   Map map=       new HashMap();      map.put("阿危", 250);   map.put("果果", 520);   map.put("菜菜", 521);         //map集合的遍历  1.获取map集合所有key的set集合,再通过key获取value值   Set ks = map.keySet();      for (String s : ks) { //   System.out.println(s);    ////通过键获得相应的值    Object value = map.get(s);    System.out.println(s "=" value);   }      ///这个数据覆盖了上一个数据   map.put("菜菜", 5211);   map.put("果果", 521);      System.out.println("----------------------");    //  Collection values = map.values(); //  for (Object o : values) { //   System.out.println(o); //    //  }      for (String s : ks) { //   System.out.println(s);    ////通过键获得相应的值    Object value = map.get(s);    System.out.println(s "=" value);   }      System.out.println("----------------------");   //map遍历方式 2.首先取出保存的所有键值Entry的Set,再遍历此Set即可         Set> entrySet = map.entrySet();      for (Entry entry : entrySet) {    System.out.println(entry);    System.out.println(entry.getKey());    System.out.println(entry.getValue());   }                          }  }  
     

效果图:

案例二

Demo02.java

package Ktdm;  import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; import java.util.Set;  public class Demo02 {  public static void main(String[] args) {   //map集合的特点 无序,键值对,键不能重复,值可以重复   Map map=     new HashMap();    Map map1=     new Hashtable();    //hashMap能以null为键,以null为值   map.put("阿危", 250);   map.put("果果", 520);   map.put("菜菜", 521);   map.put(null, "lx");      //hashtable不能以null键,不能用null为值 //  map1.put("阿危", null); //  map1.put("果果", 520); //  map1.put("菜菜", 521); //  map1.put(null, "lx");      Set> entrySet = map.entrySet();      for (Entry entry : entrySet) {    System.out.println(entry);   }                                      }  } 

效果图:

案例三

Student.java

public class Student implements Serializable{  /**   *    */  private static final long serialVersionUID = 4682477983251791848L;  private String name;  private Integer source;  public String getName() {   return name;  }  public void setName(String name) {   this.name = name;  }  public Integer getSource() {   return source;  }  public void setSource(Integer source) {   this.source = source;  }    public Student() {   // TODO Auto-generated constructor stub  }  @Override  public String toString() {   return "Student [name="   name   ", source="   source   "]";  }  public Student(String name, Integer source) {   super();   this.name = name;   this.source = source;  } }

Demo03.java

 public static void main(String[] args) {  Map>> map = 
			new HashMap>>();
		
//====================================================================
		
	List als = new ArrayList();
	als.add(new Student("张晓东",90));
	als.add(new Student("张晓西",75));
		
	List bls = new ArrayList();
	bls.add(new Student("张晓北",80));
	bls.add(new Student("张晓南",82));
		
	Map> amap = new HashMap>();
	amap.put("T110", als);
	amap.put("T111", bls);
		
//====================================================================
 
	List cls = new ArrayList();
	cls.add(new Student("张三",90));
	cls.add(new Student("李四",100));
		
	List dls = new ArrayList();
	dls.add(new Student("王五",70));
	dls.add(new Student("小六",100));
		
	List els = new ArrayList();
	els.add(new Student("小八",70));
	els.add(new Student("小九",100));
		
	Map> bmap = new HashMap>();
	bmap.put("T206", cls);
	bmap.put("T222", dls);
	bmap.put("T208", els);
		
//====================================================================
 
	List fls = new ArrayList();
	fls.add(new Student("可乐",60));
	fls.add(new Student("雪碧",50));
		
	List gls = new ArrayList();
	gls.add(new Student("哇哈哈",90));
	gls.add(new Student("老干妈",80));
		
	Map> cmap = new HashMap>();
	cmap.put("T230", fls);
	cmap.put("T231", gls);
			
	map.put("卓京初中部",amap);
	map.put("卓京高中部",bmap);
	map.put("卓京大学部",cmap);
		
	Set keySet = map.keySet();
	for (String key : keySet) {
		Map> value = map.get(key);
		System.out.println(key);
		Set keySet2 = value.keySet();
		for (String key2 : keySet2) {
			System.out.println("\t"+key2);
			List ls = value.get(key2);
			for (Student stu : ls) {
				System.out.println("\t\t"+stu.getName()+"  "+stu.getSource());
			}
		}
	}

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章