Java 如何将Hashmap中的值转换为列表<;字符串>;
我有一个Hashmap类型Java 如何将Hashmap中的值转换为列表<;字符串>;,java,hashmap,Java,Hashmap,我有一个Hashmap类型 Map<String, List<String>> adminErrorMap = new HashMap<>(); Map adminErrorMap=newHashMap(); 我希望能够遍历整个hashmap,并将所有值放到一个列表中。钥匙是不相关的 我做过这样的事情: List<String> adminValues = new ArrayList<>(); for (Map.Entry<
Map<String, List<String>> adminErrorMap = new HashMap<>();
Map adminErrorMap=newHashMap();
我希望能够遍历整个hashmap,并将所有值放到一个列表中。钥匙是不相关的
我做过这样的事情:
List<String> adminValues = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : adminErrorMap.entrySet()) {
adminValues.add(entry.getValue().toString());
}
System.out.println(adminValues);
List adminValues=new ArrayList();
对于(Map.Entry:adminErrorMap.entrySet()){
add(entry.getValue().toString());
}
System.out.println(AdminValue);
输出
[{description=文件路径,value=PurchaseOrder.plsql},
{description=Component,value=PURCH},{description=Commit Date,
value=Thu May 05 00:32:01 IST 2016}],{description=文件路径,
value=CustomerOrder.plsql},{description=Component,value=COMP},
{description=提交日期,value=Thu June 05 00:32:01 IST 2016}]]
如您所见,主[]中有[]
如何将所有值放在一个[]内。如下图所示
或者有没有其他方法来实现这一点
[{description=文件路径,value=PurchaseOrder.plsql},
{description=Component,value=PURCH},{description=Commit Date,
value=Thu May 05 00:32:01 IST 2016},{description=文件路径,
value=CustomerOrder.plsql},{description=Component,value=COMP},
{description=提交日期,value=Thu June 05 00:32:01 IST 2016}]
使用addAll
而不是add
,以便将所有列表
的所有字符串
添加到单个列表
:
for(列表值:adminErrorMap.values())
{
adminValues.addAll(value);
}
在Java8中,您可以使用functional来实现:
adminErrorMap.values().forEach(adminValues::addAll);
您的声明类型中似乎存在一个问题:
Map<String, List<String>> adminErrorMap = new HashMap<>();
List<String> adminValues = new ArrayList<>();
基于adminErrorMap
声明的entry.getValue()
,不会返回类型为String
的对象。相反,它返回另一个列表
,这完全不是预期的结果。您只是在列表
对象上调用toString()
,这可能不是您真正想要做的
由于不能将列表
类型化对象放入需要字符串
类型化对象的容器中,因此应改用entry.getKey()
:
adminValues.add(entry.getKey());
如果我正确地回忆起我的Java,以下内容可能会更有用:
for (String key : adminErrorMap.keySet())
adminValues.add(key);
编辑此评论:
我只需要所有键的值
因为每个键都可以有多个与之相关联的值,因为它们是类型<代码>列表< /> >,那么你应该考虑更近的东西:
for(列表值:adminErrorMap.values())
adminValues.addAll(值);
我们为什么不这样做
Map<String, List<String>> mapKeyList = new HashMap<String, List<String>>();
List<String> finalList = new ArrayList<String>();
Iterator it = mapKeyList.entrySet().iterator();
while(it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
List<String> strList = (List<String>) pair.getValue();
Iterator<String> strIt= strList.iterator();
while(strIt.hasNext()){
String str = strIt.next();
finalList.add(str);
}
}
Map-mapKeyList=newhashmap();
List finalList=new ArrayList();
迭代器it=mapKeyList.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
List strList=(List)pair.getValue();
迭代器strIt=strList.Iterator();
while(strIt.hasNext()){
String str=strIt.next();
finalList.add(str);
}
}
我知道复杂性将是n*n,但我们得到了预期的结果。您只需将集合展平即可。在Java8中:
final Map<String, List<String>> adminErrorMap = ImmutableMap.of(
"a", Lists.newArrayList("first", "second"),
"b", Lists.newArrayList("third")
);
final List<String> results = adminErrorMap.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
results.forEach(System.out::println);
一个简单的方法是:
它不应该是adminValues.add(entry.getKey())代码>或类似的东西?您不能将列表
放入一个容器,该容器需要一个字符串
,如果我需要这些键的话。但是我只需要所有键的值,那么为什么您将adminValues
声明为List
类型而不是List
?啊哈!我看你说得有道理!谢谢:)我会把我的评论作为一个答案,如果它对你有用的话,那么我认为这个代码是不正确的。这个答案的投票人似乎没有注意到,adminValues
是String
对象的容器,而不是List
的容器,即声明不是ListadminValues
@ray,这是正确的。addAll接受一个集合并将该集合的所有元素添加到列表中。因此,您可以调用adminValues.addAll(value)
将值
列表的所有字符串添加到输出列表
。可能是因为我起晚了,但是adminValues
希望添加字符串
,但是值
变量不是字符串
,而是列表
,这让我觉得很奇怪。@ray注意到我使用的是addAll,而不是add。addAll需要一个元素类型与要向其中添加元素的列表相同的集合。因此,您可以调用addAll
获取List
并将另一个List
传递给它@ray这是您的代码太长了。从Java 1.5开始,迭代器类只在非常特殊的情况下使用,而这不是其中之一。对于实际需要的代码来说,这段代码不必要地长/冗长,其时间复杂度也不是很高。但是如果我们得到了预期的输出,那么我认为可以使用这段代码。。
for (List<String> values : adminErrorMap.values())
adminValues.addAll(values);
Map<String, List<String>> mapKeyList = new HashMap<String, List<String>>();
List<String> finalList = new ArrayList<String>();
Iterator it = mapKeyList.entrySet().iterator();
while(it.hasNext()){
Map.Entry pair = (Map.Entry)it.next();
List<String> strList = (List<String>) pair.getValue();
Iterator<String> strIt= strList.iterator();
while(strIt.hasNext()){
String str = strIt.next();
finalList.add(str);
}
}
final Map<String, List<String>> adminErrorMap = ImmutableMap.of(
"a", Lists.newArrayList("first", "second"),
"b", Lists.newArrayList("third")
);
final List<String> results = adminErrorMap.values().stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
results.forEach(System.out::println);
first
second
third
new ArrayList<>(map.values());
map.values().stream().collect(Collectors.toList());