camelCaseUser
camelCaseUser

Reputation: 27

How do I print out this stream?

library.stream()
             .map(book -> book.getAuthor())
             .filter(author -> author.getAge() >= 50)
             .map(Author::getLastName)
             .limit(10)
             .collect(Collectors.toList());

How Do I print out the list? I've tried

System.out.println(Collectors.toList());

but it gives me

 java.util.stream.Collectors$CollectorImpl@4ec6a292

Upvotes: 2

Views: 616

Answers (5)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

You need to either get this expression assigned to some list like this,

List<String> lastNameList = library.stream()
             .map(book -> book.getAuthor())
             .filter(author -> author.getAge() >= 50)
             .map(Author::getLastName)
             .limit(10)
             .collect(Collectors.toList());

And then print it using,

System.out.println(lastNameList);

OR you can directly print it like this,

System.out.println(library.stream()
             .map(book -> book.getAuthor())
             .filter(author -> author.getAge() >= 50)
             .map(Author::getLastName)
             .limit(10)
             .collect(Collectors.toList()));

You're actually doing this,

System.out.println(Collectors.toList());

Which has nothing to print except an empty object of type Collectors, which is why you are seeing this,

java.util.stream.Collectors$CollectorImpl@4ec6a292

Upvotes: 3

Naman
Naman

Reputation: 32008

You can make use of Stream.peek to print the list of lastNames of the authors above 50 yrs of age as following :

List<Book> library = List.of(new Book(new Author("overflow", 100)), 
                             new Book(new Author("stack", 80)), 
                             new Book(new Author("nullpointer", 49)));

// you were ignoring the result of collect
List<String> lastNames = library.stream()
            .map(Book::getAuthor)
            .filter(author -> author.getAge() >= 50)
            .map(Author::getLastName)
            .limit(10)
            .peek(System.out::println) // this would print "overflow" and "stack"
            .collect(Collectors.toList());

Also, note if your prime intention is just to print the names and not store them, you can simply use forEach instead of collect, both of them are terminal operations just that collect has a return type based on the type of Stream while forEach is void :-

library.stream()
       .map(Book::getAuthor)
       .filter(author -> author.getAge() >= 50)
       .map(Author::getLastName)
       .limit(10)
       .forEach(System.out::println);

All of the above, considering the objects in use to being similar to the following

class Book {
    Author author;

    Book(Author author) {
        this.author = author;
    }

    // ... getters
}

class Author {
    String lastName;
    int age;

    Author(String lastName, int age) {
        this.lastName = lastName;
        this.age = age;
    }

    // ... getters
}

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 40078

Use foreach() method in List

library.stream()
    .map(book -> book.getAuthor())
    .filter(author -> author.getAge() >= 50)
    .map(Author::getLastName)
    .limit(10)
    .forEach(System.out::println);

If you want to print collected list here is an example

List<Integer> l = new ArrayList<>();
l.add(10);
l.add(20);
l.forEach(System.out::println);

Upvotes: 2

Locke
Locke

Reputation: 8972

You need to use Arrays.toString(SomeArray) or some other method such as a for loop.

When outputting something that does not implement toString(), java will just output it in the unreadable format of Class$Subclass@LocationInMemory, which doesn't exactly help much.

Upvotes: 1

Sandeepa
Sandeepa

Reputation: 3755

This example will give you an idea,

public class A {

    public static void main(String[] args) throws IOException {
        Stream<String> s = Stream.of("a", "b", "c");
        List<String> names = s.collect(Collectors.toList());
        System.out.println(names);
    }

}

Upvotes: 1

Related Questions