账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Java8 unordered() 无序化无效
    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
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 尤忘、记忆】 普通会员 1楼

      在Java 8中,unordered()方法用于创建一个无序的ListSet集合,但是unordered()方法默认情况下是返回一个TreeSet集合,而不是一个无序的列表。这是因为在Java 8中,TreeSet是自定义的,具有以下特性:

      1. Element Access Order: The elements in a TreeSet are 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.

      2. Duplicates Ignored: TreeSet doesn't allow duplicate elements, which means that it maintains the insertion order of elements.

      3. Navigability: TreeSet is a navigable set, which means that you can use various operations like containsKey(), containsValue(), equals(), get(), get(index), iterator(), keySet(), lastIndexOf(), next(), remove(), size(), toArray(), and size(), among others, on the underlying List or Set.

      However, if you want to convert a List or Set into an unordered() TreeSet with a specific ordering, you can use the TreeMap class 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 List with the elements 1, 3, 5, 7, 9, 2, 4 in a specific order. We then convert this list to a TreeMap using the TreeMap constructor with a custom Comparator that reverses the order of the elements.

      The resulting unorderedMap will 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 the Comparator.reverseOrder() method. The elements 5 and 7 will appear at the top of the map, and the elements 1 and 2 will appear at the bottom of the map.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部