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
🌐
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.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › join or append stream elements in java
Join or Append Stream Elements in Java
December 13, 2024 - For example, if we use a comma as a delimiter, then this method will result in a comma-separated string. import java.util.stream.Stream; Stream<String> stringStream = Stream.of("Hello", "World", "Java", "Streams"); // Joining with a delimiter ...
🌐
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. ... package com.mkyong; import java.util.Arrays; import java.util.List; public class JavaStringExample1 { public static void main(String[] args) { List<String> list ...
🌐
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 - Prints the resulting joined string to the console. ... The list elements are joined with a comma (“,”) as the delimiter, enclosed within square brackets (“[]”) as specified.
🌐
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 - Let’s take the following list ... utilities for String processing and provides the conversion method join(): String commaSeparatedString = String.join(",", arraysAsList); assertThat(commaSeparatedString).isEqualTo("ONE,TW...
🌐
Java67
java67.com › 2016 › 08 › java-8-stringjoiner-example-how-to-join-String.html
Java 8 StringJoiner Example - How to join multiple Strings with delimiter in Java? | Java67
You can further see these Java Functional Programming and Stream Courses to learn more about useful features like Lambda expression and Stream in a quick time. Let's see our first example, which will join String by a comma to create a CSV String in Java 8 using the StringJoiner class.
🌐
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 - You can leverage Java 8 Collectors to concatenate some objects into a string separated by a delimiter: ... List<Integer> numbers = Arrays.asList( 4, 8, 15, 16, 23, 42 ); return numbers.stream() .map( n -> n.toString() ) .collect( Collectors.joining( ...
🌐
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.
Find elsewhere
🌐
Medium
medium.com › javarevisited › nuanced-string-joining-in-java-7db547c331d0
Nuanced String Joining in Java | Javarevisited
April 23, 2024 - I wrote an article for 97 Things ... through some example Java code using Collectors.joining() and String.join() to create a comma separated list of names....
🌐
StackHowTo
stackhowto.com › home › java › how to join list string with commas in java
How To Join List String With Commas In Java - StackHowTo
October 9, 2021 - If you are forced to use an older version of Java, and don’t want or can’t use a library like Google Guava or Apache Commons, you have to write a method yourself. /** * * @param delimiter: String that should separate the strings * @param strings: strings that should be joined * @return the joined string */ public static String join(String delimiter, 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 - If the values are presented as a fixed set of String values, we can use java.util.StringJoiner class or String.jojn() method: StringJoiner joiner = new StringJoiner(", ") .add("cat").add("fish").add("dog"); System.out.print(joiner); //prints: ...
🌐
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.
🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › convert-a-set-of-string-to-a-comma-separated-string-in-java
Convert a Set of String to a comma separated String in Java
September 23, 2022 - import java.util.*; import java.util.stream.Collectors; public class JavaExample { public static void main(String args[]) { // Set of Strings Set<String> animalSet = new HashSet<>(); animalSet.add("Lion"); animalSet.add("Tiger"); animalSet.add("Monkey"); animalSet.add("Rabbit"); // Displaying Set elements System.out.println("Set of Strings: " + animalSet); // Using Stream API for conversion // collect() method returns the result of the // intermediate operations performed on the stream String str = animalSet.stream().collect( Collectors.joining(",")); // Display the Comma separated String as output System.out.println("Comma Separated String: "+ str); } } ... I have 15 years of experience in the IT industry, working with renowned multinational corporations.
🌐
Attacomsian
attacomsian.com › blog › java-convert-list-to-comma-separated-string
Convert a list to a comma-separated string in Java
October 29, 2022 - The most common way to convert a list of strings into a comma-separated string is by using the static join() method from the String class: List<String> list = List.of("?", "?", "?", "?", "?"); String str = String.join(",", list); System.out...
🌐
YouTube
youtube.com › adam bien
Collectors.joining for Comma Separated Strings #java #shorts - YouTube
Collectors::joining https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/util/stream/Collectors.html#joining(java.lang.CharSequence) does the jo...
Published: December 14, 2022
Views: 1K
🌐
How to do in Java
howtodoinjava.com › home › java array › join or concatenate strings with comma in java
Join or Concatenate Strings with Comma in Java
February 22, 2023 - In the second example, we are joining a string array with a comma delimiter. String[] strArray = { "How", "To", "Do", "In", "Java" }; String joinedString = StringUtils.join(strArray); System.out.println(joinedString); //HowToDoInJava String joinedString2 = StringUtils.join(strArray, ","); System.out.println(joinedString2); //How,To,Do,In,Java
🌐
Medium
medium.com › @AlexanderObregon › javas-collectors-joining-method-explained-a4adb120b785
Java’s Collectors.joining() Method Explained | Medium
November 2, 2024 - These include tasks such as combining user names into a readable string, constructing file paths, and assembling query parameters for URLs. Imagine you’re tasked with displaying a list of user names in a comma-separated format for display in a report or on a user interface. Instead of manually iterating through the list and concatenating the names, Collectors.joining() provides a cleaner and more readable solution. ... import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class UserNameJoiner { public static void main(String[] args) { List<String> users = Arrays.asList("Mason", "Chloe", "Liam", "Sophie"); String userDisplay = users.stream() .collect(Collectors.joining(", ", "User List: ", ".")); System.out.println(userDisplay); // Output: User List: Mason, Chloe, Liam, Sophie.