Are there any concurrency issues when using Java8's stream to operate external collections?
代言
代言 2017-06-23 09:13:05
0
1
856

Description: Take out the non-DataTime in cp. Is the following code reasonable? Is there any concurrency problem with deviceDataMap?

Map<String,Map<String,String>> deviceDataMap = new HashMap<>();
String cp = "DataTime=20040506010101;SB1-RT=1.1;SB2-RT=2.1";

List<String> cpValusList = Arrays.asList(cp.split(";"));

    cpValusList.stream().filter(item -> !item.contains("DataTime=")).forEach(item ->{
        String deviceId = item.substring(0,item.indexOf("-"));
        if(!deviceDataMap.containsKey(deviceId)){
            Map<String,String> oneDeviceIdValusMap = new HashMap<>();
            List<String> deviceIdValueList = Arrays.asList(item.split(","));
            deviceIdValueList.forEach(value->{
                String[] temp = value.split("=");
                oneDeviceIdValusMap.put(temp[0], temp[1]);
            });
            
            deviceDataMap.put(deviceId, oneDeviceIdValusMap);
        }
        
    });
代言
代言

reply all(1)
Peter_Zhu

Parallel stream is a stream that divides the content into multiple data blocks and uses different threads to process each data block separately

There should be no concurrency issues in the above code. It seems that I still don’t understand the principle of java8 stream》》》》http://blog.csdn.net/sunjin94...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!