java 在Java 8中没有预定义方法的情况下将集合转换为另一种集合类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31560824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:47:24  来源:igfitidea点击:

Converting a collection to another collection type without predefined method in Java 8

javacollectionsjava-8

提问by Konstantine

What I'm looking for is a way to convert a collection of one type to a collection of another type using streamsand mapWITHOUT having a predefined function/functional interface that converts object A to object B. Java 6 example:

我正在寻找的是一种使用映射将一种类型的集合转换为另一种类型的集合的方法,而无需具有将对象 A 转换为对象 B 的预定义函数/功能接口。 Java 6 示例:

for (ObjectA objA : collectionA) {
    ObjectB objB = new ObjectB();
    objB.setId(objA.getId());
    collectionB.add(objB);
}

I'm looking for something in the lines of:

我正在寻找以下内容:

List<ObjectB> collectionB = collectionA.stream().map(objA -> {
    ObjectB objB = new ObjectB();
    objB.setId(objA.getId());
    return objB;
});

Which of course doesn't work but you get the gist of what I'm trying to do.

这当然行不通,但您已了解我正在尝试做的事情的要点。

回答by Konstantin Yovkov

After you do the mapping, you have to collectthe mapped objects:

完成映射后,您必须收集映射对象:

List<ObjectB> collectionB = collectionA.stream().map(objA -> {
    ObjectB objB = new ObjectB();
    objB.setId(objA.getId());
    return objB;
}).collect(Collectors.toList());

回答by Eran

Your sample code is simply missing the terminal collectoperation, to collect the elements of the Stream<ObjectB>that was returned from your mapoperation into a List<ObjectB>:

您的示例代码只是缺少终端collect操作,用于Stream<ObjectB>将从您的map操作返回的元素收集到一个List<ObjectB>

List<ObjectB> collectionB = collectionA.stream().map(objA -> {
    ObjectB objB = new ObjectB();
    objB.setId(objA.getId());
    return objB;
}).collect(Collectors.toList());

If you add to your ObjectBclass a constructor that accepts a parameter of type ObjectAand sets the ID, you can simplify your code :

如果向ObjectB类添加一个接受类型参数ObjectA并设置 ID的构造函数,则可以简化代码:

i.e. in ObjectByou add this constructor :

即在ObjectB你添加这个构造函数:

public ObjectB (ObjectA objA)
{
    setId(objA.getId());
}

And your code becomes :

你的代码变成:

List<ObjectB> collectionB = collectionA.stream()
                                       .map(ObjectB::new)
                                       .collect(Collectors.toList());

回答by rogerdpack

For followers,

对于追随者,

 incompatible types: no instance(s) of type variable(s) R 
 exist so that java.util.stream.Stream<R> conforms to   
 java.util.List<org.class.Name>

after doing a map conversion, actually meant "you are passing the output of your stream to a method that doesn't take that as a parameter" (the stream map conversion itself is actually working), or you're assigning it to a variable that's of a different type.

进行映射转换后,实际上意味着“您将流的输出传递给不将其作为参数的方法”(流映射转换本身实际上在工作),或者您将其分配给变量那是另一种类型。

回答by Shubham Jain

You can directly convert it by passing the reference of the first collection to another collection parameterize constructor.

您可以通过将第一个集合的引用传递给另一个集合参数化构造函数来直接转换它。

Example:

例子:

     List<String> ff = new ArrayList<String>();
     ff.add("AA");
     ff.add("AA");
     ff.add("cc");
     ff.add("dd");
     ff.add("ee");

     System.out.println(ff.size());

     HashSet<String> gg = new HashSet<>(ff);

     System.out.println(gg.size());
     for (String s: gg) {
          System.out.println("output= " + s);
    }

The size of 5 is decremented to 4 in another collection as List can contains duplicates while Set is not.

在另一个集合中,5 的大小递减为 4,因为 List 可以包含重复项而 Set 不包含重复项。

Output:

输出:

5

4

output = AA

output = cc

output = dd

output = ee

5

4

输出 = AA

输出 = cc

输出 = dd

输出 = ee