Java integer ArrayList returns elements in a specific range
I have an integer Java ArrayList
ArrayList <Integer> ageList = new ArrayList <Integer>();
I have some integer values in this ArrayList I want to create a new ArrayList that contains all the elements of the above ArrayList passing conditions, such as values between 25 and 35 That is, if my agelist contains a value
My NEWLIST should contain
How can I do this?
Note: I came to Java from the target c background, where I used nspredicate to do this easily Is there a similar method in Java?
Solution
There is no "filter" function in the standard API (but in guava and Apache Commons.) You must create a new list, loop through the original list and manually add the elements in the range 25-35 to the new list
If you do not intend to keep the original list, you can use iterator and iterator The remove method removes the element from the scope
If there are no duplicates in the list, you can use TreeSet. In this case, you can use headset / tailset to get all elements in a specific range
Related questions: (in fact, it may even be repeated)
> What is the best way to filter a Java Collection?