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> friends = Arrays.asList("CSS", "HTML", "Oracle", "Dart");

    Comparator<String> compByLength = (aName, bName) -> aName.length() - bName.length();
    friends.stream().sorted(compByLength).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    List<Trade> trades = TradeUtil.createTrades();

    Map<String, List<Trade>> issuers = trades.stream().collect(Collectors.groupingBy(t -> t.getIssuer()));

    System.out.println("Grouped List: " + issuers);
}

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));

    persons.stream().reduce((p1, p2) -> p1.age > p2.age ? p1 : p2).ifPresent(System.out::println); // Pamela
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();
    double average = roster.stream().filter(p -> p.getGender() == Person.Sex.MALE).mapToInt(Person::getAge)
            .average().getAsDouble();//from w ww.j  a v a  2  s  . c om

    System.out.println("Average age of male members (bulk data operations): " + average);
}

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<Person> personsOver18 = persons.stream().filter(p -> p.getAge() > 18);
    personsOver18.forEach(p -> System.out.println(p.getFirstName()));

}

From source file:Ran.java

public static void main(String[] args) {

    // Get and shuffle the list of arguments
    List<String> argList = Arrays.asList(args);
    Collections.shuffle(argList);

    // Print out the elements using JDK 8 Streams
    argList.stream().forEach(e -> System.out.format("%s ", e));

    // Print out the elements using for-each
    for (String arg : argList) {
        System.out.format("%s ", arg);
    }//from  w ww.  j av a  2  s. c om

    System.out.println();
}

From source file:Main.java

public static void main(String[] args) {
    List<Employee> persons = Employee.persons();
    System.out.println("Before increasing the   income:   " + persons);

    persons.stream().filter(Employee::isFemale).forEach(p -> p.setIncome(p.getIncome() * 1.10));

    System.out.println("After increasing the   income:   " + persons);
}

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) -> sum += p.age, (sum1, sum2) -> sum1 + sum2);

    System.out.println(ageSum);//from   w w w .ja  va  2  s. 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));

    String names = persons.stream().filter(p -> p.age >= 18).map(p -> p.name)
            .collect(Collectors.joining(" and ", "In Germany ", " are of legal age."));

    System.out.println(names);/* 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));

    Double averageAge = persons.stream().collect(Collectors.averagingInt(p -> p.age));

    System.out.println(averageAge); // 19.0
}