Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 1.40 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ## before.java
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. interface Predicate<T> {
  8.         public boolean isTrue(T x);
  9. }
  10.  
  11. public class Scratch {
  12.  
  13.         static <T> List<T> filter(List<T> l, Predicate<T> p) {
  14.                
  15.                 List<T> filtered = new ArrayList<T>();
  16.                 for (T x : l) {
  17.                         if (p.isTrue(x)) filtered.add(x);
  18.                 }
  19.                
  20.                 return filtered;
  21.         }
  22.        
  23.         public static void main(String[] args) {
  24.  
  25.                 List<Integer> l = filter(
  26.                                 Arrays.asList(1,2,3),
  27.                                 new Predicate<Integer>() {
  28.                                         public boolean isTrue(Integer x) {
  29.                                                 return x % 2 == 0;
  30.                                         }
  31.                                 }
  32.                 );
  33.                
  34.                 System.out.println(l);
  35.         }
  36. }
  37.  
  38.  
  39. ## after.java
  40.  
  41.  
  42. import java.util.ArrayList;
  43. import java.util.Arrays;
  44. import java.util.List;
  45.  
  46. interface Predicate<T> {
  47.         public boolean isTrue(T x);
  48. }
  49.  
  50. public class Scratch {
  51.  
  52.         static <T> List<T> filter(List<T> l, Predicate<T> p) {
  53.                
  54.                 List<T> filtered = new ArrayList<T>();
  55.                 for (T x : l) {
  56.                         if (p.isTrue(x)) filtered.add(x);
  57.                 }
  58.                
  59.                 return filtered;
  60.         }
  61.        
  62.         public static void main(String[] args) {
  63.  
  64.                 List<Integer> filtered = new ArrayList<Integer>();
  65.                 for (Integer x1 : Arrays.asList(1,2,3)) {
  66.                         Predicate<Integer> r = new Predicate<Integer>() {
  67.                                                                 public boolean isTrue(Integer x) {
  68.                                                                         return x % 2 == 0;
  69.                                                                 }
  70.                                                         };
  71.                         if (x1 % 2 == 0) filtered.add(x1);
  72.                 }
  73.                 List<Integer> l = filtered;
  74.                
  75.                 System.out.println(l);
  76.         }
  77. }