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
🌐
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 ) ); } Yeah, it's bad, but every project has one :) #string · #java · #join · #stream · Say Thanks ·
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › join or append stream elements in java
Join or Append Stream Elements in Java
December 13, 2024 - Stream<String> stringStream = ... " + result); // Output: Hello, Java, Streams · The String.join() is another simple method for concatenating strings....
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(", "));
🌐
Javaprogramto
javaprogramto.com › 2020 › 12 › java-8-stream-join-string.html
Java 8 Streams - Join or Append Stream of Strings using Collectors.joining() JavaProgramTo.com
November 26, 2021 - A quick guide to learn how to join stream of strings in java 8 with a delimiter or separator using Collectors.joining() api method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-8-streams-collectors-joining-method-with-examples
Java 8 Streams | Collectors.joining() method with Examples - GeeksforGeeks
January 22, 2026 - The Collectors.joining() method in Java 8 Streams is a terminal operation used to combine stream elements into a single String.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › StringJoiner.html
StringJoiner (Java Platform SE 8 )
July 21, 2026 - A StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence).
🌐
Baeldung
baeldung.com › home › java › java streams › string operations with java streams
String Operations with Java and Stream API | Baeldung
January 8, 2024 - Java 8 has introduced a new Stream API that lets us process data in a declarative manner. In this quick article, we would learn how to use the Stream API to split a comma-separated String into a list of Strings and how to join a String array into a comma-separated String.
Find elsewhere
🌐
Medium
medium.com › javarevisited › nuanced-string-joining-in-java-7db547c331d0
Nuanced String Joining in Java | Javarevisited
April 23, 2024 - The append method in StringBuilder is nice enough to call String.valueOf on each Integer value for us so we don’t have to. In the end, we assert we wind up with a String that matches this string “*1*, *2*, *3*, *4*, *5*”. For the next implementation, we will use a Stream and Collectors.joining to create the String.
🌐
Medium
medium.com › @AlexanderObregon › javas-collectors-joining-method-explained-a4adb120b785
Java’s Collectors.joining() Method Explained | Medium
November 2, 2024 - The Collectors.joining() method provides a simple and effective way to combine stream elements into a formatted string. From joining user names to building file paths and query strings, its flexibility makes it a valuable tool for any Java developer.
🌐
Rip Tutorial
riptutorial.com › joining a stream to a single string
Java Language Tutorial => Joining a stream to a single String
The Collectors.joining() method can be used for this, like in the following example: Stream<String> fruitStream = Stream.of("apple", "banana", "pear", "kiwi", "orange"); String result = fruitStream.filter(s -> s.contains("a")) .map(String::...
🌐
Medium
nicksamoylov.medium.com › java-streams-29-collect-5-string-join-and-collectors-joining-668bca0d6079
Java streams 29. Collect 5. String.join() and Collectors.joining() | by Nick Samoylov | Medium
August 23, 2020 - String str = Stream.of("cat", "fish", "dog") .collect(Collectors .joining(", ", "Animals: ", ", and others.")); System.out.print(str); /prints: Animals: cat, fish, dog, and others. This way it is definitely easier to do than using the reduce() operation demonstrated in the previous post. Since Java 8, all collections and many other objects acquired the method that converts them to a Stream, so you can enjoy now this easy way to create a String with comma-separated values.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-string-stream-to-join-them-in-java
How to convert string Stream to join them in Java?
July 30, 2019 - The following is an example to convert string Stream to join them: import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<String> stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming"); final String str = stream.collect(Collectors.joining(" ")); System.out.println("Join result...\n"+str); } }
🌐
Davidvlijmincx
davidvlijmincx.com › home › java › java stream join lists
Java Stream join lists
March 10, 2022 - The easiest way to join two lists with Streams is by using the Stream.concat(Stream a, Stream b) method. The method takes two Stream instances as parameters and puts the second Stream behind the first Stream. The result is a new list of strings with the content of both lists.
🌐
Wordpress
ivarconr.wordpress.com › 2013 › 11 › 20 › java-8-joining-strings-with-stream-api
Java 8: joining strings with Stream API | Developer writing about stuff
November 20, 2013 - This problem is really easy to solve using the new Stream API, as shown in code example above. The key to the solution is the joining-collector provided as a ready to use Collector. The joining-collector uses StringBuilder under the hood, to build up the resulting String. How would the solution look like with imperative styled for-loops? How many garbage variables would you need? The example touches multiple new concepts, such as filter, sorted, map, collect, introduced in the Java 8 Stream API.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › StringJoiner.html
StringJoiner (Java SE 11 & JDK 11 )
January 20, 2026 - A StringJoiner may be employed to create formatted output from a Stream using Collectors.joining(CharSequence).
🌐
Blogger
javarevisited.blogspot.com › 2016 › 06 › 10-examples-of-joining-string-in-java-8.html
10 Examples of Joining String in Java - String.join vs StringJoiner
May 11, 2023 - JDK 8 also provides a joining Collector, which you can use to join String from a Stream, as shown below.
🌐
Java Guides
javaguides.net › 2024 › 09 › how-to-convert-stream-to-string-in-java.html
How to Convert Stream to String in Java 8
September 10, 2024 - Step 1: A stream of strings is created using Stream.of(). Step 2: The Collectors.joining() method is used to concatenate the elements without any delimiter. Step 3: The resulting string is printed. import java.util.stream.Stream; import ...
🌐
Medium
medium.com › @ravindra.cse.tu › java-8-streams-joining-strings-with-delimiters-e641c6b42280
Java 8 Streams: Joining Strings with Delimiters | by Ravindra K Yadav | Medium
November 24, 2024 - Delimiter (“,”) to separate elements in the joined string. Prefix (“[“) to be added before the first element. Suffix (“]”) to be added after the last element.