流未按插入顺序提供数据
List<Long> trippletList= new ArrayList<>();
trippletList.add(1l);
trippletList.add(3l);
trippletList.add(9l);
trippletList.add(9l);
trippletList.add(27l);
trippletList.add(81l);
Map<Long,Long> arrangedMap=new LinkedHashMap();
arrangedMap=arr.stream().collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
尝试根据其频率收集列表以进行映射,其中键将是数组列表中存在的元素,值将是它在数组列表中的出现次数。打印时,第一个元素来了 81. 如何维护订单
所以答案应该是排列地图(1,1)排列地图(3,1)排列地图(9,2)排列地图(27,1)排列地图(81,1)
回答
你没有使用LinkedHashMap你创建的。并默认Collectors.groupingBy使用 a HashMap。
为了让它生成一个LinkedHashMap,你应该写:
Map<Long,Long> arrangedMap =
trippletList.stream()
.collect(Collectors.groupingBy(Function.identity(),
LinkedHashMap::new,
Collectors.counting()));
现在,如果您打印Map,您将看到:
{1=1, 3=1, 9=2, 27=1, 81=1}