Skip to content

Instantly share code, notes, and snippets.

@aruld
Created September 16, 2012 04:16
Show Gist options
  • Save aruld/3730977 to your computer and use it in GitHub Desktop.
Save aruld/3730977 to your computer and use it in GitHub Desktop.
Query an ArrayList with LINQ sample ported from C# to Java 8 (http://msdn.microsoft.com/en-us/library/bb397937.aspx)
import java.util.ArrayList;
import java.util.List;
public class LINQQueryExample {
public static class Student {
public String firstName;
public String lastName;
public int[] scores;
Student(String firstName, String lastName, int[] scores) {
this.firstName = firstName;
this.lastName = lastName;
this.scores = scores;
}
}
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
students.add(new Student("Svetlana", "Omelchenko", new int[]{98, 92, 81, 60}));
students.add(new Student("Claire", "O’Donnell", new int[]{75, 84, 91, 39}));
students.add(new Student("Sven", "Mortensen", new int[]{88, 94, 65, 91}));
students.add(new Student("Cesar", "Garcia", new int[]{97, 89, 85, 82}));
students.stream()
.filter(s -> s.scores[0] > 95)
.forEach(s -> System.out.println(s.lastName + ": " + s.scores[0]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment