Java的Map集合常用方法

import java.util.*;
public class Main {
	public static void main(String[]args) {
		HashMap<Integer,Double> map=new HashMap<>();
		map.put(1, 2.4);
		map.put(6, 2.5);
		map.put(3, 6.8);
		System.out.println("①"+map);
		//遍历根据主键输出值
		Set<Integer> set=map.keySet();
		System.out.println("②"+map.keySet());
		for(Integer a:set) {
			Double value=map.get(a);
			System.out.println("③"+value);
		}
		//遍历值集合
		for ( Object value : map.values() ) {
			System.out.println(value);
			}

		//遍历输出键和值
		Set<Map.Entry<Integer, Double>> entries=map.entrySet();
		System.out.println(entries);
		for(Map.Entry<Integer, Double> entry:entries) {
			Integer key=entry.getKey();
			Double value=entry.getValue();
			System.out.println("④"+"key=" + key + " value=" + value);
		}
		//Iterator遍历
		Iterator it=entries.iterator();
		while(it.hasNext()) {
			Map.Entry me = (Map.Entry) it.next(); // ③ 获得键值对对象
			Object key = me.getKey(); // ④ 从键值对对象中获取key值
			Object value = me.getValue(); // ⑤ 根据key获得对应的value值
			System.out.println("key=" + key + " value=" + value);
		}
		//判断是否含义键
		System.out.println("⑤"+map.containsKey(1));
		//判断是否含义值
		System.out.println("⑥"+map.containsValue(2.4));
		//map.put(key,value) 可以添加 map.remove(key) 可以移除,map.clear() 可以清空 map.size()获取map长度
		
		//排序使用TreeMap
		//根据键值key
		Map<Integer,Double> tree=new TreeMap<>(
			new Comparator<Integer>() {
				@Override
				public int compare(Integer o1,Integer o2) {
					return o2-o1;//降序,升序不用写默认o1-o2 Map<Integer,Double> tree=new TreeMap();
				}
			});
		tree.putAll(map);
		System.out.println(tree);
		//根据value排序
		List<Map.Entry<Integer, Double>> list=new ArrayList<>(map.entrySet());
		Collections.sort(list,(o1,o2)->(int) (o1.getValue()-o2.getValue()));//o1.getValue()-o2.getValue()升序,反正降序
		for(Map.Entry i:list) {
			System.out.println("key   "+i.getKey()+" value "+i.getValue());
		}
		
	}
}

输出:
①{1=2.4, 3=6.8, 6=2.5}
②[1, 3, 6]
③2.4
③6.8
③2.5
2.4
6.8
2.5
[1=2.4, 3=6.8, 6=2.5]
④key=1 value=2.4
④key=3 value=6.8
④key=6 value=2.5
key=1 value=2.4
key=3 value=6.8
key=6 value=2.5
⑤true
⑥true
{6=2.5, 3=6.8, 1=2.4}
key 1 value 2.4
key 6 value 2.5
key 3 value 6.8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的Map是一种键值对存储的数据结构,它提供了快速查找和访问数据的能力。下面是Map集合常用的一些方法: 1. put(Object key, Object value):向Map中添加键值对。 2. get(Object key):根据键获取对应的值。 3. remove(Object key):根据键删除对应的键值对。 4. containsKey(Object key):判断Map中是否包含指定的键。 5. containsValue(Object value):判断Map中是否包含指定的值。 6. size():返回Map中键值对的数量。 7. isEmpty():判断Map是否为空。 8. clear():清空Map中所有的键值对。 9. keySet():返回Map中所有键的Set集合。 10. values():返回Map中所有值的Collection集合。 11. entrySet():返回Map中所有键值对的Set集合。 示例代码: ```java // 创建Map对象 Map<String, Integer> map = new HashMap<>(); // 添加键值对 map.put("apple", 1); map.put("banana", 2); map.put("orange", 3); // 获取值 int value = map.get("apple"); System.out.println(value); // 输出:1 // 判断是否包含键 boolean containsKey = map.containsKey("pear"); System.out.println(containsKey); // 输出:false // 判断是否包含值 boolean containsValue = map.containsValue(2); System.out.println(containsValue); // 输出:true // 获取键的Set集合 Set<String> keySet = map.keySet(); System.out.println(keySet); // 输出:[apple, banana, orange] // 获取值的Collection集合 Collection<Integer> values = map.values(); System.out.println(values); // 输出:[1, 2, 3] // 获取键值对的Set集合 Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); System.out.println(entrySet); // 输出:[apple=1, banana=2, orange=3] // 删除键值对 map.remove("banana"); // 清空Map map.clear(); // 判断Map是否为空 boolean isEmpty = map.isEmpty(); System.out.println(isEmpty); // 输出:true ``` 以上就是JavaMap集合常用的一些方法。使用Map可以方便地存储和访问键值对数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值