在Java中对无序集合进行洗牌操作

WBOY
WBOY 转载
2023-08-20 09:33:07 294浏览

在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 it provides an 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,如有侵犯,请联系admin@php.cn删除