>我试图实现功能类似于 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>
列表(其中字符串是来自myClass
的数据成员的数据private String name
)?
我的猜测是解决方案应该是通过匿名类。但是,我还不明白我应该如何实现它。
如果你使用的是Java 8,你可以利用stream
s和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
该解决方案还利用了方法参考以及MyClass::getName
。 这会在stream
map
中的每个对象上调用 getName
方法,并使用 .map()
将其 ping 到转换后的流中的相应位置。
接下来,它使用 .collect()
将其从stream
恢复到list
Collectors.toList()
。
如果您在 myClassList
中处理大量对象,则可以使用 .parallelStream()
而不是 .stream()
来加快此过程,但是如果您不处理大量数据,您可能会看到 .parallelStream()
的性能下降。 这完全取决于您期望在List
中存在多少对象。
public interface Converter<I, O> {
void tranformer(List list);
O retriever(I obj);
}
_
public static <I, O> void transform(Converter<I, O> converter, List inputList) {
Iterator<I> it = inputList.iterator();
List list = new LinkedList<>();
while (it.hasNext()) {
list.add(converter.retriever(it.next()));
}
converter.tranformer(list);
}
_
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 myclasscollection = new MyClassCollection(list);
final List collectionList = myclasscollection.collection();
CollectionUtils.transform(new Converter<myClass, String>() {
@Override
public void tranformer(List list) {
employeeList.clear();
employeeList.addAll(list);
}
@Override
public String retriever(myClass obj) {
return obj.name; // make the data member public or add getter
}
}, collectionList);
collectionList.get(0).toString.toLowerCase();
}
这并不完全是您需要的,但我敢打赌这不是一个糟糕的选择。请注意,可以输出集合集合列表将是对象的集合(不是字符串),但是,您可以像这样访问字符串数据类型的方法collectionList.get(0).toString.toLowerCase();
希望这有所帮助。