java HashMap通过value反查key的代码示例
时间:2022-06-07 08:50:55
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MapValueGetKey {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put(1,"A");
map.put(2,"A");
map.put(3,"A");
map.put(4,"A");
map.put(5,"A");
String value = "A";
ArrayList arr = valueGetKey(map, value);
if(!arr.isEmpty()) {
for(int i=0; i<arr.size(); i++) {
System.out.println(arr.get(i));
}
}
}
private static ArrayList valueGetKey(Map map,String value) {
Set set = map.entrySet();
ArrayList arr = new ArrayList<>();
Iterator it = set.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
if(entry.getValue().equals(value)) {
int s = (int)entry.getKey();
arr.add(s);
}
}
return arr;
}
}
结果如下:
1
2
3
4
5
标签:java,HashMap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决
2022-09-19 19:35:17
java中类与对象的使用详情
2023-09-27 17:10:43
Kotlin基础教程之数据类型
2023-12-06 15:23:22
![](https://img.aspxhome.com/file/2023/5/65745_0s.png)
详解Eclipse提交项目到GitHub以及解决代码冲突
2022-05-15 09:04:52
![](https://img.aspxhome.com/file/2023/6/78966_0s.png)
浅析Java中comparator接口与Comparable接口的区别
2023-11-01 20:31:14
WinForm绘制圆角的方法
2023-06-21 14:23:51
Java+element实现excel的导入和导出
2022-07-31 12:45:53
![](https://img.aspxhome.com/file/2023/4/62464_0s.png)
Java实现天天酷跑小游戏完整代码(附源码)
2021-08-03 09:43:43
![](https://img.aspxhome.com/file/2023/3/60833_0s.png)
Android简单实现文件下载
2023-08-28 06:33:17
Java 多线程学习详细总结
2023-11-18 22:55:24
![](https://img.aspxhome.com/file/2023/9/119109_0s.jpg)
Android EditText实现关键词批量搜索示例
2022-12-09 06:20:21
C# 两种方式反编译修改源码(dnspy,ildasm & ilasm)
2022-04-29 18:48:56
![](https://img.aspxhome.com/file/2023/2/104312_0s.png)
JavaWeb实现文件上传下载功能实例解析
2023-10-07 17:41:39
![](https://img.aspxhome.com/file/2023/9/62629_0s.png)
Android实现支付宝支付密码输入界面
2023-06-20 14:57:15
![](https://img.aspxhome.com/file/2023/8/83508_0s.gif)
JVM Client和Server端有什么区别
2023-08-05 22:49:53
![](https://img.aspxhome.com/file/2023/5/64385_0s.png)
java中文转全拼工具类分享
2022-02-07 09:50:57
Mybatis自动创建表和更新表结构
2022-11-17 19:43:00
利用Distinct()内置方法对List集合的去重问题详解
2023-01-31 00:45:30
Flink开发IDEA环境搭建与测试的方法
2023-02-21 21:20:46
![](https://img.aspxhome.com/file/2023/0/84270_0s.jpg)
C#实现类型的比较示例详解
2022-02-18 18:17:44
![](https://img.aspxhome.com/file/2023/1/126111_0s.png)