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().

Answer from cybersoft on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_join.asp
Java String join() Method
The join() method joins one or more strings with a specified separator.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. For example, List<String> strings = new LinkedList<>(); strings.add("Java");strings.add("is"); strings.add("cool"); String message = String.join(" ", strings); //message ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-join-examples
Java String join() with examples - GeeksforGeeks
April 8, 2025 - public static String join(CharSequence deli, CharSequence... ele) and public static String join (CharSequence deli, Iterable<? extends CharSequence> ele) Parameters: deli- delimiter to be attached with each element ele- string or char to be attached with delimiter Returns : string joined with delimiter. ... // Java program to demonstrate // working of join() method class GfG { public static void main(String args[]) { // delimiter is "<" and elements are "Four", "Five", "Six", "Seven" String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven"); System.out.println(gfg1); } }
🌐
Baeldung
baeldung.com › home › java › java string › concatenating strings in java
Concatenating Strings in Java | Baeldung
May 8, 2025 - StringJoiner abstracts all of the String.join functionality into a simple to use class. The constructor takes a delimiter, with an optional prefix and suffix.
🌐
Medium
medium.com › javarevisited › nuanced-string-joining-in-java-7db547c331d0
Nuanced String Joining in Java | Javarevisited
April 23, 2024 - Here we use the ArrayAdapter, convert it to a LazyIterable (which is an Iterable) and collect String instances using String::valueOf. The most surprising thing I didn’t know about String.join is that there are no public overloads that take a prefix and suffix, like Collectors.joining has. We can also use Stream to turn the Integer array into an Iterable<String>.
🌐
CodeGym
codegym.cc › java blog › strings in java › java string join() method
Java String join() Method
January 8, 2025 - This method takes a delimiter as a first parameter and an iterable as a second parameter whose elements must implement CharSequence and will be joined. public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-join-method
Java String join() method
June 4, 2024 - The join() method of String class is introduced in Java 8. This method is used to concatenate strings with specified delimiter. It is also used to combine multiple elements of a collection with the specified separator.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.join
String.Join Method (Java.Lang) | Microsoft Learn
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
Top answer
1 of 3
12

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().

2 of 3
6

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(", "));
🌐
Reddit
reddit.com › r/java › in java 8 we can finally join strings
r/java on Reddit: In Java 8 we can finally join strings
March 22, 2014 - Well, now we can quit all that smelly Util.join variations with String.join. Not that I'm going to run. ... And here is where I roll my eyes at silly Java programmers...
🌐
Medium
medium.com › @AlexanderObregon › javas-string-join-method-explained-77e1503095ec
Java’s String.join() Method Explained | Medium
September 2, 2024 - The String.join() method is one of the most useful utilities introduced in Java 8. It allows developers to combine a sequence of strings into a single string, with each element separated by a specified delimiter.
🌐
Programiz
programiz.com › java-programming › library › string › join
Java String join()
Note: join() is a static method. You do not need to create a string object to call this method. Rather, we call the method using the class name String.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › join or append stream elements in java
Join or Append Stream Elements in Java
December 13, 2024 - joining() – the input elements are concatenated into a String, in encounter order. joining(delimiter) – the input elements are concatenated into a String, separated by the specified delimiter, in encounter order.
🌐
Coderanch
coderanch.com › t › 706702 › java › String-replace-join
String replace/join (Beginning Java forum at Coderanch)
February 25, 2019 - Well, it seems to me that, even if you were using it correctly, you don't need join(), because your loop - at least up to line 18 - appears to be replacing every third ',' correctly. And that means that when it's finished, you will have an array with the correct characters already in it, so you can create a new String with: new String(ch); No need for join() at all - which, to be honest, is probably not what you wanted anyway.
🌐
Coderwall
coderwall.com › p › im4lja › joining-objects-into-a-string-with-java-8-stream-api
Joining Objects into a String with Java 8 Stream API (Example)
July 17, 2023 - public static String join( Collection collection, String delimiter ) { return collection.stream() .map( Object::toString ) .collect( Collectors.joining( delimiter ) ); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-concat-examples
Java String concat() Method with Examples - GeeksforGeeks
Note: Strings are immutable in Java, so every concatenation creates a new string object instead of modifying the existing one.
Published: April 7, 2026
🌐
TutorialsPoint
tutorialspoint.com › article › join-strings-in-java
Join Strings in Java
March 11, 2026 - To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element. Let’s say we want to join the strings “Demo” and “Text”.
🌐
DEV Community
dev.to › satyam_gupta_0d1ff2152dcc › master-the-java-string-join-method-a-2025-guide-with-examples-best-practices-2oem
Master the Java String join() Method: A 2025 Guide with Examples & Best Practices - DEV Community
November 6, 2025 - Okay, cool, it joins strings. But where does this fit in a real project? Everywhere! Building CSV Data Need to generate a CSV line from a list of data? join() is your best friend. java List userData = List.of("John Doe", "john.doe@email.com", "New York"); String csvLine = String.join(",", userData); // Output: John Doe,john.doe@email.com,New York
🌐
Mkyong
mkyong.com › home › java › java – how to join list string with commas
Java – How to join List String with commas | mkyong.com
November 29, 2018 - In Java, we can use String.join(",", list) to join a List String with commas.