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

2023年 8月 28日 42.6k 0

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

在Java中有两种类型的集合。一种是有序集合,另一种是无序集合。有序集合按照插入的顺序存储元素,即它保持元素的插入顺序。而无序集合,如Map和Set,不保持任何顺序。

在本文中,我们将创建一个无序的集合,并尝试使用内置方法 'Collections.shuffle()' 来对其元素进行洗牌。

Program to shuffle elements of Unordered Collection Set

SortedSet 接口

这个接口的名称中包含了术语“Sorted”,表示它包含了所有元素按升序排列。它扩展了Set接口的属性。为了使用SortedSet的特性,我们将使用实现了SortedSet接口的树集类。

语法

SortedSet 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 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 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 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 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 arayList = new ArrayList(workers.entrySet());
Collections.shuffle(arayList);
// printing details after shuffling
System.out.println("Elements of the newly shuffled map: ");
for (Map.Entry 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中对无序集合进行洗牌操作的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论