baumlol
baumlol

Reputation: 85

How to filter a Collection dependening on multiple attributes?

I'm sorry, I don't know how to express my issue better, so I want to show an example. I have a Collection of a class and each object of this Collection also contains objects of other classes.

Three of these objects are Date objects. Now I want to filter my collection. Let's say I first want to get the highest of these three Date objects. Now I want to take this highest Date of each object in the Collection and check if it's before today's Date. I hope you get my point.

I want to use the Java 8 Streams API and filter my Collection.

List<MyClass> c = new ArrayList<MyClass>();
c = query.getResultList();
c.stream().filter(); // I don't know how to filter this Collection

How can I filter my Collection properly? I only want to have objects in this Collection which have it's highest Date object before today's Date.

Upvotes: 0

Views: 573

Answers (1)

kunpapa
kunpapa

Reputation: 367

I've never used it, but you can reach some help at stream java doc:

Reading this, you can try this:

Calendar today = Calendar.getInstance();
List<MyClass> c = new ArrayList<MyClass>();
c = query.getResultList();
c.stream().filter(w -> today.getTime().compareTo(w.getDate())>0);

Upvotes: 1

Related Questions