I have a class A which is in the client and I have an identical class A1 in the domain of my project. I have a function which receives Set<A> and I want to convert it to List<A1> and return. I was able to do it with traditional foreach loop.
But how can this be done via streams?
private List<A1> myDataRep(final Set<A> myData) {
//Convert and return List.
}
CodePudding user response:
return myData.stream()
.map(a -> new A1(a)) // or however you convert a single A into an A1
.collect(Collectors.toList());
CodePudding user response:
There are multiple ways:
class Mapper<T> {
List<T> toList(Set<T> s) {
return s.stream().toList();
}
}
final Set<String> s = Sets.set("one", "two", "three");
// explicit creating a list
assertThat(Lists.newArrayList(s).size()).isEqualTo(3);
// using the stream API
assertThat(s.stream().toList().size()).isEqualTo(3);
// using a class decorating the conversion. it might
// add a bit of convenience, when state is required
// for mapping or filtering
assertThat(new Mapper<String>().toList(Sets.set("one", "two", "three")).size()).isEqualTo(3);
CodePudding user response:
You could stream your input Set and then map each A element to A1 with your custom mapping method. Finally, you collect the elements within a List and then return them.
public class MyClass {
public static List<A1> convert(Set<A> set){
return set.stream()
.map(MyClass::myMapping) // mapping each A element to A1 with your custom mapping implementation
.collect(Collectors.toList()); //returning the list of A1 elements
}
private static A1 myMapping(A a){
retun /* ... my mapping implementation ... */
}
}
CodePudding user response:
I have a class A which is in the client and I have an identical class A1
I guess by saying identical, you mean that this classes share some attributes, so that information contained in the instance of A is sufficient to create a new instance of A1.
If that is the case, you can define a constructor inside A1 class that will expect an instance of A as an argument.
With that you method body might look like that:
return myData.stream().map(A1::new).toList();
Note, operation toList() is accessible with Java 16, for earlier versions you should use collect(Collectors.toList()) instead.
CodePudding user response:
You need to change method on map.
Set<Integer> integerSet = Set.of(1,2,3);
List<String> stringList = integerSet.stream().map(String::valueOf).collect(Collectors.toList());
CodePudding user response:
I would define a lambda to map A to A1
- first create some records (immutable classes for demo)
record A(int getVal, String getStr) {}
record A1(String getStr){
@Override
public String toString() {
return getStr();
}
}
- Then initialize a list of
A's.
List<A> listA = List.of(new A(1, "Alpha"), new A(2, "Beta"));
- Define the mapping function (here, simple concatenation).
Function<A, A1> mapAtoA1 = a-> new A1(a.getVal() a.getStr());
- Stream
listAdoing the conversion.
List<A1> listA1 = listA.stream().map(mapAtoA1).toList();
System.out.println(listA1);
prints
[1Alpha, 2Beta]
