Java HashMap:一个键,多个值
如何获取此地图中第一个关键点的第三个值?这是可能的吗?标准Java HashMap不能为每个键存储多个值,您添加的任何新条目都将覆盖以前的条目。有库可以做到这一点,但最简单的普通Java方法是创建Java HashMap:一个键,多个值,java,Java,如何获取此地图中第一个关键点的第三个值?这是可能的吗?标准Java HashMap不能为每个键存储多个值,您添加的任何新条目都将覆盖以前的条目。有库可以做到这一点,但最简单的普通Java方法是创建列表的映射,如下所示: Map<Object,ArrayList<Object>> multiMap = new HashMap<>(); Map multiMap=newhashmap(); 尝试使用集合存储键的值: Map<Key, Collection&
列表的映射,如下所示:
Map<Object,ArrayList<Object>> multiMap = new HashMap<>();
Map multiMap=newhashmap();
尝试使用集合存储键的值:
Map<Key, Collection<Value>>
Map
你必须自己维护价值清单你有这样的东西吗
HashMap<String, ArrayList<String>>
HashMap
如果是这样,您可以通过ArrayList.get(i)遍历您的ArrayList并获取您喜欢的项目。听起来您正在寻找一个新的项目。具有各种实现,通常通过类创建
我建议,使用该实现可能比滚动自己的实现、确定API应该是什么样子、在添加值时仔细检查现有列表等更简单。如果您的情况特别厌恶第三方库,那么这样做可能是值得的,但除此之外,Guava是一个极好的库,它可能也会帮助您编写其他代码:)例如:
Map<Object,Pair<Integer,String>> multiMap = new HashMap<Object,Pair<Integer,String>>();
想到一个有两个键的映射,我马上就不得不使用一个用户定义的键,这可能是一个类。以下是关键类:
public class MapKey {
private Object key1;
private Object key2;
public Object getKey1() {
return key1;
}
public void setKey1(Object key1) {
this.key1 = key1;
}
public Object getKey2() {
return key2;
}
public void setKey2(Object key2) {
this.key2 = key2;
}
}
// Create first map entry with key <A,B>.
MapKey mapKey1 = new MapKey();
mapKey1.setKey1("A");
mapKey1.setKey2("B");
公共类映射键{
私有对象密钥1;
私有对象密钥2;
公共对象getKey1(){
返回键1;
}
公共无效设置键1(对象键1){
此参数为0.key1=key1;
}
公共对象getKey2(){
返回键2;
}
公共无效设置键2(对象键2){
这个.key2=key2;
}
}
//使用键创建第一个地图条目。
MapKey mapKey1=新的MapKey();
mapKey1.setKey1(“A”);
mapKey1.setKey2(“B”);
我在随机搜索中找到了这个博客,我认为这将有助于做到这一点:
公共类MutliMapTest{
公共静态void main(字符串…参数){
Multimap myMultimap=ArrayListMultimap.create();
//添加一些键/值
myMultimap.put(“水果”、“班纳纳”);
myMultimap.put(“水果”、“苹果”);
myMultimap.put(“水果”、“梨”);
myMultimap.put(“蔬菜”、“胡萝卜”);
//得到尺寸
int size=myMultimap.size();
System.out.println(大小);//4
//获取价值
收集水果=myMultimap.get(“水果”);
System.out.println(水果);//[香蕉、苹果、梨]
收集蔬菜=myMultimap.get(“蔬菜”);
System.out.println(蔬菜);//[胡萝卜]
//在整个Mutlimap上迭代
for(字符串值:myMultimap.values()){
系统输出打印项次(值);
}
//删除单个值
myMultimap.移除(“水果”、“梨”);
System.out.println(myMultimap.get(“水果”);/[Bannana,Pear]
//删除键的所有值
myMultimap.removeAll(“水果”);
System.out.println(myMultimap.get(“水果”);//[](空集合!)
}
}
这是我在一个类似问题中发现的
Map hm=newhashmap();
列表值=新的ArrayList();
价值。增加(“价值1”);
价值。增加(“价值2”);
hm.put(“键1”,值);
//获取arraylist
System.out.println(hm.get(“key1”);
结果:[值1,值2]HashMap–使用列表的单键和多个值
Map<String, List<String>> map = new HashMap<String, List<String>>();
// create list one and store values
List<String> One = new ArrayList<String>();
One.add("Apple");
One.add("Aeroplane");
// create list two and store values
List<String> Two = new ArrayList<String>();
Two.add("Bat");
Two.add("Banana");
// put values into map
map.put("A", One);
map.put("B", Two);
map.put("C", Three);
Map Map=newhashmap();
//创建列表1并存储值
列表一=新的ArrayList();
一、添加(“苹果”);
一.加入(“飞机”);
//创建列表2并存储值
列表二=新的ArrayList();
二、添加(“Bat”);
二.加入(“香蕉”);
//将值放入地图
地图放置(“A”,一);
地图放置(“B”,二);
地图放置(“C”,三);
您可以这样做(根据需要添加访问修饰符):
这样,键的每个值都应该有一个唯一的标识符。如果键是已知的,也会为抓取提供O(1)。编写一个新类,该类保存您需要的所有值,并使用新类的对象作为HashMap中的值
HashMap<String, MyObject>
class MyObject {
public String value1;
public int value2;
public List<String> value3;
}
HashMap
类MyObject{
公共字符串值1;
公共价值2;
上市价值3;
}
以下是如何将hashmap提取到数组中的代码,其中包含arraylist
Map<String, List<String>> country_hashmap = new HashMap<String, List<String>>();
//Creating two lists and inserting some data in it
List<String> list_1 = new ArrayList<String>();
list_1.add("16873538.webp");
list_1.add("16873539.webp");
List<String> list_2 = new ArrayList<String>();
list_2.add("16873540.webp");
list_2.add("16873541.webp");
//Inserting both the lists and key to the Map
country_hashmap.put("Malaysia", list_1);
country_hashmap.put("Japanese", list_2);
for(Map.Entry<String, List<String>> hashmap_data : country_hashmap.entrySet()){
String key = hashmap_data.getKey(); // contains the keys
List<String> val = hashmap_data.getValue(); // contains arraylists
// print all the key and values in the hashmap
System.out.println(key + ": " +val);
// using interator to get the specific values arraylists
Iterator<String> itr = val.iterator();
int i = 0;
String[] data = new String[val.size()];
while (itr.hasNext()){
String array = itr.next();
data[i] = array;
System.out.println(data[i]); // GET THE VALUE
i++;
}
}
Map country_hashmap=new hashmap();
//创建两个列表并在其中插入一些数据
List_1=新的ArrayList();
列表1.添加(“16873538.webp”);
列表1.添加(“16873539.webp”);
List_2=新的ArrayList();
列表2.添加(“16873540.webp”);
列表2.添加(“16873541.webp”);
//将列表和键插入地图
国家(马来西亚,清单1);
国家(日本),列表2);
对于(Map.Entry hashmap\u数据:country\u hashmap.entrySet()){
String key=hashmap_data.getKey();//包含键
List val=hashmap_data.getValue();//包含数组列表
//打印hashmap中的所有键和值
System.out.println(键+“:”+val);
//使用interator获取ArrayList的特定值
迭代器itr=val.Iterator();
int i=0;
字符串[]数据=新字符串[val.size()];
while(itr.hasNext()){
字符串数组=itr.next();
数据[i]=数组;
System.out.println(data[i]);//获取值
i++;
}
}
Apache Commons集合类是解决方案
MultiMap multiMapDemo = new MultiValueMap();
multiMapDemo .put("fruit", "Mango");
multiMapDemo .put("fruit", "Orange");
multiMapDemo.put("fruit", "Blueberry");
System.out.println(multiMap.get("fruit"));
// Mango Orange Blueberry
Maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --
>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
除了这里的所有答案之外,我有一个解决方案,我使用它,如果您知道要添加到键中的多个值的长度,它将非常有用。
在我的例子中,它是2,所以我选择了这个而不是列表。
HashMap multimap=newhashmap();
multimap.put(“my_key”,新字符串[]{“my_value1”,“my_value2”});
我可以使用HashMap::get的成员函数获取键的第一个值,通过第三个?很遗憾,我找不到任何代码。您找不到自己的代码吗?这就是他说的。听起来你可能有点紧张
Map<String,String> componentMap = new HashMap<String,String>();
componentMap.put("foo","bar");
componentMap.put("secondFoo","secondBar");
complexMap.put("superFoo",componentMap);
{superFoo={secondFoo=secondBar, foo=bar}}
HashMap<String, MyObject>
class MyObject {
public String value1;
public int value2;
public List<String> value3;
}
Map<String, List<String>> country_hashmap = new HashMap<String, List<String>>();
//Creating two lists and inserting some data in it
List<String> list_1 = new ArrayList<String>();
list_1.add("16873538.webp");
list_1.add("16873539.webp");
List<String> list_2 = new ArrayList<String>();
list_2.add("16873540.webp");
list_2.add("16873541.webp");
//Inserting both the lists and key to the Map
country_hashmap.put("Malaysia", list_1);
country_hashmap.put("Japanese", list_2);
for(Map.Entry<String, List<String>> hashmap_data : country_hashmap.entrySet()){
String key = hashmap_data.getKey(); // contains the keys
List<String> val = hashmap_data.getValue(); // contains arraylists
// print all the key and values in the hashmap
System.out.println(key + ": " +val);
// using interator to get the specific values arraylists
Iterator<String> itr = val.iterator();
int i = 0;
String[] data = new String[val.size()];
while (itr.hasNext()){
String array = itr.next();
data[i] = array;
System.out.println(data[i]); // GET THE VALUE
i++;
}
}
MultiMap multiMapDemo = new MultiValueMap();
multiMapDemo .put("fruit", "Mango");
multiMapDemo .put("fruit", "Orange");
multiMapDemo.put("fruit", "Blueberry");
System.out.println(multiMap.get("fruit"));
// Mango Orange Blueberry
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 --
>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
HashMap<String, String[]> multimap= new HashMap<>();
multimap.put("my_key", new String[]{"my_value1", "my_value2"});