i have class A {int sNo; String name;} with valid constructor.
i need to trim name and sort on the basis of name using stream API.
public class Test1 {
public static void main(String[] args) {
ArrayList<A> l = new ArrayList();
l.add(new A(1, " 1name "));
l.add(new A(7, " 3name "));
l.add(new A(6, ""));
l.add(new A(5, " 2name "));
l.add(new A(4, " 5name "));
l.add(new A(2, ""));
List<String> i = l.stream()
.filter(s -> !s.name.isEmpty())
.map(s -> s.name.trim())
.sorted()
.collect(Collectors.toList());
System.out.println(i);
}
}
it is returning only name in sorted order
[, 1name, 2name, 3name, 5name]
but i need entire object.
i know s -> s.name.trim() is the reason its storing name only,
how can apply operation on particular filed but store entire object.