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

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

Iterating list of map for writing only its content

问题

以下为您翻译的内容:

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

通过以下代码,

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

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

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

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

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

  1. 0.3, 0.3, 0.3, ...0.3
  2. 0.31, 0.31, 0.31, ...0.31,
  3. ...

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

非常感谢您提前的帮助~

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

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

然后我得到了如下结果:

  1. [0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865]
  2. [0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144]
  3. [0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273]
  4. [0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558]
  5. [0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038]
  6. [0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696]
  7. [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,

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

I get a List of Map printed which is,

  1. [ari=[0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03],
  2. 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

  1. 0.3, 0.3, 0.3, ...0.3
  2. 0.31, 0.31, 0.31, ...0.31,
  3. ...

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.

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

and I got

  1. [0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865, 0.0056009042654552865]
  2. [0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144, 0.31583809304895144]
  3. [0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273, 0.9924090841607273]
  4. [0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558, 0.8093898651356558]
  5. [0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038, 0.015200181208559038]
  6. [0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696, 0.03947825506938696]
  7. [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实现,其中包括方括号。

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

  1. cj.makingDefault().stream()
  2. .forEach(a -> print(a.values()));
  3. private void print(Collection<Double> values) {
  4. System.out.println(values.stream()
  5. .map(String::valueOf)
  6. .collect(Collectors.joining(",")));
  7. }
英文:

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.

  1. cj.makingDefault().stream()
  2. .forEach(a -&gt; print(a.values()));
  3. private void print(Collection&lt;Double&gt; values) {
  4. System.out.println(values.stream()
  5. .map(String::valueOf)
  6. .collect(Collectors.joining(&quot;,&quot;)));
  7. }

答案2

得分: 1

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

  1. Map<String, List<Double>> map = ...
  2. String result = map.values().stream()
  3. .map(s -> s.stream()
  4. .map(String::valueOf)
  5. .collect(Collectors.joining(", ")))
  6. .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.

  1. Map&lt;String, List&lt;Double&gt;&gt; map = ...
  2. String result = map.values().stream()
  3. .map(s -&gt; s.stream()
  4. .map(String::valueOf)
  5. .collect(Collectors.joining(&quot;, &quot;)))
  6. .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:

确定