Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

java - Lambda function changing sequence of elements in input

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

HashMap's do not preserve any order. If you want to ensure that elements in the resultant map maintain encounter order you can specify a LinkedHashMap as the resultant map type.

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(),
            (a,b)->a , //merge function not used.
            LinkedHashMap::new // map implementation 
          ));
   }

The above will only preserve the order in which the elements are placed on the stream and that the stream itself maintains that order. If the stream is unordered, then the stream will will not take special actions to preserve the order (e.g. in case of parallel processing).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...