comparable

How to efficiently compare Sets? [duplicate]

跟風遠走 提交于 2019-12-01 21:15:27
This question already has an answer here: What is the fastest way to compare two sets in Java? 9 answers Given two Sets: how to compare them efficiently in Java? (a) keep them as List s, sort them and compare them. ( Comparable ) (b) keep them as Set s and compare the hashCode of the Sets? background: many comparisons need to be done Sets are small (usually < 5 elements per set). The proper way to compare two sets is to use the equals method . I would not worry about performance unless you have proven that this is a part of your code that is causing performance issue (which I doubt). And

Sorting ArrayList of Arraylist<String> in java

[亡魂溺海] 提交于 2019-12-01 17:56:38
I have an ArrayList of ArrayList of String. In Outer ArrayList on each index each Inner ArrayList has four items have four parameters. Contacts Id Contacts Name Contacts Adress Contacts Number Now I want to sort the complete ArrayList of the on the basis of Contact Name Parameter. Means I want to access the outer Arraylist and the inner ArrayList present on each index of outer Arraylist should be sorted according to contact Name. Comparator / Comparable Interfaces not likely to help me. Collection.sort can't help me Sorting Arraylist of Arraylist of Bean . I have read this post but it is for

Java - how to sort object in many ways: Arrays.sort(), Comparable<T>

北战南征 提交于 2019-12-01 17:53:47
Let's say that I have an array with objects, where I have some employees (objects). They all have: int age , double salary . I want to sort this array so my class implements Comparable <Employee> . I've made a method: public int compareTo(Employee other) { return Double.compare(salary, other.salary); } And it's ok, sorting is working fine. But I'm sorting by double salary . Now I want to sort by int age so what now? I've made a method: public int compareAge(Employee other) { return Integer.compare(age, other.age); } And how can I use Arrays.sort() with this? I want to have possibility to use

Best way to order an HashMap by key in Java?

限于喜欢 提交于 2019-12-01 13:09:45
This is the first time that I have to order an HashMap in Java. I need to do that by key but in my case the key is an object so I need to order by a specific field. Trying to figure it by my own I've considered to proceed with this simple scratch of code: private HashMap<SimpleDBField, String> sortTable(HashMap<SimpleDBField, String> row){ LinkedHashMap<SimpleDBField, String> orderedRow = new LinkedHashMap<SimpleDBField, String>(); for(int i = 1; i <= row.size(); i ++){ Iterator iterator = row.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<SimpleDBField, String> entry = (Map.Entry

Comparable与Comparator的区别

时光怂恿深爱的人放手 提交于 2019-12-01 12:19:12
前几天在项目中遇到了一个将复杂对象进行排序的问题:计算BingMap地图上距离 当前位置5KM内 发生事故(TrafficIncident)的点到当前位置的距离,并按距离升序排序。距离都算出来了,但这些TrafficIncident对象的排序却难到了我。经同事提醒,Comparable或Comparator是一个不错的选择。于是在网上搜索了一些资料,总结下来。 方式一: 实现Comparable接口 Comparable是java.lang包下的一个接口,该接口里只有一个compareTo()方法: package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo(T o); } Comparable翻译为“可比较的”,表明实现该接口的类都是可以比较的,即实现Comparable接口的类本身就已经支持自比较,例如: String、Integer 自己就可以完成比较大小操作,它们已经实现了Comparable接口。查看String类的源码可以看见是这样声明的(JDK1.7): public final class String implements java.io.Serializable, Comparable<String>, CharSequence

Best way to order an HashMap by key in Java?

白昼怎懂夜的黑 提交于 2019-12-01 12:01:48
问题 This is the first time that I have to order an HashMap in Java. I need to do that by key but in my case the key is an object so I need to order by a specific field. Trying to figure it by my own I've considered to proceed with this simple scratch of code: private HashMap<SimpleDBField, String> sortTable(HashMap<SimpleDBField, String> row){ LinkedHashMap<SimpleDBField, String> orderedRow = new LinkedHashMap<SimpleDBField, String>(); for(int i = 1; i <= row.size(); i ++){ Iterator iterator =

Ordering enum values

蓝咒 提交于 2019-12-01 06:32:32
I was wondering if there is any way of ordering enum for different classes. If for example, I have a fixed group of chemicals which react in different ways to other chemicals, some strongly, some weakly. I basically want to be able to switch up the order in which they are arranged depending on the chemical the group is supposed to react to(i.e depending on the class.). I do know that I am supposed to use Comparable but I am not sure how to do it. If I am not clear enough, leave a comment and I will explain further. Thanks. public static enum Chem { H2SO4, 2KNO3, H20, NaCl, NO2 }; So I have

How does the sort() method of the Collection class call the Comparable's compareTo()?

有些话、适合烂在心里 提交于 2019-12-01 06:23:51
Suppose I want to sort a list of Employee objects: Employee emp1 = new Employee("Abhijit", 10); Employee emp2 = new Employee("Aniket", 5); Employee emp3 = new Employee("Chirag", 15); List<Employee> employees = new ArrayList<Employee>(); employees.add(emp1); employees.add(emp2); employees.add(emp3); Collections.sort(employees); System.out.println("sorted List is: "+employees); And my Employee class implements Comparable , therefore it must override the compareTo() method. And I have to write my sorting logic in the compareTo() method. class Employee implements Comparable<Employee> { String name

Ordering enum values

冷暖自知 提交于 2019-12-01 04:59:33
问题 I was wondering if there is any way of ordering enum for different classes. If for example, I have a fixed group of chemicals which react in different ways to other chemicals, some strongly, some weakly. I basically want to be able to switch up the order in which they are arranged depending on the chemical the group is supposed to react to(i.e depending on the class.). I do know that I am supposed to use Comparable but I am not sure how to do it. If I am not clear enough, leave a comment and

How does the sort() method of the Collection class call the Comparable's compareTo()?

半腔热情 提交于 2019-12-01 04:41:20
问题 Suppose I want to sort a list of Employee objects: Employee emp1 = new Employee("Abhijit", 10); Employee emp2 = new Employee("Aniket", 5); Employee emp3 = new Employee("Chirag", 15); List<Employee> employees = new ArrayList<Employee>(); employees.add(emp1); employees.add(emp2); employees.add(emp3); Collections.sort(employees); System.out.println("sorted List is: "+employees); And my Employee class implements Comparable , therefore it must override the compareTo() method. And I have to write