HashMap常用方法总结

我们前两篇文章围绕着集合的知识进行讲解,前两篇围绕着集合的一个分支collection的两个实现类,总结了常用方法。

本篇文章讲述集合的另一个分支Map的实现类——HashMap。

Map集合基于 key/value)映射。每个键最多只能映射一个值。键可以是任何引用数据类型的值,不可重复;值可以是任何引用数据类型的值,可以重复;键值对存放无序

HashMap常用方法

1.put(K key, V value) 将键(key)/值(value)映射存放到Map集合中。

2.get(Object key) 返回指定键所映射的值,没有该key对应的值则返回 null。

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		System.out.println(map.get("zhaobin"));
		System.out.println(map.get("tom"));
	}
}
输出结果 70
        null

3.size()  返回Map集合中数据数量。

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		System.out.println(map.size());
	}
}

输出结果:2

4.clear() 清空Map集合

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		System.out.println(map.size());
		map.clear();
		System.out.println(map.size());
	}
}
输出结果:
2
0

5.isEmpty () 判断Map集合中是否有数据,如果没有则返回true,否则返回false

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		System.out.println(map.size());
		map.clear();
		System.out.println(map.isEmpty());
		System.out.println(map.size());
	}
}
输出结果:
2
true
0

6.remove(Object key) 删除Map集合中键为key的数据并返回其所对应value值。

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		System.out.println(map.remove("bob"));
		System.out.println(map.size());
	}
}
输出结果:
60
1

7.values()  返回Map集合中所有value组成的以Collection数据类型格式数据。

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		map.put("bb", 6);
		System.out.println(map.values());
	}
}
输出结果:
[70, 6, 60]

8.keySet()  返回Map集合中所有key组成的Set集合

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		map.put("bb", 6);
		System.out.println(map.keySet());
	}
}
输出结果:
[zhaobin, bb, bob]

9.containsKey(Object key)  判断集合中是否包含指定键,包含返回 true,否则返回false

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		map.put("bb", 6);
		System.out.println(map.containsKey("bob"));
		System.out.println(map.containsKey("b"));
	}
}

 

输出结果:
true
false

10.containsValue(Object value)  判断集合中是否包含指定值,包含返回 true,否则返回false。

package test;

import java.util.HashMap;

public class Map {
	public static void main(String[] args) {
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		map.put("zhaobin",70);
		map.put("bob", 60);
		map.put("bb", 6);
		System.out.println(map.containsValue(70));
		System.out.println(map.containsValue(100));
	}
}
输出结果:
true
false

11.HashMap用entrySet() 遍历集合,

entrySet()  将Map集合每个key-value转换为一个Entry对象并返回由所有的Entry对象组成的Set集合

用法如下:

package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Test {
	
	public static void main(String[] args) {
		Map<String, String> studentMap = new HashMap<String, String>();
		studentMap.put("002", "小王");
		studentMap.put("001", "小李");
		
		Set<Entry<String, String>> studentSet = studentMap.entrySet();
		for (Entry<String, String> entry : studentSet) {
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println(key+":"+value);
		}
	}
}

 

 

  • 13
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值