- 33
- 0
Java8 unordered()无序化无效, 多次运行结果还是与流中元素顺序一致
// 多次运行结果: 5, 1, 2, 6, 3, 7, 4 没有达到无序的效果
Stream.of(5, 1, 2, 6, 3, 7, 4).unordered().forEach(System.out::println);
求大神指教
相关代码
/**
* S unordered(); 产生一个与当前流中元素相同的无序流, 当流本身就是无序或流已经无序, 会返回流本身
*/
@Test
public void unorderedTest() {
Stream.of(5, 1, 2, 6, 3, 7, 4).forEach(System.out::println);
// 多次运行结果: 5, 1, 2, 6, 3, 7, 4 没有达到无序的效果 TODO
Stream.of(5, 1, 2, 6, 3, 7, 4).unordered().forEach(System.out::println);
}
- 共 0 条
- 全部回答
-
尤忘、记忆】 普通会员 1楼
在Java 8中,
unordered()方法用于创建一个无序的List或Set集合,但是unordered()方法默认情况下是返回一个TreeSet集合,而不是一个无序的列表。这是因为在Java 8中,TreeSet是自定义的,具有以下特性:-
Element Access Order: The elements in a
TreeSetare accessed in a sorted order, so that the first element in the set is always the smallest and the last element is always the largest. -
Duplicates Ignored:
TreeSetdoesn't allow duplicate elements, which means that it maintains the insertion order of elements. -
Navigability:
TreeSetis a navigable set, which means that you can use various operations likecontainsKey(),containsValue(),equals(),get(),get(index),iterator(),keySet(),lastIndexOf(),next(),remove(),size(),toArray(), andsize(), among others, on the underlyingListorSet.
However, if you want to convert a
ListorSetinto anunordered()TreeSetwith a specific ordering, you can use theTreeMapclass instead, which allows you to specify a custom order for the elements. Here's an example of how you can do this:```java import java.util.ArrayList; import java.util.List; import java.util.Map;
public class Main { public static void main(String[] args) { // Create a List or Set with elements in a specific order List
list = new ArrayList<>(Arrays.asList(1, 3, 5, 7, 9, 2, 4)); Set set = new HashSet<>(list); // Convert the List or Set to an unordered, custom order Map Map<Integer, Integer> unorderedMap = new TreeMap<>(Comparator.reverseOrder()); // Add elements to the unorderedMap unorderedMap.put(1, 2); unorderedMap.put(3, 5); unorderedMap.put(5, 7); unorderedMap.put(7, 9); unorderedMap.put(9, 2); unorderedMap.put(2, 4); // Print the unorderedMap System.out.println(unorderedMap); }} ```
In this example, we first create a
Listwith the elements1, 3, 5, 7, 9, 2, 4in a specific order. We then convert this list to aTreeMapusing theTreeMapconstructor with a customComparatorthat reverses the order of the elements.The resulting
unorderedMapwill contain the elements in the original order, with the elements in the list starting from index 0 and the elements in the set starting from index 1, as specified in theComparator.reverseOrder()method. The elements5and7will appear at the top of the map, and the elements1and2will appear at the bottom of the map. -
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
