Each iteration of your for loop overwrites the value of separatedByComma. The first iteration assigns the String "a" to it, and the second replaces it with "b".

You should Stream over the elements of the strings List in order to join the Strings that match each of them into a single output String:

String separatedByComma = 
    strings.stream()
           .flatMap(word -> occList.stream()
                                   .filter(occ -> word.equalsIgnoreCase (occ.getOccSCd()))
                                   .map (occ -> occ.getOccSNm()))
           .collect(Collectors.joining (","));

Output:

a,b
Answer from Eran on Stack Overflow
🌐
W3docs
w3docs.com › home › code snippets › java › using streams to convert a list of objects into a string obtained from the tostring method
Using streams to convert a list of objects into a string obtained from the toString method | W3docs
This will create a stream of the list, apply the toString method to each element of the stream, and then collect the resulting stream of strings into a single string, separated by commas.
🌐
DZone
dzone.com › coding › java › convert a list to a comma-separated string in java 8
Convert a List to a Comma-Separated String in Java 8 - DZone
June 29, 2016 - This tutorial demonstrates how to use streams in Java 8 and Java 7 to convert a list to a comma-separated string by manipulating the string before joining.
🌐
Baeldung
baeldung.com › home › java › java string › convert a list to a comma-separated string
Convert a List to a Comma-Separated String | Baeldung
July 2, 2025 - You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence. Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time. ... List conversion is still a hot topic as it’s something that we do very often as Java developers. In this tutorial, we’ll learn how to convert a List of String to a comma-separated String using four different approaches.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 - Converting a List to String with Examples - Java Code Geeks
July 5, 2021 - List.toString() is the simplest one but it adds the square brackets at the start and end with each string is separated with comma separator. The drawback is that we can not replace the comma with another separator and can not remove the square ...
🌐
Java2s
java2s.com › Tutorials › Java › Stream_How_to › Stream › Collect_List_of_Integer_to_a_String_with_separator.htm
Java Stream How to - Collect List of Integer to a String with separator
Back to Stream ↑ · We would like to know how to collect List of Integer to a String with separator. /*ww w . ja v a 2 s . c o m*/ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] argv) { List< Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7); System.out.println(numbers.stream().map(number -> String.valueOf(number)) .collect(Collectors.joining(", "))); } } The code above generates the following result.
🌐
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 - #stream · You can leverage Java 8 Collectors to concatenate some objects into a string separated by a delimiter: For example: List<Integer> numbers = Arrays.asList( 4, 8, 15, 16, 23, 42 ); return numbers.stream() .map( n -> n.toString() ) .collect( ...
🌐
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 - package com.program.java8; import ... terminal operation with the Collectors.joining method. ... Delimiter (“,”) to separate elements in the joined string....
🌐
Sabe
sabe.io › blog › java-convert-list-comma-separated-string
How to Convert a List to Comma-Separated String in Java | Sabe
May 25, 2022 - In this post, we learned two different methods to convert a list of strings to a single comma-separated string. Your options are to use the join() method or the collect() method on the Stream class. Thanks for reading!
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › java-convert-list-to-comma-separated-string
Convert a list to a comma-separated string in Java
October 29, 2022 - List<String> list = List.of("?", "?", "?", "?", "?"); String str = list.stream().collect(Collectors.joining(",")); System.out.println(str); // ?,?,?,?,? Finally, the last way to convert a list of strings into a comma-separated string is by using the Apache Commons Lang’s StringUtils.join() method: List<String> list = List.of("?", "?", "?", "?", "?"); String str = StringUtils.join(list, ','); System.out.println(str); // ?,?,?,?,?
🌐
Reverse Coding
reversecoding.net › java-8-convert-list-string-comma
Java 8 - Convert List to String comma separated | Reverse Coding
May 22, 2016 - Note: you can statically import java.util.stream.Collectors.joining if you prefer just typing joining(“,”). For the old times’ sake, let’s have a look at Java 7 implementation. private static final String SEPARATOR = ","; public static void main(String[] args) { List<String> cities = Arrays.asList( "Milan", "London", "New York", "San Francisco"); StringBuilder csvBuilder = new StringBuilder(); for(String city : cities){ csvBuilder.append(city); csvBuilder.append(SEPARATOR); } String csv = csvBuilder.toString(); System.out.println(csv); //OUTPUT: Milan,London,New York,San Francisco, //Remove last comma csv = csv.substring(0, csv.length() - SEPARATOR.length()); System.out.println(csv); //OUTPUT: Milan,London,New York,San Francisco
🌐
javathinking
javathinking.com › blog › java-8-convert-list-to-string-with-separator
Java 8: Convert List to String with Separator — javathinking.com
Java 8's Stream API and the Collectors.joining() method provide a convenient and functional way to convert a list to a string with a separator. By understanding the core concepts, typical usage scenarios, common pitfalls, and best practices, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-a-list-of-string-to-a-comma-separated-string-in-java
Convert a List of String to a comma separated String in Java - GeeksforGeeks
January 23, 2026 - In Java, a List<String> can be efficiently converted into a comma-separated String using built-in utilities like String.join(), which concatenates list elements with a specified delimiter while ensuring clean, readable, and loop-free code.
🌐
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 - Along with the joining, this method does the conversion from java 8 stream to string object with the default delimiter. Collectors.joining() method does concatenate the list of values or list of strings or stream of values or strings into a ...
🌐
Techie Delight
techiedelight.com › home › java › convert a list to comma-separated string in java
Convert a List to Comma-Separated String in Java | Techie Delight
July 7, 2026 - This post will discuss how to convert a List to a comma-separated String in Java... Since Java 8, you can use the String.join() method that joins strings together with a specified separator.
🌐
Baeldung
baeldung.com › home › java › java string › converting a list to string in java
Converting a List to String in Java | Baeldung
July 30, 2026 - Get started with understanding multi-threaded applications with our Java Concurrency guide: ... Since its introduction in Java 8, the Stream API has become a staple of Java development.
🌐
Kokaruk
kokaruk.com › 2017 › 09 › 15 › quickly-convert-list-of-ints-to-string-using-streams
Convert List <Integer> to comma separated string · class Rock implements Throwable
April 11, 2023 - Originally published on 2017-09-14 Java List interface doesn’t have a method to join all members to coma separated string. Previous versions required iterating over the list and appending and instance of Stringbuilder. Using Java 8 Streams makes it more neat. https://gist.github.com/rmit-s3668804-Dzmitry-Kakaruk/7cb0f29c147cfb99e503f504ba3787f5
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-arraylist-to-comma-separated-string-in-java
Convert ArrayList to Comma Separated String in Java - GeeksforGeeks
July 23, 2025 - In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows: Earlier before Java 8 there were only standard methods available in order for this conversion but with the introduction to the concept of Streams and ...
🌐
Java67
java67.com › 2018 › 10 › how-to-convert-list-or-set-of-string-to-comma-separated-string-in-java-csv.html
How to Convert a List of String to Comma Separated String (CSV) in Java 8? Example | Java67
If you don't want null, it's better to remove any nulls before joining strings from the list. By the way, Java 8 is full of such excellent features which are often get ignored in the limelight of lambda expression, Stream API, new Date Time API, and other significant features. Here is an example of converting a list and set to a comma-separated String in Java 8: