遍历地图列表以仅写入其内容

huangapple go评论64阅读模式
英文:

Iterating list of map for writing only its content

问题

以下为您翻译的内容:

每个参与者都会为其他人投票一些数值,我想要按照代理的名称来记录这些信息。

通过以下代码,

cj.makingDefault().stream()
       .forEach(a -> out.print(a.entrySet()));

我得到了一个打印出来的Map列表,内容如下:

[ari=[0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03], 
 hyo=[0.31, 0.31, 0.317, 0.31..., ji=[...], yoo=[...], mi=[...]..]

我该如何只打印代理的数值,用换行符分隔开呢?

期望的结果应该是这样的:

0.3, 0.3, 0.3, ...0.3
0.31, 0.31, 0.31, ...0.31,
...

如果有7个参与者,那么会是一个7x7的矩阵。

非常感谢您提前的帮助~

编辑)
作为一个临时解决方案,我修改了“cj.makingDefault()”函数,使其仅返回Map<String, List>,并将代码更改为以下内容。

  cj.makingDefault().entrySet()
    .stream().forEach(a -> out.print(a.getValue() + "\n"));

然后我得到了如下结果:

[0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865]
[0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144]
[0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273]
[0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558]
[0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038]
[0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696]
[0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337]

那么,我该如何消除这些方括号呢?这样就完美了。

英文:

Each participant is voting some numerical values to each others, and I want to write this information by agent's name.

By the following code,

cj.makingDefault().stream()
       .forEach(a-&gt;out.print(a.entrySet()));

I get a List of Map printed which is,

[ari=[0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03], 
 hyo=[0.31, 0.31, 0.317, 0.31..., ji=[...], yoo=[...], mi=[...]..]

How can I print out only the numberic value by agents, separated by newline?

Desired results would be like

0.3, 0.3, 0.3, ...0.3
0.31, 0.31, 0.31, ...0.31,
...

If there would be 7 participants, 7 by 7 matrix.

Thank you in advance for your help~

edit)
As a temporary solution, I modified "cj.makingDefault()" function to return only Map<String, List<Double>> and changed code to this.

  cj.makingDefault().entrySet()
    .stream().forEach(a-&gt;out.print(a.getValue()+&quot;\n&quot;));

and I got

[0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865]
[0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144]
[0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273]
[0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558]
[0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038]
[0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696]
[0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337, 0.39173806385104337]

Then how can I eliminate the suqare braket of this..? it would be perfect..

答案1

得分: 1

你所看到的是集合的默认toString实现,其中包括方括号。

你需要自己打印列表中的元素。

cj.makingDefault().stream()
    .forEach(a -> print(a.values()));

private void print(Collection<Double> values) {
    System.out.println(values.stream()
            .map(String::valueOf)
            .collect(Collectors.joining(",")));
}
英文:

What you are seeing is the default toString implementation of a collection which will include the square brackets.

You have to print the elements in the list yourself.

cj.makingDefault().stream()
   .forEach(a -&gt; print(a.values()));

 private void print(Collection&lt;Double&gt; values) {
    System.out.println(values.stream()
            .map(String::valueOf)
            .collect(Collectors.joining(&quot;,&quot;)));
}

答案2

得分: 1

你必须首先迭代地图中的值,这些值是列表,然后迭代列表中的项目,以覆盖标准的toString输出。

Map<String, List<Double>> map = ...

String result = map.values().stream()
        .map(s -> s.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(", ")))
        .collect(Collectors.joining("\n"));
英文:

You must first iterate the map values, that are lists, and then the items in those lists to override the standard toString output.

Map&lt;String, List&lt;Double&gt;&gt; map = ...

String result = map.values().stream()
        .map(s -&gt; s.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(&quot;, &quot;)))
        .collect(Collectors.joining(&quot;\n&quot;));

huangapple
  • 本文由 发表于 2020年9月14日 14:12:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63878939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定