在Java 8中添加两个列表?

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

Add two lists in java8?

问题

我正试图在Java 8中将两个列表相加。
这两个列表来自JSON中的不同对象,但我想在Java代码中将它们放到一个新对象中。

我已经尝试了listUtils.union,但这会将它们作为两个数组添加到新对象中。
addAll方法和stream.concat()也会导致错误。

示例:

JSON内容如下:

“对象1”:[{ test:”2”
}]

“对象2”:[{ test:”2”
}]

尝试将它们添加到一个HashMap中,放到对象“newobject”中,但我必须调用方法来获取这些新列表。

例如:

newlist = listUtils.Union(addobject1(), addObject1)(这些是从JSON中返回我的列表的方法名)。

put(“newobject”, newlist)

有任何建议吗?

英文:

I am trying to add two lists together in java 8.
Both lists are coming from separate objects in the json but I want to put them into a new object together in the java code.

I have tried listUtils.union but this is adding to the new object as two arrays.
The addAll method and stream .concat() are also giving errors.

Example:

Json has:

“Object 1”: [{ test:”2”
}]

“Object 2”: [{ test:”2”
}]

Trying to add to a hash map into the object “newobject” but I have to call methods to get the new lists.

For example :

Newlist = listUtils.Union(addobject1(), addObject1) (these are the method names that return my lists from the json.

put(“newobject”, newlist)

Any suggestions ?

答案1

得分: 1

根据JSON字符串,您有一些POJO的列表:

  1. class MyPojo {
  2. private final String test;
  3. public MyPojo(String t) {
  4. this.test = t;
  5. }
  6. public String getTest() {
  7. return test;
  8. }
  9. @Override
  10. public String toString() {
  11. return "{ test: " + test + "}";
  12. }
  13. }

然后object1object2是输入列表,有几种方法可以合并它们:

  1. 使用Stream.concat
  1. Stream.concat(object1.stream(), object2.stream()).collect(Collectors.toList());
  1. 使用Stream.of
  1. Stream.of(object1, object2)
  2. .flatMap(List::stream)
  3. .collect(Collectors.toList());
  1. 使用addAll
  1. List<MyPojo> result = new ArrayList<>(object1);
  2. result.addAll(object2);
  1. 流式处理列表的列表:
  1. Arrays.asList(object1, object2)
  2. .stream()
  3. .flatMap(List::stream)
  4. .collect(Collectors.toList());
英文:

According to the JSON string, you have lists of some POJO:

  1. class MyPojo {
  2. private final String test;
  3. public MyPojo(String t) {this.test = t; }
  4. public String getTest() {return test; }
  5. @Override
  6. public String toString() {
  7. return &quot;{ test: &quot; + test + &quot;}&quot;;
  8. }
  9. }

Then object1 and object2 are the input lists and there are several ways to merge them:

  1. Using Stream.concat
  1. Stream.concat(object1.stream(), object2.stream()).collect(Collectors.toList());
  1. Using Stream.of
  1. Stream.of(object1, object2)
  2. .flatMap(List::stream)
  3. .collect(Collectors.toList());
  1. Using addAll
  1. List&lt;MyPojo&gt; result = new ArrayList&lt;&gt;(object1);
  2. result.addAll(object2);
  1. Streaming list of lists
  1. Arrays.asList(object1, object2)
  2. .stream()
  3. .flatMap(List::stream)
  4. .collect(Collectors.toList());

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

发表评论

匿名网友

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

确定