I have a list of objects (instance of class A):
Class A {
private String name;
private Date createdDate;
}
List [
A ("a", 01-Jan-2017),
A ("a", 03-Jan-2017),
A ("a", 02-Jan-2017),
A ("b", 05-Jan-2017),
A ("b", 06-Jan-2017),
A ("b", 07-Jan-2017),
.
.
.
A ("x", 02-Jan-2017),
A ("x", 05-Jan-2017),
A ("x", 06-Jan-2017),
A ("x", 07-Jan-2017)
]
How can I extract a list of class A for each 'name' with latest createdDate.
I,e, expected output is -
List [
A ("a", 03-Jan-2017),
A ("b", 07-Jan-2017),
.
.
.
A ("x", 07-Jan-2017)
]
yourList.stream()
.collect(Collectors.groupingBy(
A::getName,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparing(A::getCreatedDate)),
Optional::get)))
.values();
This will return a Collection<A>
, which you can put into an ArrayList
for example if needed.
EDIT
As suggested by Holger, a better way:
...
.stream()
.collect(Collectors.toMap(
A::getName,
Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(A::getCreatedDate))))
.values();
By making A
implement Comparable<A>
you can define a custom ordering based on the createdDate
field.
class A implements Comparable<A> {
private String name;
private Date createdDate;
public A(String name, Date createdDate) {
this.name = name;
this.createdDate = createdDate;
}
@Override
public int compareTo(A other) {
return createdDate.compareTo(other.createdDate); // provided you're using java.util.Date here
}
public static void main(String[] args) {
List<A> aList = ... // Create your list here
Collections.sort(aList);
System.out.println(aList);
}
}
After calling Collections.sort(aList)
your list should be sorted based on the ordering you've implemented.
Then, you can iterate the sorted list and stop, whenever an element has a date later than what you're checking for.
This is my example :
first sort by name and then sort by date .
public class MainClass {
public static void main(String[] args) {
List<A> aList = new ArrayList<>();
aList.add(new A("a",new Date(2017,11,3)));
aList.add(new A("b",new Date(2017,3,3)));
aList.add(new A("a",new Date(2017,11,9)));
aList.add(new A("a",new Date(2017,1,23)));
aList.add(new A("b",new Date(2017,8,15)));
aList.stream().sorted(Comparator.comparing(A::getName).thenComparing(A::getCreateDate))
.filter(distinctByKey(A::getName))
.forEach(System.out::println);
}
private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor)
{
Map<Object, Boolean> map = new ConcurrentHashMap<>();
return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
}
example output :
A{name='a', createDate=Fri Feb 23 00:00:00 IRST 3917}
A{name='b', createDate=Tue Apr 03 00:00:00 IRDT 3917}
if you need collection :
replace foreach to .collect(Collectors.toList()) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.