Example usage for java.util.stream Collectors toList

List of usage examples for java.util.stream Collectors toList

Introduction

In this page you can find the example usage for java.util.stream Collectors toList.

Prototype

public static <T> Collector<T, ?, List<T>> toList() 

Source Link

Document

Returns a Collector that accumulates the input elements into a new List .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Path path = Paths.get("E:\\Java_Dev");

    List<Path> files = Files.walk(path).filter(Files::isReadable).filter(Files::isRegularFile).limit(10)
            .collect(Collectors.toList());
    System.out.println(files);//from  w w  w . ja v  a2s .c o  m
}

From source file:Main.java

public static void main(String[] args) {
    List<String> names = Arrays.asList("Chris", "HTML", "XML", "CSS", "");

    Stream<String> s = names.stream().filter(name -> name.startsWith("C"));

    List<String> collect = s.filter(Objects::nonNull).collect(Collectors.toList());
    System.out.println(collect);/*from   ww w .  j a v a  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));

    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[] args) {
    Map<Employee.Gender, List<String>> namesByGender = Employee.persons().stream().collect(Collectors
            .groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.toList())));

    System.out.println(namesByGender);
}

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<String> names = Arrays.asList("Chris", "HTML", "XML", "CSS", "");

    Stream<String> s = names.stream();

    Predicate<String> isEmpty = String::isEmpty;
    Predicate<String> notEmpty = isEmpty.negate();

    System.out.println(s.filter(notEmpty).collect(Collectors.toList()));
}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> result = Stream.of(2, 3, 4, 5).peek(x -> System.out.println("taking from stream: " + x))
            .map(x -> x + 17).peek(x -> System.out.println("after map: " + x)).filter(x -> x % 2 == 0)
            .peek(x -> System.out.println("after filter: " + x)).limit(3)
            .peek(x -> System.out.println("after limit: " + x)).collect(Collectors.toList());

    System.out.println(result);//from  w  ww. j  ava2 s  .  c o m
}

From source file:Main.java

public static void main(String[] argv) {
    List<DayOfWeek> list = new ArrayList<>();

    list = (List<DayOfWeek>) Stream.of(Month.values()).map(month -> LocalDate.now().withYear(2010).with(month)
            .with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek()).collect(Collectors.toList());

    System.out.println(list);//  w w w  .  j av a  2  s .  c  om
}

From source file:Main.java

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

    System.out.println("Names of male members with collect operation: ");
    List<String> namesOfMaleMembersCollect = roster.stream().filter(p -> p.getGender() == Person.Sex.MALE)
            .map(p -> p.getName()).collect(Collectors.toList());

    namesOfMaleMembersCollect.stream().forEach(p -> System.out.println(p));

}

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));
    });//from w  w  w  .  j a v  a2 s. c om

}