package test; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Random; import java.util.stream.Collectors; /** * * @author matthew.rupert */ public class Test { private static DoStuff stuff; private static List a; private static class MyCoolClass implements Comparable{ int value; MyCoolClass(int value) { this.value=value; } public int getValue() { return value; } public void add(int x) { value+=x; } @Override public String toString() { return Integer.toString(value); } @Override public int compareTo(MyCoolClass other) { if (this.getValue() > other.getValue()) return 1; return -1; } } private static class DoStuff { public static void doStuff() { a = new ArrayList<>(); //List a a = new ArrrayList<>(); Random rand = new Random(); for (int i=0; i<=10; i++) { a.add(new MyCoolClass(rand.nextInt(100))); } //Collections.sort(a); Collections.sort(a); System.out.println("sorted: " + a.toString()); Collections.shuffle(a); System.out.println("\nshuffled: " + a.toString()); // Filter only > 50 System.out.println("\nfiltered (>50): "); a.stream().filter(b -> b.getValue() >= 50).forEach(b -> System.out.println(b)); // Filter only > 50 and even System.out.println("\nfiltered (>50 and even): "); a.stream().filter(b -> b.getValue() >= 50).filter(b -> b.getValue() % 2 == 0).forEach(b -> System.out.println(b)); // Filter only < 50, place results in ArrayList System.out.println("\nfiltered (<50) with results as ArrayList: "); List c; c = a.stream().filter(b -> b.getValue() < 50).collect(Collectors.toList()); System.out.println(c); } } public static void main(String[] args) { DoStuff.doStuff(); } }