📜  Java中无序集合的混洗元素

📅  最后修改于: 2022-05-13 01:54:19.081000             🧑  作者: Mango

Java中无序集合的混洗元素

Java中的无序集合不提供任何顺序,即不能使用特定的索引或排序来访问元素,就像在有序集合(如 List)的情况下一样。 SetsMaps是无序集合的例子。

在Java中,我们不能直接使用 shuffle 无序集合 Collections.shuffle() 方法,因为它需要一个List作为参数。

为了打乱元素,

  • 我们必须首先将无序集合的元素存储在一个 List 中,然后我们可以使用Collections.shuffle()方法对其进行洗牌。

1) 打乱集合的元素

Java
// Java program to demonstrate the shuffling
// of a set
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a Hashset
        Set st = new HashSet<>();
  
        // Inserting elements to the set
        st.add(91);
        st.add(0);
        st.add(55);
        st.add(10);
        st.add(9);
  
        // 9 won't be inserted to the Set as Hashset does
        // not take duplicate entries
        st.add(9);
  
        // Displaying the elements of the set
        System.out.print("The Set before shuffling: ");
        
        for (int i : st)
            System.out.print(i + " ");
        
        System.out.println();
  
        // Creating a List and storing the values of the
        // Set inside it by passing the Set as a parameter
        // to the constructor
        List list = new ArrayList<>(st);
  
        // Shuffling the elements of the list using using
        // Collections.shuffle() method
        Collections.shuffle(list);
  
        // Displaying the elements of the list
        System.out.print("The List (containing elements of the Set) after shuffling: ");
        
        for (int i : list)
            System.out.print(i + " ");
    }
}


Java
// Java program to demonstrate the shuffling 
// of a map
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a HashMap
        Map mp
            = new HashMap();
  
        // Inserting some values inside the HashMap
        mp.put(1, "Geeks");
        mp.put(2, "for");
        mp.put(3, "Geeks");
        mp.put(4, "is");
        mp.put(5, "love");
  
        // Displaying the map
        System.out.println("The Map before shuffling: ");
        
        for (Map.Entry entry :mp.entrySet())
            System.out.println(entry.getKey() + " "
                               + entry.getValue());
        
        System.out.println();
  
        // Creating a list and storing the elements of the
        // Map inside of it
        List > list
            = new ArrayList<>(mp.entrySet());
  
        // Shuffling the list using shuffle() method
        Collections.shuffle(list);
  
        // Displaying the list
        System.out.println("The List (containing the elements of the Map)" 
                           + " After shuffing: ");
        
        for (Map.Entry entry : list)
            System.out.println(entry.getKey() + " "
                               + entry.getValue());
        System.out.println();
    }
}


输出
The Set before shuffling: 0 55 9 10 91 
The List (containing elements of the Set) after shuffling: 55 0 91 10 9 

2) 打乱地图的元素

Java

// Java program to demonstrate the shuffling 
// of a map
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Creating a HashMap
        Map mp
            = new HashMap();
  
        // Inserting some values inside the HashMap
        mp.put(1, "Geeks");
        mp.put(2, "for");
        mp.put(3, "Geeks");
        mp.put(4, "is");
        mp.put(5, "love");
  
        // Displaying the map
        System.out.println("The Map before shuffling: ");
        
        for (Map.Entry entry :mp.entrySet())
            System.out.println(entry.getKey() + " "
                               + entry.getValue());
        
        System.out.println();
  
        // Creating a list and storing the elements of the
        // Map inside of it
        List > list
            = new ArrayList<>(mp.entrySet());
  
        // Shuffling the list using shuffle() method
        Collections.shuffle(list);
  
        // Displaying the list
        System.out.println("The List (containing the elements of the Map)" 
                           + " After shuffing: ");
        
        for (Map.Entry entry : list)
            System.out.println(entry.getKey() + " "
                               + entry.getValue());
        System.out.println();
    }
}
输出
The Map before shuffling: 
1 Geeks
2 for
3 Geeks
4 is
5 love

The List (containing the elements of the Map) After shuffing: 
2 for
5 love
4 is
3 Geeks
1 Geeks