Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将Hashmap中的值转换为列表<;字符串>;_Java_Hashmap - Fatal编程技术网

Java 如何将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<

我有一个Hashmap类型

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());