在Java中有兩種類型的集合。一種是有序集合,另一種是無序集合。有序集合按照插入的順序儲存元素,即它保持元素的插入順序。而無序集合,如Map和Set,不保持任何順序。
在本文中,我們將建立一個無序的集合,並嘗試使用內建方法 'Collections.shuffle()' 來對其元素進行洗牌。
Program to shuffle elements of Unordered Collection Set
SortedSet 介面
這個介面的名稱包含了術語“Sorted”,表示它包含了所有元素按升序排列。它擴展了Set介面的屬性。為了使用SortedSet的特性,我們將使用實作了SortedSet介面的樹集類別。
文法
SortedSet< element_Type > collection_name = new TreeSet<>();
在這裡,element_Type 是包裝類,而不是原始資料類型。
Collections.shuffle()
的翻譯為:Collections.shuffle()
這個方法由 'java.util' 套件提供,作為一個洗牌器。它接受一個集合作為參數,然後隨機重新排列元素。
文法
Collections.shuffle( nameOfcollection );
程式碼的工作原理
我們將建立一個名為 'treeSt' 的Tree Set,並使用內建方法 'add()' 儲存一些類型為String的元素。
現在,建立一個新的ArrayList並複製先前的Tree Set的所有元素。
最後,使用方法‘Collections.shuffle()’來打亂ArrayList的元素,然後列印它們。
Example
的中文翻譯為:範例
import java.util.*; public class Srtset { public static void main(String args[]) { // Creating a tree set SortedSet<String> treeSt = new TreeSet<>(); // Adding elements in the tree set treeSt.add("Tutorix"); treeSt.add("Simply"); treeSt.add("Easy"); treeSt.add("Learning"); treeSt.add("Tutorials"); treeSt.add("Point"); // print elements before shuffling System.out.println("Elements of the given set without performing shuffling: "); System.out.println(treeSt); // storing the elements of tree set in array list List<String> arayList = new ArrayList<>(treeSt); // performing shuffle operation on list Collections.shuffle(arayList); // display the shuffled elements System.out.println("Shuffled elements of the given set: "); System.out.println(arayList); } }
輸出
Elements of the given set without performing shuffling: [Easy, Learning, Point, Simply, Tutorials, Tutorix] Shuffled elements of the given set: [Easy, Simply, Learning, Tutorix, Tutorials, Point]
對無序集合映射的元素進行洗牌的程式
樹狀圖
It is a class that is used to implement NavigableMap Interface. It stores the elements of the map in a tree structure. To sort the LinkedHashMap elements we need to use this class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that eed to useesan class. The most obvious reason for this is that 東西efficient alternative to store the key-value pairs in sorted order.
TreeMap的一般語法如下所示−
文法
TreeMap< TypeOfKey, TypeOfValue > nameOfMap = new TreeMap<>();
程式碼的工作原理
建立一個名為'workers'的TreeMap對象,並使用'put()'方法將元素插入其中。
現在,定義一個新的ArrayList,並使用‘entrySet()’方法將‘workers’的所有元素複製到其中。
繼續前進,使用方法 'Collections.shuffle()' 來打亂 ArrayList 的元素。
最後,定義一個for-each迴圈來列印重新洗牌的元素。 'getKey()'方法將檢索鍵,'getValue()'將取得其對應的值。
Example
的中文翻譯為:範例
import java.util.*; public class Suffle { public static void main(String args[]) { TreeMap<String, Integer> workers = new TreeMap<>(); // Adding elements in the workers map workers.put("Vaibhav", 4000); workers.put("Ansh", 3000); workers.put("Vivek", 1500); workers.put("Aman", 2000); workers.put("Tapas", 2500); // printing details workers map without shuffle System.out.println("Elements of the map: "); for (String unKey : workers.keySet()) { System.out.println("Name: " + unKey + ", Salary: " + workers.get(unKey)); } // create new ArrayList List<Map.Entry<String, Integer>> arayList = new ArrayList<>(workers.entrySet()); Collections.shuffle(arayList); // printing details after shuffling System.out.println("Elements of the newly shuffled map: "); for (Map.Entry<String, Integer> print : arayList) { System.out.println("Name: " + print.getKey() + ", Salary: " + print.getValue()); } } }
輸出
Elements of the map: Name: Aman, Salary: 2000 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500 Name: Vaibhav, Salary: 4000 Name: Vivek, Salary: 1500 Elements of the newly shuffled map: Name: Vaibhav, Salary: 4000 Name: Aman, Salary: 2000 Name: Vivek, Salary: 1500 Name: Ansh, Salary: 3000 Name: Tapas, Salary: 2500
結論
在本文中,我們學習如何透過範例來對無序集合的元素進行洗牌。我們也發現了兩個名為Map和Set的無序集合。
以上是在Java中對無序集合進行洗牌操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器