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

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

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

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题











Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
