user1156544
user1156544

Reputation: 1807

Joining Strings via stream

I am trying to join a list of names:

List<String> names;
names = books.stream()
        .map( b -> b.getName() )
        .filter( n -> ( (n != null) && (!n.isEmpty()) ) )
        .collect(Collectors.joining(", "));

This does not compile saying:

incompatible types. inference variable R has incompatible bounds

So after some research, it seems that there is something I misunderstood. I thought that .map( b -> b.getName() ) returned/changed the type to a String, and it seems something is wrong there. If I use .map(Book::getName) instead, I still get an error, but I probably don't fully understand the difference.

However, this does not complain:

List<String> names;
names = books.stream()
        .map( b -> b.getName() )
        .map( Book::getName )
        .filter( n -> ( (n != null) && (!n.isEmpty()) ) )
        .collect(Collectors.joining(", "));

Can someone explain me why? Some didactic explanation about differences between .map( b -> b.getName() ) and .map(Book::getName) are appreciated too, since I think I didn't get it right.

Upvotes: 4

Views: 8440

Answers (3)

Kaplan
Kaplan

Reputation: 3718

if you prefer mapping over map

as String

String names = books.stream().collect(mapping(Book::getName,
    filtering(s -> s != null && ! s.isBlank(),
      joining(", "))));

as List

List<String> names = books.stream().collect(mapping(Book::getName,
    filtering(s -> s != null && ! s.isBlank(),
      toList())));

Upvotes: 0

Manoel Campos
Manoel Campos

Reputation: 1001

If you are using Collectors.joining(), the result will be a single concatenated String:

String names = books.stream()
        .map( b -> b.getName() )
        .filter(n -> (n != null) && !n.isEmpty())
        .collect(Collectors.joining(", "));

The Collectors.toList() is the one that returns a List:

List<String> namesList = books.stream()
        .map( b -> b.getName() )
        .filter(n -> (n != null) && !n.isEmpty())
        .collect(Collectors.toList());

Book::getName is a method reference and will have the same result as b -> b.getName(). Method reference is clearer and enables to pass other existing methods as a parameter to methods such as map(), as long as the passed method conforms to the signature of the expected functional interface. In this case, map() expects an instance of the Function interface. Thus, you can give any reference to a method that conforms to the signature of the abstract R apply(T t) method from such an interface.

Since you are mapping a Book to a String, the actual signature for the method to be given to the map() must be String apply(Book t). This can be read as "receive a Book and return a String". This way, any method you pass that conforms to this definition is valid. When you pass a method reference Book::getName, the getName method itself doesn't conform to the signature presented above (because it has no parameter at all), but it conforms to the definition of such a signature: you pass a book and return a String from its name.

Thus, consider that, inside the class where you have your book list, you also have a method which performs any operation over a Book, returning a String. The method below is an example that receives a Book and gets the first 10 characters from its name:

public String getReducedBookName(Book b){
  if(b.getName() == null)
     return "";

  String name = b.getName();
  return name.substring(0, name.length() > 10 ? 10 : name.length());
}

You can also pass this method (which is not inside the Book class) as parameter to the map() method:

String names = books.stream()
            .map(this::getReducedBookName)
            .filter(n -> !n.isEmpty())
            .collect(Collectors.joining(", "));

Upvotes: 4

cybersoft
cybersoft

Reputation: 1473

The joining(", ") collector will collect and join all Strings into a single string using the given separator. The returning type of collect in this case is String, but you are trying to assign the result to a List. If you want to collect Strings into a List, use Collectors.toList().

If you have a collection with Book instances, then it will be enough to map a stream of Books to a stream of Strings once.

Difference between lamdba & method refrence

  • A lamdba expression may be written as a block, containing multiple operations:

    b -> {
        // here you can have other operations
        return b.getName(); 
    }
    

    if lambda has single operation, it can be shortened:

    b -> b.getName()
    
  • Method reference is just a "shortcut" for a lambda with a single operation. This way:

    b -> b.getName()
    

    can be replaced with:

    Book::getName
    

    but if you have a lambda like this:

    b -> b.getName().toLowerCase()
    

    you cant use a reference to the getName method, because you are making and additional call to toLowerCase().

Upvotes: 7

Related Questions