将集合<myClass> 转换为集合<String>
transform Collection<myClass> to Collection<String>
我试图实现类似于 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() return 不是 List<myClass>
而是 List List<String>
的(其中字符串是来自 myClass
的 private String name
数据成员的数据)?
我的猜测是,解决方案应该是通过匿名 class。但是,我还不明白我应该如何实施它。
如果您使用的是 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()
.
将其连接到转换后的流中的相应位置
接下来它使用 .collect()
使用 Collectors.toList()
.
将其从 stream
恢复到 list
如果您在 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 将是对象的集合(不是 String ),但是,您可以像这样正确访问 String 数据类型的方法 collectionList.get(0).toString.toLowerCase();
希望这有帮助。
我试图实现类似于 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() return 不是 List<myClass>
而是 List List<String>
的(其中字符串是来自 myClass
的 private String name
数据成员的数据)?
我的猜测是,解决方案应该是通过匿名 class。但是,我还不明白我应该如何实施它。
如果您使用的是 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()
.
接下来它使用 .collect()
使用 Collectors.toList()
.
stream
恢复到 list
如果您在 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 将是对象的集合(不是 String ),但是,您可以像这样正确访问 String 数据类型的方法 collectionList.get(0).toString.toLowerCase();
希望这有帮助。