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

新兴技术对Java的平台独立性既有威胁也有增强。1)云计算和容器化技术如Docker增强了Java的平台独立性,但需要优化以适应不同云环境。2)WebAssembly通过GraalVM编译Java代码,扩展了其平台独立性,但需与其他语言竞争性能。

不同JVM实现都能提供平台独立性,但表现略有不同。1.OracleHotSpot和OpenJDKJVM在平台独立性上表现相似,但OpenJDK可能需额外配置。2.IBMJ9JVM在特定操作系统上表现优化。3.GraalVM支持多语言,需额外配置。4.AzulZingJVM需特定平台调整。

平台独立性通过在多种操作系统上运行同一套代码,降低开发成本和缩短开发时间。具体表现为:1.减少开发时间,只需维护一套代码;2.降低维护成本,统一测试流程;3.快速迭代和团队协作,简化部署过程。

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

要解决Java应用程序中的平台特定问题,可以采取以下步骤:1.使用Java的System类查看系统属性以了解运行环境。2.利用File类或java.nio.file包处理文件路径。3.根据操作系统条件加载本地库。4.使用VisualVM或JProfiler优化跨平台性能。5.通过Docker容器化确保测试环境与生产环境一致。6.利用GitHubActions在多个平台上进行自动化测试。这些方法有助于有效地解决Java应用程序中的平台特定问题。

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

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

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境