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

2 java collections anonymous-class apache-commons

我尝试实现类似于CollectionUtils 转换(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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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 ) ; 
    }
}
Run Code Online (Sandbox Code Playgroud)

class MyClassCollection{
    private List<myClass> list ; 

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

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

}
Run Code Online (Sandbox Code Playgroud)

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
    } 

}
Run Code Online (Sandbox Code Playgroud)

output: [John,Mike,Eric,Mark,Alex] // output after line 1 
output: [John,Mike,Eric,Mark,Alex] // should be output after line 2 
Run Code Online (Sandbox Code Playgroud)

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

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

Cra*_*806 5

如果您使用的是 Java 8,您可以使用streams 并map()执行如下操作:

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
Run Code Online (Sandbox Code Playgroud)

该解决方案还利用了方法参考MyClass::getName。这将调用getName每个对象上的方法,并stream map使用 ping 将其连接到转换后的流中相应的位置.map()

接下来,它使用.collect()将其从 a 返回streamlistusing Collectors.toList()

如果您正在处理 中的大量对象,则可以使用代替 来myClassList加快此过程,但如果您不处理大量数据,则可能会发现使用 时性能有所下降。这完全取决于您期望在..parallelStream().stream().parallelStream()List