I have the following code using java Stream
.
I am trying the get the function to build a string of value: "a,b" in this case.
however, the output (separatedByComma
in this case) is always "b".
Could somebody shed some light please?
@Test
public void testJoin() {
List<MOccS> occList = new ArrayList<> ( );
MOccS mOccS = new MOccS ();
mOccS.setOccSCd ( "1" );
mOccS.setOccSNm ( "a" );
occList.add ( mOccS );
MOccS mOccS2 = new MOccS ();
mOccS2.setOccSCd ( "2" );
mOccS2.setOccSNm ( "b" );
occList.add ( mOccS2 );
List<String> strings = new ArrayList<> ( );
strings.add ( "1" );
strings.add ( "2" );
String separatedByComma = "";
for(String word: strings) {
separatedByComma = occList.stream ()
.filter ( occ -> word.equalsIgnoreCase ( occ.getOccSCd () ) )
.map ( occ -> occ.getOccSNm () )
.collect ( Collectors.joining ( "," ) );
}
System.out.println (separatedByComma);
}
class MOccS{
String occSCd;
String occSNm;
...
getter/setter
...
}