栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录

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("tt"+stu.getName()+"  "+stu.getSource());
			}
		}
	}

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号