[英]How to retrieve an element inside an ArrayList value in a HashMap?
I'm new to Java and I'm struggling to figure out something trivial.我是 Java 的新手,我正在努力找出一些微不足道的东西。
I have a HashMap
with keys of type String
and values of type ArrayList <String>
, and it returns something like this:我有一个HashMap
,其键类型为String
,值类型为ArrayList <String>
,它返回如下内容:
dict = {Color=[Blue, Purple]}
I'm struggling to figure out how I can grab a specific element from the array in the value of dict.我正在努力弄清楚如何从 dict 的值中从数组中获取特定元素。 Like if I got dict.value[0]
, it would return the string 'Blue'.就像我得到dict.value[0]
一样,它会返回字符串“Blue”。
I've tried doing:我试过做:
Object values = dict.values().toArray[0];
and I assumed that since I changed it to an array, I could do something like values.get(0)
to get the first element, but that doesn't work because values is type Object
.我假设因为我将它更改为一个数组,所以我可以执行values.get(0)
之类的操作来获取第一个元素,但这不起作用,因为 values 是类型Object
。 Any ideas on how to resolve this?关于如何解决这个问题的任何想法?
if your HashMap
is like:如果您的HashMap
是这样的:
Map<String, List<String>> myColorsMap = new HashMap<>();
And after populating the map is like:填充 map 后就像:
{Red=[firstRed, secondRed, thirdRed], Blue=[firstBlue, secondBlue, thirdBlue]}
then you can retrieve Blue 's key value (a List
) like:然后您可以检索Blue的键值(一个List
),例如:
String blueFirstElement = myColorsMap.get("Blue").get(0); // --> will give first element of List<String> stored against Blue key.
To get collection of all keys in your map (or "dictionary"):要收集 map(或“字典”)中的所有键:
myColorsMap.keySet();
will give:会给:
[Red, Blue]
When you do:当你这样做时:
Object values = dict.values().toArray[0];
First, this is wrong.首先,这是错误的。 What you were trying to do is this:你试图做的是:
Object values = dict.values().toArray();
Which returns Object[]
and it is ambiguous.它返回Object[]
并且它是模棱两可的。 No need to do that.没必要那样做。 Java's Map
interface and HashMap
implementation have a lot of utility methods for you to iterate and retrieve your values. Java 的Map
接口和HashMap
实现有很多实用方法供您迭代和检索值。
First, use .get()
on map to get the ArrayList using key首先,在 map 上使用.get()
使用密钥获取 ArrayList
List<String> colors = dict.get("Color");
Then use .get()
on list to get the element using index然后在列表上使用.get()
来获取使用索引的元素
String color = colors.get(0);
You can do this in one line this way您可以通过这种方式在一行中执行此操作
String color = dict.get("Color").get(0);
You are trying with dict.values().toArray[0]
which is wrong syntax thats the problem.您正在尝试使用dict.values().toArray[0]
这是错误的语法,这就是问题所在。 dict.values()
will return Collection<List<String>>
, you can get List of values this way dict.values()
将返回Collection<List<String>>
,您可以通过这种方式获取值列表
List<List<String>> listOfValues = new ArrayList<>(colorsMap.values());
Then you can access each value by index using .get()
然后您可以使用.get()
通过索引访问每个值
List<String> colors = listOfValues.get(0);
Note : HashMap don't store any order.注意:HashMap 不存储任何订单。
Cast to type of java.util.List
, and then you can get value through index.转换为java.util.List
类型,然后可以通过索引获取值。
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Map<String, List<String>> map = new HashMap<>(2);
map.put("color", Arrays.asList("blue", "yellow", "green", "white"));
map.put("size", Arrays.asList("small", "medium", "large"));
List<String> o = (List<String>) map.values().toArray()[0];
System.out.println(o.get(0)); //output is 'blue'
List<String> s = (List<String>) map.values().toArray()[1];
System.out.println(s.get(1)); //output is 'medium'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.