您好,欢迎访问一九零五行业门户网

在Java中使用Comparable接口按值对LinkedHashMap进行排序

linkedhashmap是一个通用类,用于实现map接口。此外,它是 hashmap 类的子类,因此它可以使用 hashmap 类的所有方法并执行类似的操作。
java提供了多种对linkedhashmap进行排序的方法,我们将通过本文学习如何使用comparable接口创建它并按其值进行排序。
按值对linkedhashmap进行排序的程序在直接跳到排序程序之前,让我们先看一下几个概念 -
linkedhashmap正如我们之前讨论的,linkedhashmap 类扩展了 hashmap 类来实现 map 接口。它维护键值对。 key 是一个用于获取和接收与其关联的值的对象。它将映射的元素按照插入的顺序存储在 linkedlist 中,即它维护元素的插入顺序。此外,每当我们返回其元素时,它都会按插入顺序打印。
linkedhashmap 的一般语法如下 -
语法linkedhashmap< typeofkey, typeofvalue > nameofmap = new linkedhashmap<>();
在上述语法中,
typeofkey − 指定键的数据类型。
typeofvalue − 指定将要存储在映射中的值的数据类型。
nameofmap − 给你的地图取一个合适的名字。
类似的界面java提供了多种排序算法和方法,可以帮助我们对数组、列表或任何集合进行排序。当我们想要按自然顺序对自定义对象进行排序时,可比较接口是一种非常有用的附加方法。例如,它按字典顺序对字符串进行排序,按数字顺序对数字进行排序。该接口在“java.lang”包中可用。
语法class nameofclass implements comparable<nameofclass>
compareto()方法comparable 接口仅定义了一个名为“compareto”的方法,可以覆盖该方法以对对象集合进行排序。它提供了将类的对象与其自身进行比较的能力。当“this”对象等于传递的对象时,它返回 0,如果“this”对象更大,则返回正值,否则返回负值。
语法compareto(nameofclass nameofobject);
collections.sort()方法集合接口的“collections”类提供了一个名为“collections.sort()”的静态方法,可以对指定集合(如 arraylist 或 linkedlist)的元素进行排序。它在“java.util”包中可用。
语法collections.sort(nameofcollection);
算法步骤 1 - 创建一个实现comparable接口的类'cart'。在类内部,声明两个变量,并定义一个构造函数,该构造函数带有两个参数'item'和'price',分别为字符串和双精度浮点数类型。
第 2 步 - 进一步,我们将使用“tostring()”方法将对象的数据转换为字符串。然后,定义“compareto”方法以及“cart”类的对象作为参数,以将“this”对象与新创建的对象进行比较。
第三步 - 现在,在main()方法中,声明一个名为'obj'的linkedhashmap类的'cart'对象,并使用名为'put()'的内置方法将对象的详细信息存储到其中。'item'是键,其对应的值是'price'。
步骤 4 - 最后,定义一个名为“srtlist”的 arraylist 集合来存储 linkedhashmap 的排序元素。现在,将“obj”作为参数传递给“collections.sort()”方法,以按值执行排序操作。
示例import java.util.*;import java.lang.*;public class cart implements comparable<cart> { string item; double price; cart(string item, double price) { // this keyword shows these variables belongs to constructor this.item = item; this.price = price; } // method for converting object into string public string tostring() { return item: + item + , + price: + price; } public string getname() { // to retrieve item name return this.item; } // overriding method public int compareto(cart comp) { if(this.price > comp.price) { return 1; } else { return -1; } } public static void main(string[] args) { // declaring collection linkedhashmap linkedhashmap<string, cart> obj = new linkedhashmap<>(); // adding object to the obj map cart obj1 = new cart(rice, 59); obj.put(obj1.getname(), obj1); cart obj2 = new cart(milk, 60); obj.put(obj2.getname(), obj2); cart obj3 = new cart(bread, 45); obj.put(obj3.getname(), obj3); // printing details obj map in unsorted order system.out.println(elements of the map: ); for (string unkey : obj.keyset()) { system.out.println(obj.get(unkey)); } list<cart> srtlist = new arraylist<>(obj.values()); collections.sort(srtlist); // sorting the object // printing details of obj map in sorted order system.out.println(elements of the newly sorted map: ); system.out.println(srtlist); }}
输出elements of the map: item: rice, price: 59.0item: milk, price: 60.0item: bread, price: 45.0elements of the newly sorted map: [item: bread, price: 45.0, item: rice, price: 59.0, item: milk, price: 60.0]
结论在 java 1.0 版本中,首次引入了类似的接口,并在“java.lang”包中提供。在本文中,我们探索了linkedhashmap以及comparable接口在排序操作中的使用。
以上就是在java中使用comparable接口按值对linkedhashmap进行排序的详细内容。
其它类似信息

推荐信息