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("Bob", "Tom", "Jeff", "Scott", "Jennifer", "Steve");

    Optional<String> firstS = names.stream().filter(name -> name.startsWith("S")).findFirst();
    System.out.println(firstS.orElse("None found"));

    Optional<String> firstZ = names.stream().filter(name -> name.startsWith("Z")).findFirst();
    if (firstZ.isPresent()) {
        System.out.println(firstZ.get());
    } else {//  w  ww  . ja v a  2 s  . co m
        System.out.println("No Z's found");
    }
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> roster = createRoster();
    roster.stream().forEach(p -> p.printPerson());

}

From source file:Main.java

public static void main(String[] args) {

    List<String> names = Arrays.asList("CSS", "CSS3", "HTML", "Java", "Javascript", "Oracle", "MySQL");

    String name5 = names.stream().map(Main::toUpperCase).filter(Main::greaterThanFive).limit(2)
            .collect(Collectors.joining(" + ")).toString() + " = WSM!!!";

    System.out.println(name5);/*from w w w  .  j av  a2  s.c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> persons = Arrays.asList(new Person("FooBar", 12), new Person("BarFoo", 16));
    Stream<Integer> stream = persons.stream().map(x -> x.getAge());
    int sum = stream.reduce(0, (l, r) -> l + r);
    System.out.println(sum);//  w ww. ja  v a  2 s  . com
}

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

    argList = argList.stream().filter(isHelp.negate()).collect(Collectors.toList());

    System.out.println(argList);/* w ww.  j  a  va  2 s. c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    List<Employee> persons = Employee.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) {
    List<String> names = Arrays.asList("XML", "HTML", "CSS", "Javascript", "Java", "Static");

    Optional<String> longestName = names.stream()
            .reduce((name1, name2) -> name1.length() >= name2.length() ? name1 : name2);

    if (longestName.isPresent()) {
        System.out.println(longestName.get());
    } else {//from   w w w . java  2  s .  c  om
        System.out.println("WTF?");
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<Trade> trades = TradeUtil.createTrades();
    trades.stream().filter(t -> t.getIssuer().equals("IBM")).forEach(System.out::println);

    System.out.println();//  ww  w . java 2 s  . co  m
    trades.stream().filter(t -> t.getIssuer().equals("IBM")).filter(t -> t.getQuantity() > 15)
            .forEach(x -> System.out.println("IBM trades, qty over 15 " + x));

    System.out.println();
    Consumer<Trade> printTrade = x -> System.out.println(x);
    trades.stream().filter(t -> t.getIssuer().equals("IBM")).filter(t -> t.getQuantity() > 15)
            .filter(t -> t.getStatus().equals("NEW")).forEach(printTrade);
}

From source file:Main.java

public static void main(String args[]) {
    List<Person> people = Arrays.asList(new Person("B", 25, "Main Street"), new Person("A", 27, "Off Street"));
    List<String> listNames = people.stream().map(u -> u.getName()).collect(Collectors.toList());
    System.out.println(listNames);
}

From source file:Main.java

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

    Employee[] men = persons.stream().filter(p -> p.getGender() == Gender.MALE).toArray(Employee[]::new);

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