Java 对无序集合的元素进行排序
java中的无序集合不提供任何顺序,也就是说,不能像List这样的有序集合那样使用特定的索引或顺序来访问这些元素。集合和Map是无序集合的例子。
在java中,我们不能直接使用Collections.shuffle()方法对无序集合进行洗牌,因为它需要一个List作为参数。
用于洗牌的元素。
- 我们必须首先将无序集合的元素存储在一个List中,然后我们可以使用Collections.shuffle()方法对其进行洗牌。
1) 洗涤一个集合的元素
// 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<Integer> 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<Integer> list = new ArrayList<>(st);
// Shuffling the elements of the list 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 + " ");
}
}
输出
The Set before shuffling: 0 55 9 10 91
The List (containing elements of the Set) after shuffling: 55 0 91 10 9
2) 洗涤Map上的元素
// 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<Integer, String> mp
= new HashMap<Integer, String>();
// 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<Integer, String> 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<Map.Entry<Integer, String> > 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<Integer, String> 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