首頁  >  文章  >  Java  >  在Java中對無序集合進行洗牌操作

在Java中對無序集合進行洗牌操作

WBOY
WBOY轉載
2023-08-20 09:33:07782瀏覽

在Java中對無序集合進行洗牌操作

在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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除