目录
Map是一个接口类,没有继承Collection。存储的是键值对,键一定是唯一的,不能重复。
| 方法 | 说明 |
| V get(Object key) | 返回key对应的value |
| V getOrDefault(Object key, V defaultValue) | 返回key对应的value, key 不存在,返回默认值 |
| V put(K key, V value) | 设置key对应的value |
| V remove(Object key) | 删除key对应的映射关系 |
| Set | 返回所有key的不重复集合 |
| Collection | 返回所有value的可重复集合 |
| Set | 返回所有的key-value映射关系 |
| boolean containsKey(Object key) | 判断是否包含key |
| boolean containsValue(Object value) | 判断是否包含value |
借助Set
| 方法 | 说明 |
| K getKey() | 返回entry中的key |
| V getValue() | 返回entry中的value |
| V setValue(V value) | 将键值对中的value替换为指定value |
- Set
> entrySet = hashmap.entrySet(); - for(Map.Entry
entry : entrySet){ - int a = entry.getKey();
- System.out.println(entry);
- }
| 方法 | 说明 |
| boolean add(E e) | 添加元素,但重复元素不会被添加成功 |
| void clear() | 清空集合 |
| boolean contains(Object 0) | 判断o是否在集合中 |
| Iterator | 返回迭代器 |
| boolean remove(Object o) . | 删除集合中的0 |
| int size() | 返回set中元素的个数 |
| boolean isEmpty() | 检测set是否为空,空返回true, 否则返回false |
| Object[] toArray() | 将set中的元素转换为数组返回 |
| boolean containsAll(Collection> c) | 集合c中的元素是否在set中全部存在,是返回true, 否则返回true |
| boolean addAl(Collection extends | 将集合c中的元素添加到set中,可以达到去重的效果 |
- public static void main(String[] args) {
- HashSet
set = new HashSet<>(); - set.add(1);
- set.add(2);
- set.add(3);
- set.add(4);
- Iterator
iterator = set.iterator(); - while (iterator.hasNext()){
- System.out.println(iterator.next());
- }
- }
- public int func(int[] nums){
- Set
hashSet = new HashSet<>(); - for(int i=0;i
- if(hashSet.contains(nums[i])){
- return nums[i];
- }
- hashSet.add(nums[i]);
- }
- return -1;
- }
3.2 去除给定的数据中重复的数据
- public void func2(int[] nums){
- Set
hashSet = new HashSet<>(); - for(int i=0;i
- hashSet.add(nums[i]);
- }
- System.out.println(hashSet);
- }
3.3 统计重复的数据出现的次数
- public void fun3(int[] nums){
- Map
hashmap = new HashMap<>(); -
- for (int i = 0; i < nums.length; i++) {
- if(hashmap.containsKey(nums[i])){
- // 如果在hashmap中存在,就更新次数
- int count = hashmap.get(nums[i]);
- count = count+1;
- hashmap.put(nums[i],count);
- }else{
- // 不存在,就放入hashmap中,值是1
- hashmap.put(nums[i],1);
- }
- }
- Set
> entrySet = hashmap.entrySet(); - // 遍历hashmap
- for(Map.Entry
entry : entrySet){ - int a = entry.getKey();
- System.out.println(entry);
- }
- }
3.4 只出现一次的数据
- class Solution {
- public int singleNumber(int[] nums) {
- Set
set = new HashSet<>(); - for(int i=0;i
- // 如果已经出现,就从set中移除该元素
- if(set.contains(nums[i])){
- set.remove(nums[i]);
- }else{
- set.add(nums[i]);
- }
- }
- // set剩下的元素就是只出现一次的元素
- Iterator
it = set.iterator(); -
- while(it.hasNext()){
- return it.next();
- }
- return -1;
- }
- }
3.5 复制带随机指针的链表
- class Solution {
- public Node copyRandomList(Node head) {
- Map
map = new HashMap<>(); - Node cur = head;
- while(cur != null){
- // 将新节点和旧节点的对应关系存储到map中
- Node node = new Node(cur.val);
- map.put(cur,node);
- cur = cur.next;
- }
- cur = head;
- while(cur != null){
- // 得到新节点
- // 从map中得到新节点和旧节点 根据旧节点的next和random属性修改新节点的属性
- Node node = map.get(cur);
- node.next = map.get(cur.next);
- node.random = map.get(cur.random);
- cur = cur.next;
- }
- return map.get(head);
- }
- }
3.6 宝石与石头
- class Solution {
- public int numJewelsInStones(String jewels, String stones) {
- Set
set = new HashSet<>(); - for(int i =0;i
- char ch = jewels.charAt(i);
- // 对jewels中的字符进行去重
- if(!set.contains(ch)){
- set.add(ch);
- }
- }
- int sum = 0;
- for(int i =0;i
- char ch = stones.charAt(i);
- // stones中的字符如果在set中出现,sum++
- if(set.contains(ch)){
- sum++;
- }
- }
- return sum;
- }
- }
3.7 坏键盘打字
- import java.util.*;
- public class Main{
- public static void func(String str1,String str2){
- // 先将字符串转成大写的字符数组
- char[] ch1 = str1.toUpperCase().toCharArray();
- char[] ch2 = str2.toUpperCase().toCharArray();
- // ch1是正常的输入
- // ch2是实际的输入
- Set
set1 = new HashSet<>(); - for(char ch:ch2){
- // 统计ch2中出现的不重复的字符 ,这些字符都是好的按键
- if(!set1.contains(ch)){
- set1.add(ch);
- }
- }
- Set
set2 = new HashSet<>(); - for(char ch:ch1){
- // 如果字符没有在好的按键集合里,就加到set2中,加之前要判断是否已经存在,如果存在,就不用再add了
- if(!set1.contains(ch)){
- if(!set2.contains(ch)){
- set2.add(ch);
- System.out.print(ch);
-
- }
- }
- }
- }
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- String str1 = scanner.nextLine();
- String str2 = scanner.nextLine();
- func(str1,str2);
- }
- }
3.8 前K个高频单词
- 先统计每个单词的出现次数
- 建立一个小根堆,重写compare方法。存在两种情况,第一种是两个词的出现次数一样,比较单词的首字母的AscII码值。第二种情况是两个词的出现次数不一样
- topK思想。
- class Solution {
- public List
topKFrequent(String[] words, int k) { - int c = k;
- HashMap
hashmap = new HashMap<>(); - // 统计每个单词的出现次数
- for(String str:words){
- if(!hashmap.containsKey(str)){
- hashmap.put(str,1);
- }else{
- int count = hashmap.get(str);
- hashmap.put(str,count+1);
- }
- }
- // 建立一个小根堆,重写compare方法
- PriorityQueue
> queue = new PriorityQueue<>(new Comparator>(){ - @Override
- public int compare(Map.Entry
o1,Map.Entry o2) { - if(o1.getValue().equals(o2.getValue())){
- return o2.getKey().compareTo(o1.getKey());
- }
- return o1.getValue()-o2.getValue();
- }
- });
- // topK的思想
- for(Map.Entry
entry : hashmap.entrySet()){ - if(k >0){
- queue.offer(entry);
- k--;
- }else{
- Map.Entry
peek = queue.peek(); - int tmp = peek.getValue().compareTo(entry.getValue());
- if(tmp < 0){
- queue.poll();
- queue.offer(entry);
- }else if(tmp == 0){
- if(peek.getKey().compareTo(entry.getKey()) >0 ){
- queue.poll();
- queue.offer(entry);
- }
- }
- }
- }
- List
result = new ArrayList<>(); - for(int i=0;i
- result.add(queue.poll().getKey());
- }
- Collections.reverse(result);
- return result;
- }
- }
-
相关阅读:
LeetCode-高频 SQL 50 题:连接 篇
2022年最新山西机动车签字授权人模拟考试及答案
【Java小知识点】类加载器的区别
算法篇 滑动窗口 leetcode 长度最小的子数组
常识——虚拟机安装centos7与联网
区块链智能合约开发
Spring之ioc
物联网智慧大屏
串口控制小车电机转动+蓝牙长按控制
Go语言基础之包
-
原文地址:https://blog.csdn.net/weixin_44258092/article/details/126453640