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<Person> roster = createRoster();

    System.out.println("Male members of the collection (bulk data operations):");
    roster.stream().filter(e -> e.getGender() == Person.Sex.MALE).forEach(e -> System.out.println(e.getName()));

}

From source file:Main.java

public static void main(String[] args) {
    Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

    Comparator<Integer> normal = Integer::compare;
    Comparator<Integer> reversed = normal.reversed();
    Collections.sort(listOfIntegers, reversed);

    listOfIntegers.stream().forEach(e -> System.out.print(e + " "));
    System.out.println("");

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    List<Foo> foos = new ArrayList<>();

    IntStream.range(1, 4).forEach(num -> foos.add(new Foo("Foo" + num)));

    foos.forEach(f -> IntStream.range(1, 4).forEach(num -> f.bars.add(new Bar("Bar" + num + " <- " + f.name))));

    foos.stream().flatMap(f -> f.bars.stream()).forEach(b -> System.out.println(b.name));
}

From source file:Main.java

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

    System.out.println("Members by gender:");
    Map<Person.Sex, List<Person>> byGender = roster.stream().collect(Collectors.groupingBy(Person::getGender));

    List<Map.Entry<Person.Sex, List<Person>>> byGenderList = new ArrayList<>(byGender.entrySet());

    byGenderList.stream().forEach(e -> {
        System.out.println("Gender: " + e.getKey());
        e.getValue().stream().map(Person::getName).forEach(f -> System.out.println(f));
    });/*from w  ww  . j a  v  a 2  s.  c  o m*/

}

From source file:Main.java

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

    int cpus = Runtime.getRuntime().availableProcessors();
    long start = System.currentTimeMillis();
    trades.stream().parallel().forEach(t -> doSomething(t));
    long end = System.currentTimeMillis();

    System.out.println("Time for parallel op:" + (end - start) / 1000 + "sec on " + cpus);
}

From source file:Main.java

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

    List<Person> list = persons.stream().filter(p -> p.getAge() > 18).collect(Collectors.toList());
    list.forEach(p -> System.out.println(p.getFirstName()));

}

From source file:Main.java

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

    System.out.println("Total age by gender:");
    Map<Person.Sex, Integer> totalAgeByGender = roster.stream().collect(
            Collectors.groupingBy(Person::getGender, Collectors.reducing(0, Person::getAge, Integer::sum)));

    List<Map.Entry<Person.Sex, Integer>> totalAgeByGenderList = new ArrayList<>(totalAgeByGender.entrySet());

    totalAgeByGenderList.stream()//from  ww  w .  j  a  va  2  s.com
            .forEach(e -> System.out.println("Gender: " + e.getKey() + ", Total Age: " + e.getValue()));

}

From source file:Main.java

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

    System.out.println("Names by gender:");
    Map<Person.Sex, List<String>> namesByGender = roster.stream().collect(
            Collectors.groupingBy(Person::getGender, Collectors.mapping(Person::getName, Collectors.toList())));

    List<Map.Entry<Person.Sex, List<String>>> namesByGenderList = new ArrayList<>(namesByGender.entrySet());

    namesByGenderList.stream().forEach(e -> {
        System.out.println("Gender: " + e.getKey());
        e.getValue().stream().forEach(f -> System.out.println(f));
    });/* w ww.  j  a v a2  s.  c o  m*/

}

From source file:org.neo4j.nlp.examples.wikipedia.main.java

public static void main(String[] args) throws IOException {
    List<Map<String, Object>> results = getWikipediaArticles();

    results = results.stream().filter(a -> !a.get("title").toString().trim().isEmpty())
            .collect(Collectors.toList());
    //System.out.println(results);
    // Train model
    results.forEach(row -> {/*from  ww w  . j a  va2 s  .  co m*/
        System.out.println("Training on '" + row.get("title").toString() + "'");
        String articleText = getArticleText(row.get("title").toString());
        if (articleText != null) {
            trainOnText(new String[] { (String) articleText }, new String[] { (String) row.get("title") });
        }

    });

    //System.out.println(results);
    //        try {
    //            Thread.sleep(10);
    //        } catch (InterruptedException e) {
    //            e.printStackTrace();
    //        }
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

    List<Integer> serialStorage = new ArrayList<>();

    System.out.println("Serial stream:");
    listOfIntegers.stream()

            // Don't do this! It uses a stateful lambda expression. 
            .map(e -> {/*from   w w w  . j  a  v  a2s  .c  o  m*/
                serialStorage.add(e);
                return e;
            })

            .forEachOrdered(e -> System.out.print(e + " "));
    System.out.println("");

    serialStorage.stream().forEachOrdered(e -> System.out.print(e + " "));
    System.out.println("");

    System.out.println("Parallel stream:");
    List<Integer> parallelStorage = Collections.synchronizedList(new ArrayList<>());
    listOfIntegers.parallelStream()

            // Don't do this! It uses a stateful lambda expression.
            .map(e -> {
                parallelStorage.add(e);
                return e;
            })

            .forEachOrdered(e -> System.out.print(e + " "));
    System.out.println("");

    parallelStorage.stream().forEachOrdered(e -> System.out.print(e + " "));
    System.out.println("");

}