Example usage for java.util List stream

List of usage examples for java.util List stream

Introduction

In this page you can find the example usage for java.util List stream.

Prototype

default Stream<E> stream() 

Source Link

Document

Returns a sequential Stream with this collection as its source.

Usage

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    int highestValue = transactions.stream().map(Graduate::getValue).reduce(0, Integer::max);
    System.out.println(highestValue);

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    String traderStr = transactions.stream().map(transaction -> transaction.getTrader().getName()).distinct()
            .sorted().reduce("", (n1, n2) -> n1 + n2);
    System.out.println(traderStr);

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    boolean milanBased = transactions.stream()
            .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan"));
    System.out.println(milanBased);

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    List<String> cities = transactions.stream().map(transaction -> transaction.getTrader().getCity()).distinct()
            .collect(Collectors.toList());
    System.out.println(cities);//  w  ww .j a  v  a2s.c om

}

From source file:Main.java

public static void main(String... args) {
    Student raoul = new Student("Raoul", "Cambridge");
    Student mario = new Student("Mario", "Milan");
    Student alan = new Student("Alan", "Cambridge");
    Student brian = new Student("Brian", "Cambridge");

    List<Graduate> transactions = Arrays.asList(new Graduate(brian, 2011, 300), new Graduate(raoul, 2012, 1000),
            new Graduate(raoul, 2011, 400), new Graduate(mario, 2012, 710), new Graduate(mario, 2012, 700),
            new Graduate(alan, 2012, 950));

    List<Graduate> tr2011 = transactions.stream().filter(transaction -> transaction.getYear() == 2011)
            .sorted(Comparator.comparing(Graduate::getValue)).collect(Collectors.toList());
    System.out.println(tr2011);/*from  w ww. ja  v  a 2  s . c om*/

}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    stringCollection.stream().sorted().filter((s) -> s.startsWith("a")).forEach(System.out::println);
}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    stringCollection.stream().map(String::toUpperCase).sorted((a, b) -> b.compareTo(a))
            .forEach(System.out::println);
}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    long startsWithB = stringCollection.stream().filter((s) -> s.startsWith("b")).count();

    System.out.println(startsWithB); // 3

}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    // filtering/* w ww .j  av  a 2  s  .c  o  m*/

    stringCollection.stream().filter((s) -> s.startsWith("a")).forEach(System.out::println);
}

From source file:Main.java

public static void main(final String[] args) {
    List<String> stringCollection = new ArrayList<>();
    stringCollection.add("ddd2");
    stringCollection.add("aaa2");
    stringCollection.add("bbb1");
    stringCollection.add("aaa1");
    stringCollection.add("bbb3");
    stringCollection.add("ccc");
    stringCollection.add("bbb2");
    stringCollection.add("ddd1");

    boolean anyStartsWithA = stringCollection.stream().anyMatch((s) -> s.startsWith("a"));

    System.out.println(anyStartsWithA); // true
}