Java oneliner to clear lists

Is there something like this in the java constructor (implemented here in python):

[] = [item for item in oldList if item.getInt() > 5] 

Today I am using something like:

 ItemType newList = new ArrayList(); for( ItemType item : oldList ) { if( item.getInt > 5) { newList.add(item); } } 

And for me, the first way looks a little smarter.

+6
java closures python collections
source share
5 answers

Java 7 may or may not implement closures and therefore support functions such as this, but this is not the case at present, so in a Java virtual machine you can do this in Groovy , Scala or Clojure (others are possible), but in java you can only approach this with helpers such as Guava Collections2.filter () .

Sample JDK 7 code:

 findItemsLargerThan(List<Integer> l, int what){ return filter(boolean(Integer x) { x > what }, l); } findItemsLargerThan(Arrays.asList(1,2,5,6,9), 5) 

Groovy sample code:

 Arrays.asList(1,2,5,6,9).findAll{ it > 5} 

Guava sample code:

 Collections2.filter(Arrays.asList(1, 2, 5, 6, 9), new Predicate<Integer>(){ @Override public boolean apply(final Integer input){ return input.intValue() > 5; } } ); 

Scala sample code (thanks Bolo):

 Array(1, 2, 5, 6, 9) filter (x => x > 5) 
+5
source share

You can watch lambdaj . There is a selection method that you can use with the hamcrest condition.

+3
source share

Nothing is impossible (-:

 import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListCleaner { public static void main(String[] args) { final List<Integer> oldList = Arrays.asList(new Integer[] { 23, 4, 5, 657 }); System.out.println(oldList); List<Integer> newList = new ArrayList<Integer>() { { for (Integer element : oldList) { if (element > 5) { this.add(element); } } } }; System.out.println(newList); } } 

The only limitation is that oldList must be final.

+1
source share

This can be done in pure Java, but you need to write a support class for the next successful code run:

  List<Integer> oldList = Arrays.asList(new Integer[] { 1, 2, 5, 6, 9 }); List<Integer> newList = new Filter<Integer>(oldList) { { findAll(it > 5); } }.values(); System.out.println(newList); // [6, 9] 

In case you are wondering why this code compiles, see Hidden Java Functions : Initializing Double Brackets. This creates an anonymous instance of the Filter class, which contains its variable and provides the findAll () method.

The filter class itself has one drawback, which creates a new instance for each element of the list to calculate the logical condition in findAll ():

 public abstract class Filter<T> { protected List<T> values = new ArrayList<T>(); protected T it; public Filter(List<T> values) { if (values != null) { this.values.addAll(values); } if (values.isEmpty()) { throw new RuntimeException("Not for empty collections!"); } it = values.iterator().next(); // instance initializer gets executed here, calls findAll } protected void findAll(boolean b) throws Throwable { // exit condition for future calls if (values.size() > 1) { // only repeat for each entry, if values has multiple entries Iterator<T> iterator = values.iterator(); while (iterator.hasNext()) { // don't evalute again for the first entry if (!b) { iterator.next(); iterator.remove(); b = true; } else { // for each other entry create an argument with one element List<T> next = new ArrayList<T>(); next.add(iterator.next()); // get constructor of anonymous class Constructor<?> constructor = this.getClass().getDeclaredConstructors()[0]; // invoke constructor and thus execute instance initializer again Filter<T> filtered = (Filter<T>) constructor.newInstance(new Object[] { null, next }); // if values is empty, the condition didn't match and the element can be removed if (filtered.values.isEmpty()) { iterator.remove(); } } } } else { // one element can be checked directly if (!b) { values.clear(); } } } public List<T> values() { return values; } } 

But since instantiating is pretty cheap these days, and the Filter class is applicable to all objects, you can include it in the Utils package.

Greetz, GHAD

+1
source share

No, such a dynamic language construct is not yet supported in Java :-) So, you need to live with your option 2

0
source share

All Articles