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<Trade> trades = TradeUtil.createTrades();

    List<Integer> list = trades.stream().map(t -> t.getQuantity()).collect(Collectors.toList());
    System.out.println("Collect List: " + list);
}

From source file:Main.java

public static void main(String[] args) {

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

    //just sort using lambda comparator
    System.out.println("sort:");
    friends.stream().sorted((aName, bName) -> aName.compareTo(bName)).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();

    Integer totalAge = roster.stream().mapToInt(Person::getAge).sum();

    System.out.println("Sum of ages (sum operation): " + totalAge);

}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5);

    Stream<Integer> s = Stream.concat(numbers.stream(), numbers1.stream());

    System.out.println(s.count());
}

From source file:Main.java

public static void main(String[] args) {

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

    //just sort using lambda comparator
    System.out.println("sort:");

    friends.stream().sorted(String::compareTo).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    List<Employee> persons = Employee.persons();
    // Find any male
    Optional<Employee> anyMale = persons.stream().filter(Employee::isMale).findAny();
    if (anyMale.isPresent()) {
        System.out.println("Any male:   " + anyMale.get());
    } else {//from   www  .j a  va 2s.co  m
        System.out.println("No male  found.");
    }
    // Find the first male
    Optional<Employee> firstMale = persons.stream().filter(Employee::isMale).findFirst();
    if (firstMale.isPresent()) {
        System.out.println("First male:   " + anyMale.get());
    } else {
        System.out.println("No male  found.");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();

    Integer totalAgeReduce = roster.stream().map(Person::getAge).reduce(0, (a, b) -> a + b);

    System.out.println("Sum of ages with reduce(identity, accumulator): " + totalAgeReduce);

}

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().max(compByLength)
            .ifPresent(longest -> System.out.println("\nThe longest name is " + longest));
}

From source file:Main.java

public static void main(String[] args) {
    List<Employee> persons = Employee.persons();

    // Check if all persons are males
    boolean allMales = persons.stream().allMatch(Employee::isMale);
    System.out.println("All  males: " + allMales);

    // Check if any person was born in 1971
    boolean anyoneBornIn1971 = persons.stream().anyMatch(p -> p.getDob().getYear() == 1971);
    System.out.println("Anyone born  in 1971:  " + anyoneBornIn1971);

}

From source file:Main.java

public static void main(String[] argv) {
    List<Person> persons = Arrays.asList(new Person("HTML", 12), new Person("Aim", 34), new Person("John", 23));
    persons.stream().map(Person::getFirstName).filter(name -> name.startsWith("A")).limit(10)
            .forEach(name -> System.out.println(name));
}