java高级--Map

存储类型特点
Collection不唯一,无序
Llist不唯一,有序
Set唯一,无序

1 、map的使用

Map countries = new HashMap();
countries.put("China","中国")//将数据存入到map中
countries.size()//获取map的数据长度
//获取map集合中特定的key对应的value,返回的数据是object类型,需要强制数据类型转换。
String country = (String)countries.get("China"); 
countries.containsKey("USA")//获取key值是否包含usa
countries.remove("USA")、、移除键值为usa的value
countries.keySet();//获取键的集合,值的集合,键值对的集合
countries.values()


遍历Map
方法一:
Set keys = countries.keySet();//获取所有的key
for(Object obj : keys){
	String key = (String)obj;//由于获取到的所有的key都是对象类型,所以需要转成String类型
	String value = (String)countries.get(key);
}

方法二:
Iterator itor = keys.iterator();
while(itor.hasNext()){
	String key = (String)itor.next()//获取得到map中每一个key
	String value = (String) countries.get(key);//根据map中每个key对应的值去找对应的value
}

方法三:
Set set = countries.entrySet();//获取map中的键值对。
for(Object obj : set){
	Map.Entry me = (Map.Entry)obj;//将对象数据类型转换成Map.Entry类型
	String key =  (String)me.getKey();//获取出来键值对中的键
 	String value = (String)me.getValue()//获取map中的值
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值