资讯详情

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

1.Map

2.案例

案例一

案例二

案例三


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<String, Object> map=       new HashMap<String, Object>();      map.put("阿危", 250);   map.put("果果", 520);   map.put("菜菜", 521);         //map集合的遍历  1.获取map集合所有key的set集合,再通过key获取value值   Set<String> 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<Object> 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<Entry<String, Object>> entrySet = map.entrySet();      for (Entry<String, Object> 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<String, Object> map=     new HashMap<String, Object>();    Map<String, Object> map1=     new Hashtable<String, Object>();    //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<Entry<String, Object>> entrySet = map.entrySet();      for (Entry<String, Object> 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<String, Map<String,List&ltStudent>>> map = 
			new HashMap<String, Map<String,List<Student>>>();
		
//====================================================================
		
	List<Student> als = new ArrayList<Student>();
	als.add(new Student("张晓东",90));
	als.add(new Student("张晓西",75));
		
	List<Student> bls = new ArrayList<Student>();
	bls.add(new Student("张晓北",80));
	bls.add(new Student("张晓南",82));
		
	Map<String, List<Student>> amap = new HashMap<String, List<Student>>();
	amap.put("T110", als);
	amap.put("T111", bls);
		
//====================================================================
 
	List<Student> cls = new ArrayList<Student>();
	cls.add(new Student("张三",90));
	cls.add(new Student("李四",100));
		
	List<Student> dls = new ArrayList<Student>();
	dls.add(new Student("王五",70));
	dls.add(new Student("小六",100));
		
	List<Student> els = new ArrayList<Student>();
	els.add(new Student("小八",70));
	els.add(new Student("小九",100));
		
	Map<String, List<Student>> bmap = new HashMap<String, List<Student>>();
	bmap.put("T206", cls);
	bmap.put("T222", dls);
	bmap.put("T208", els);
		
//====================================================================
 
	List<Student> fls = new ArrayList<Student>();
	fls.add(new Student("可乐",60));
	fls.add(new Student("雪碧",50));
		
	List<Student> gls = new ArrayList<Student>();
	gls.add(new Student("哇哈哈",90));
	gls.add(new Student("老干妈",80));
		
	Map<String, List<Student>> cmap = new HashMap<String, List<Student>>();
	cmap.put("T230", fls);
	cmap.put("T231", gls);
			
	map.put("卓京初中部",amap);
	map.put("卓京高中部",bmap);
	map.put("卓京大学部",cmap);
		
	Set<String> keySet = map.keySet();
	for (String key : keySet) {
		Map<String, List<Student>> value = map.get(key);
		System.out.println(key);
		Set<String> keySet2 = value.keySet();
		for (String key2 : keySet2) {
			System.out.println("\t"+key2);
			List<Student> ls = value.get(key2);
			for (Student stu : ls) {
				System.out.println("\t\t"+stu.getName()+"  "+stu.getSource());
			}
		}
	}

标签: t110电容

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

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司