1

I am looking for a simple method instead of while/for loop, for filtering out set of objects from ArrayList<Object> base on the object that contains a unique value.

For example ,i have list of object as

[Status [status=new], Status [status=closed], Status [status=new], Status [status=closed], Status [status=new], Status [status=closed], Status [status=new], Status [status=closed], Status [status=new], Status [status=closed]].

I need the objects that has status value"new" into a list to compare.No need of the object that has already closed.

2
  • Post some code of what you have done. And explain yourself better, please. Commented Dec 21, 2015 at 13:08
  • use Set<> instead of ArrayList Commented Dec 21, 2015 at 13:49

1 Answer 1

4

The unique way to write less and readable code for filtering a collection is use java 8 lambda expressions.

Unfortunately java8 is not still supported by Android but....you can use retrolambda. And if you need to use the Stream API you can try this plugin Lightweight-Stream-API. (the Stram API is not supported by retrolambda)

In that case:

List<Person> newList = Stream.of(personList).filter(p -> p.getAge() > 18).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

2 Comments

where did p comes from inside filter(p ->..)?
in the statement filter(p -> p.getAge() > 18) p actually declares itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.