java - Stream.map()方法是保持原有顺序么?还是会重新排序呢?
PHP中文网
PHP中文网 2017-04-18 10:10:56
0
3
1348
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
PHPzhong

Hello, in the case of non-concurrent streams, the processing order of the map method of the stream depends on the order in which the stream currently processes the upstream collection. The order of the ArrayList defaults to the natural order (insertion order), so the ArrayList collection is converted to the map method of the stream. Ability to process collection elements in natural order.

// 输出c b a
List<String> a = new LinkedList<String>();
a.add("c");
a.add("b");
a.add("a");
a.stream().map(x -> {
  System.out.println(x);
  return x;
}).count();

Map<String,String> m = new HashMap<String,String>();
m.put("4","D");
m.put("2","B");
m.put("1","A");
m.put("3","C");
// m.values() 默认顺序为A B C D
// 因此stream map的处理顺序也是 A B C D
m.values().stream().map(x -> {
                    System.out.println(x);
                    return x;
                }).count();
左手右手慢动作

map Mapping, this has nothing to do with sorting.

List<String> list = new ArrayList<>();
list.add("A");
list.add("B");

list.stream().map((Function<String, Object>) s -> s + "1").forEach(System.out::println);
// output:
// A1
// B1
Ty80

parallelStream does not guarantee the order, streamp guarantees the order

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!