如何将字符列表和整数列表设置到一个列表中?

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

How to set list of chars and list of integers into one list?

问题

private List combineLists() {
Set mapKeyToList = output().keySet();
Collection values = output().values();

List<Object> combinedList = new ArrayList<>();

for (Character key : mapKeyToList) {
    combinedList.add(key + " - " + values.iterator().next());
    values.remove(values.iterator().next());
}

return combinedList;

}

英文:

How to set list of chars and list of integers into one list? I have two lists which I took from hash map and now I have to format it into pretty view. After formatting I have two lists one of integers and other one of characters. Is it possible to put this values into one array list

 private List&lt;Character&gt; convertToList(){
    Set&lt;Character&gt; mapKeyToList = output().keySet();
    List&lt;Character&gt; keys = new ArrayList&lt;&gt;(mapKeyToList);
    return keys;
}
private List&lt;Integer&gt; convertInts(){
    Collection&lt;Integer&gt; values = output().values();
    List&lt;Integer&gt; quantity = new ArrayList&lt;&gt;(values);
    return quantity;
}

Example of output :

&quot;char&quot; - int
&quot;char&quot; - int
&quot;char&quot; - int

答案1

得分: 3

你可以从你的映射中获取 entrySet 并获得一个新的 ArrayList。

List<Map.Entry<Character, Integer>> res = new ArrayList<>(output().entrySet());

你可以使用 .getKey().getValue() 来获取键和值。

for (Map.Entry<Character, Integer> entry : res) {
  Character c = entry.getKey();
  Integer i = entry.getValue();
  // 格式化
}
在这里你也可以直接在循环中使用 `resultMap.entrySet()` 而不是 `res`。

<details>
<summary>英文:</summary>

You can get the `entrySet` from your map and get in new ArrayList

    List&lt;Entry&lt;Character, Integer&gt;&gt; res = new ArrayList&lt;&gt;(output().entrySet());

You can get key and value using `.getKey()` and `.getValue()`

    for (Entry&lt;Character, Integer&gt; entry : res) {
      Character c = entry.getKey();
      Integer i = entry.getValue();
      // formatting 
    }
Here you can directly use `resultMap.entrySet()` instead of `res` in loop also.

</details>



# 答案2
**得分**: 0

假设这两个列表的大小相同如果它们是从同一个映射创建的话),您可以使用 for 循环从每个列表中提取元素并填充新列表

```java
List<String> newList = new ArrayList<>();

for (int i = 0; i < ints.size(); i++) {
    newList.add(String.format("%s - %d", chars.get(i), ints.get(i)));
}

当然,如果您使用的是 Java 8 或更高版本,您还可以使用 Streams API 遍历映射并执行此操作。

List<String> result = values.entrySet().stream()
        .map(x -> String.format("%s - %d", x.getKey(), x.getValue()))
        .collect(Collectors.toList());
英文:

Assuming that both lists are the same size (which they should be, if they are created from the same map), you can just use a for loop to pull elements from each list and populate the new list.

List&lt;String&gt; newList = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; ints.size(); i++) {
newList.add(String.format(&quot;%s - %d&quot;, chars.get(i), ints.get(i)));
}

Of course, you could also loop over the map and do this with the Streams API if you are using Java 8 or later.

    List&lt;String&gt; result = values.entrySet().stream()
.map(x -&gt; String.format(&quot;%s - %d&quot;, x.getKey(), x.getValue()))
.collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年10月2日 16:52:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64168722.html
匿名

发表评论

匿名网友

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

确定

  • 开发者交流平台

    本页二维码