News for hackers. Stuff that matters.
Re:Why I absolutely love java by nl on Tuesday February 08, 2011 @08:23PM (#2195775)
Screw that.

Others have already pointed out the Google Guava library. This version is almost all just standard Java, just using Guava to create the list of numbers.

  ArrayList<Integer> list =  com.google.common.collect.Lists.newArrayList(1,2,3,4);
  for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {if (it.next() % 2 == 0) {it.remove();}}		
  System.out.println(list);
Also, Java does have REPL. It's called the Eclipse scrapbook view. I'm sure Netbeans has something similar (and that is part of one of the standard Java SDK downloads).
Re:Why I absolutely love java by eneveu on Sunday February 13, 2011 @11:19AM (#2213055)
I'd do it this way, using Guava's functional programming ability + static imports where it makes sense:

  import static com.google.common.collect.Lists.*;
  import com.google.common.collect.Iterables;
  [...]
  public static void main(String[] args) {
    List<Integer> list = newArrayList(1,2,3,4);
    System.out.println(Iterables.filter(list, isEven()));
  }

  private static Predicate<Integer> isEven() {
    return new Predicate<Integer>() {
      public boolean apply(Integer input) {
        return input % 2 == 0;
      }
    };
  }
The lack of closures is seriously felt when creating the "isEven" Predicate. This will be a lot cleaner next year (?), when closures are introduced in the language. For now, we have to live with the verbosity...

It's possible to hide some of this verbosity and write clean code. For example, I often extract the function / predicate declarations to utility classes to avoid polluting the code. In this case, I would move the "isEven()" method to a "MathPredicates" utility class. The code ends up looking like:

    List<Integer> list = newArrayList(1,2,3,4);
    System.out.println(Iterables.filter(list, MathPredicates.isEven()));
Re:Why I absolutely love java by andralex on Tuesday February 08, 2011 @08:56PM (#2195892)
Still terrible.
Re:Why I absolutely love java by fleitz on Tuesday February 08, 2011 @08:51PM (#2195872)
If you look close enough you can almost figure out what it's doing between all the line noise.
Re:Why I absolutely love java by nl on Tuesday February 08, 2011 @09:16PM (#2195964)

  // create an array list with 4 integer elements
  ArrayList<Integer> list =  com.google.common.collect.Lists.newArrayList(1,2,3,4); 

  // get an Iterator from the list, and use it to iterate though the list
  for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
    // check if this element is even. 
    // If we want the odd numbers, change to != 0 
    if (it.next() % 2 == 0) {
      // remove it if it is even
      it.remove();
    }
  }		

  // print the list
  System.out.println(list);
Re:Why I absolutely love java by TeHCrAzY on Tuesday February 08, 2011 @10:16PM (#2196103)
Not sure if you were being a smart ass, but it would be much more readable without any of the comments, and simply with the new lines.
Re:Why I absolutely love java by nl on Tuesday February 08, 2011 @10:51PM (#2196209)
I'm being a smartass. Kinda..

The commenter said they could almost figure out what it's doing. It's not clear from that that comment if it is just the formatting that was the problem (which I agree, isn't very readable).