I am new to using Lambda functions in Java. I am observing a particular snippet of code used in an application. This snippet has a list in it's input. A lambda function chain is then called upon this list. However without applying any sorting operation, the sequence of elements in the output is changed. Can somebody please explain why would this happen? Attaching the code below:-
public static void main( String[] args ) {
final Map<String, List<Channel>> sdpIdToChannels =new HashMap<String, List<Channel>>();
final List<Channel> channels=new ArrayList<Channel>();
Channel channel1=new Channel();
channel1.setMeterId( "49e10e02-b959-495d-8dca-5922bbeeaefd" );
Channel channel2=new Channel();
channel2.setMeterId( "ccc3c92f-9904-419a-92cc-c62e807c8df7" );
Channel channel3=new Channel();
channel3.setMeterId( "75007bfb-1fb2-4b46-bb3f-2688674ef091" );
channels.add( channel1 );
channels.add( channel2 );
channels.add( channel3 );
sdpIdToChannels.put( "sp_id_green_button_100", channels );
Map<String, Channel> channelPerMeterIdStatic = getChannelPerMeterIdStatic( sdpIdToChannels );
System.out.println( "channelPerMeterIdStatic=" + channelPerMeterIdStatic );
}
private static Map<String, Channel> getChannelPerMeterIdStatic( final Map<String, List<Channel>> sdpIdToChannels ) {
return sdpIdToChannels.values()
.stream()
.filter( Objects::nonNull )
.flatMap( Collection::stream )
.collect( Collectors.toMap( Channel::getMeterId, Function.identity() ) );
}
The output map channelPerMeterIdStatic has different sequence of Channel if we compare the meterId.