java - 将 Collection<myClass> 转换为 Collection<String>

标签 java collections apache-commons anonymous-class

我试图实现类似于 CollectionUtils transform (Apache Commons Collections) 的功能

class CollectionUtils {

    public static void transformerModifier(Collection<MyClass> myCollection) {
        // How should I implement this method in order that 
        // output from the line 1 and line 2 will be the same ? 
    }

    public static List<String> transform(Collection<MyClass> myCollection) {
        List<String> strCollection = new LinkedList<>();
        for (MyClass item : myCollection) {
            strCollection.add(item.getName());
        }
        return strCollection;
    }
}

class myClass { 
    private String name; 
    private int value; 

    myClass( String name, int value) {
        this.name = name ; 
        this.value = value; 
    }

    public String toString(){
        return new String(name+ ":" + value ) ; 
    }
}

class MyClassCollection{
    private List<myClass> list ; 

    myClassCollection(List<myClass> list){
        this.list = list; 
    }

    List<myClass> collection(){
        return list.clone(); 
    }

}

public class TestClass{

    public static void main (String[] args) {

        List<MyClass> list = new ArrayList<>(); 
        list.add(new myClass("John", 12);
        list.add(new myClass("Mike", 16);
        list.add(new myClass("Eric", 13);
        list.add(new myClass("Mark", 142);
        list.add(new myClass("Alex", 112);

        MyClassCollection myOjb = new MyClassCollection(list );
        CollectionUtils.transformerModifier(myObj.collection() ); 
        List<MyClass> myList = CollectionUtils.transform(myObj.collection()); 
        System.out.println(Arrays.toString(myObj.collection().toArray)); // line 1
        System.out.println(Arrays.toString(myList.toArray));            // line 2
    } 

}

output: [John,Mike,Eric,Mark,Alex] // output after line 1 
output: [John,Mike,Eric,Mark,Alex] // should be output after line 2 

我的问题是,是否可以通过更改对象 myObj 的集合的方式实现方法 transformerModifier,以便 myObj.collection() 返回的不是 List<myClass>。但是列表 List<String> (其中字符串是来自 private String name 的数据成员 myClass 的数据)?

我的猜测是,解决方案应该是通过匿名类。但是,我还不明白我应该如何实现它。

最佳答案

如果您使用的是 Java 8,则可以使用 streammap() 来执行如下操作:

List<MyClass> myClassList = new ArrayList<>();
//add your items to myClassList here

List<String> names = myClassList.stream().map(MyClass::getName).collect(Collectors.toList());
//names will now consist of a List of all the names associated with
//each of the MyClass objects within myClassList in the same order

此解决方案还使用了方法引用 MyClass::getName。这会调用 stream 中每个对象的 getName 方法 map 使用 .map( )

接下来它使用 .collect() 将它从 stream 带回 list 使用 Collectors.toList()

如果您在 myClassList 中处理大量对象,可以使用 .parallelStream() 而不是 .stream()< 来加快此过程,但如果您不处理大量数据,您可能会发现 .parallelStream() 的性能下降。这完全取决于您希望在 List 中出现多少个对象。

关于java - 将 Collection<myClass> 转换为 Collection<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622131/

相关文章:

java - 如何禁用标准 Ant 任务?

java - 为什么计数器是 double 而不是 long?

c# - 想要 : . 快速高效地存储一堆不区分大小写的字符串的网络集合

java - 为什么HashMap的初始容量是16(2的幂),Hashtable的初始容量是11(素数)?

java - 从 FileItem 的名称中仅提取文件名

java - 集成测试中 Spring Controller 中的 Mock Feign 客户端

java - 如何制作android下载事件?

c# - 如何在不进行广泛搜索的情况下根据 2 个条件获取对象?

java - 如何使用支持 Zip64 的 ScatterZipOutputStream 实现并行 Zip 创建?

java - Maven 对 apache commons-text 的依赖?