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) {
    List<String> names = Arrays.asList("XML", "HTML", "Java", "Javascript", "CSS", "C");

    Function<String, Predicate<String>> startsWithLetter = letter -> name -> name.startsWith(letter);

    names.stream().filter(startsWithLetter.apply("J")).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Person result = persons.stream().reduce(new Person("", 0), (p1, p2) -> {
        p1.age += p2.age;/* w w w.  j a v  a 2 s.  c om*/
        p1.name += p2.name;
        return p1;
    });

    System.out.format("name=%s; age=%s", result.name, result.age);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    List<Person> filtered = persons.stream().filter(p -> p.name.startsWith("P")).collect(Collectors.toList());

    System.out.println(filtered); // [Peter, Pamela]
}

From source file:Main.java

public static void main(String[] argv) {
    List<Person> persons = Arrays.asList(new Person("Joe", 12), new Person("Jim", 34), new Person("John", 23));

    Stream<MyWrapper> stream = persons.stream().filter(p -> p.getAge() > 18).map(p -> new MyWrapper(p));

    System.out.println(stream.count());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));
    Map<Integer, String> map = persons.stream()
            .collect(Collectors.toMap(p -> p.age, p -> p.name, (name1, name2) -> name1 + ";" + name2));

    System.out.println(map);//w  w  w  .  j a v a 2s.c o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Integer ageSum = persons.stream().reduce(0, (sum, p) -> {
        System.out.format("accumulator: sum=%s; person=%s\n", sum, p);
        return sum += p.age;
    }, (sum1, sum2) -> {//from  www.  j av  a  2  s  .  com
        System.out.format("combiner: sum1=%s; sum2=%s\n", sum1, sum2);
        return sum1 + sum2;
    });

    System.out.println(ageSum);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    IntSummaryStatistics ageSummary = persons.stream().collect(Collectors.summarizingInt(p -> p.age));

    System.out.println(ageSummary);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Max", 18), new Person("Peter", 23),
            new Person("Pamela", 23), new Person("David", 12));

    Map<Integer, List<Person>> personsByAge = persons.stream().collect(Collectors.groupingBy(p -> p.age));

    personsByAge.forEach((age, p) -> System.out.format("age %s: %s\n", age, p));
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<String> argList = Arrays.asList("help", "v");

    Predicate<String> isHelp = (s) -> s.matches("(h|help)");
    Predicate<String> isVerbose = (s) -> s.matches("(v|verbose)");

    boolean needsHelp = argList.stream().anyMatch(isHelp);
    System.out.println(needsHelp);

    boolean verbose = argList.stream().anyMatch(isVerbose);
    System.out.println(verbose);/*from w w w .j  av a 2  s.  com*/
}

From source file:Main.java

public static void main(String[] args) {

    List<String> friends = Arrays.asList("CSS", "HTML", "Oracle", "Dart");

    //same but reverse
    System.out.println("\nsort reverse:");
    Comparator<String> comp = (aName, bName) -> aName.compareTo(bName);

    friends.stream().sorted(comp.reversed()).forEach(System.out::println);
}