The best way to filter a Java Collection?

0 votes
May 28, 2018 in Java by sharth
• 3,350 points
69 views

1 answer to this question.

0 votes

 this problem is solved using streams and lambdas in one line of code:

List<Person> beerDrinkers = persons.stream()
    .filter(p -> p.getAge() > 16).collect(Collectors.toList());

Use Collection#removeIf to modify the collection in place. 

persons.removeIf(p -> p.getAge() <= 16);
answered May 28, 2018 by parth295
• 4,640 points

Related Questions In Java

0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,050 points
177 views
0 votes
2 answers

what is the best way to convert an ArrayList to a String

You could probably use the Joiner class ...READ MORE

answered Aug 19 in Java by Sirajul
• 30,540 points
70 views
0 votes
1 answer

What is the simplest way to read JSON from a URL in java

Read json from url use url.openStream() and read contents ...READ MORE

answered Jun 13, 2018 in Java by samarth295
• 2,190 points
300 views
0 votes
2 answers

What is the easiest way to iterate through the characters of a string in Java?

There are two approaches to this: for(int i ...READ MORE

answered Aug 19 in Java by Sirajul
• 30,540 points
74 views
0 votes
1 answer

Setting time zone of a java.util.Date

You can make use of the following ...READ MORE

answered Aug 14, 2018 in Java by anto.trigg4
• 3,420 points
4,324 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,880 points
606 views
0 votes
1 answer
0 votes
2 answers

Scanner is skipping nextLine() after using next() or nextFoo()?

use input.nextLine(); after your nextInt() function for example:- input.nextInt(); ...READ MORE

answered May 11 in Java by Sourav Dangi
1,116 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
102 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,640 points
58 views