英文:
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->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->out.print(a.getValue()+"\n"));
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 -> print(a.values()));
private void print(Collection<Double> values) {
System.out.println(values.stream()
.map(String::valueOf)
.collect(Collectors.joining(",")));
}
答案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<String, List<Double>> map = ...
String result = map.values().stream()
.map(s -> s.stream()
.map(String::valueOf)
.collect(Collectors.joining(", ")))
.collect(Collectors.joining("\n"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论