Java Comparator usage

I'm fairly new to java thus the question. My task is to create a class Checker which uses a comparator desc to sort the players. The sorting logic is to sort the players in decreasing order by score and then if two players have the same score, the one whose name is lexicographically larger should appear first. This is the Player class class Player { String name; int score; } The

Java比较器的用法

我相当新的Java,因此这个问题。 我的任务是创建一个类检查器,它使用比较器desc对玩家进行排序。 排序逻辑是按照降序对玩家进行排序,然后如果两个玩家的得分相同,那么名称按字典顺序排列的玩家应该首先出现。 这是Player类 class Player { String name; int score; } 比较器被这样调用 Checker check=new Checker(); ................. Arrays.sort(Player,check.desc); 这是我试过的, class Checker imple

When to use Comparator and when to use Comparable in Java?

I have an Employee class which has 3 fields like below. class Employee { private int empId; private String empName; private int empAge; public Employee(int empId, String empName, int empAge) { this.empId = empId; this.empName = empName; this.empAge = empAge; } // setters and getters For this I want to sort based on Employee Name(empName), if multiple employees have the s

何时使用Comparator以及何时在Java中使用Comparable?

我有一个Employee类,有3个字段,如下所示。 class Employee { private int empId; private String empName; private int empAge; public Employee(int empId, String empName, int empAge) { this.empId = empId; this.empName = empName; this.empAge = empAge; } // setters and getters 为此,我想根据员工姓名(empName)进行排序,如果多个员工具有相同的姓名,则根据员工ID(empId)进行排序。

Implement BST using comparable or comparator

I'm trying to create a generic BinarySearchTree<T> class. I want to provide two options (constructors), Empty constructor for a generic class which implements Comparable<T> ie If Dog is a class which implements Comparable<Dog> , then: BinarySearchTree<Dog> bst = new BinarySearchTree<Dog>(); Pass a Comparator<T> for a generic class which need not have

使用可比较的或比较器来实施BST

我试图创建一个通用的BinarySearchTree<T>类。 我想提供两个选项(构造函数), 实现Comparable<T>的泛型类的空构造函数,即如果Dog是实现Comparable<Dog> ,则: BinarySearchTree<Dog> bst = new BinarySearchTree<Dog>(); 为Comparator<T>传递一个不需要实现Comparable<T>的泛型类,即如果Dog是一个类(没有实现Comparable<Dog> ),并且DogComp是一个实现Comparator

Different types of comparables

when implementing the comparator interface to achieve natural ordering of objects : say if we had the class Account: class Account { Account(String name, int id) int balance() void deposit(int n) } we wanted to sort the Account balances of two accounts in order whats the diffrence between these two methods? public class comparebalances implements Comparable <Account> {

不同类型的可比较物

当实现比较器接口来实现对象的自然排序时: 说如果我们有班级帐户: class Account { Account(String name, int id) int balance() void deposit(int n) } 我们希望按顺序对两个帐户的帐户余额进行排序 这两种方法有什么区别? public class comparebalances implements Comparable <Account> { public int compare (Account acc1, Account acc2) { return acc1.balance()-acc2.balanc

Does a natural comparator exist in the standard api?

I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); }

标准api中是否存在自然比较器?

我需要一个比较器作为策略模式的一部分,可以使用对象的自然排序或一些自定义排序。 对于自然排序情况,我写了一个简单的比较器: private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } } 似乎很简单,但我想知道是否有人知道标准API中的一个。 我查看了Tre

Why doesn't java.lang.Number implement Comparable?

This question already has an answer here: Comparing the values of two generic Numbers 11 answers It's worth mentioning that the following expression: new Long(10).equals(new Integer(10)) is always false , which tends to trip everyone up at some point or another. So not only can you not compare arbitrary Number s but you can't even determine if they're equal or not. Also, with

为什么java.lang.Number不实现Comparable?

这个问题在这里已经有了答案: 比较两个通用数字的值11个答案 值得一提的是下面的表达式: new Long(10).equals(new Integer(10)) 总是false ,这往往会在某个点或另一个地方让每个人都出现。 所以你不仅可以比较任意的Number但你甚至不能确定它们是否相等。 另外,对于真实的原始类型( float , double ),确定两个值是否相等是棘手的,必须在可接受的误差范围内完成。 尝试如下代码: double d1 = 1.0d; double d2

When should a class be Comparable and/or Comparator?

I have seen classes which implement both Comparable and Comparator . What does this mean? Why would I use one over the other? The text below comes from Comparator vs Comparable Comparable A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances. Comparator A

什么时候班级应该是Comparable和/或Comparator?

我见过实现Comparable和Comparator的类 。 这是什么意思? 为什么我会用另一种呢? 下面的文本来自Comparator vs Comparable 可比 一个可比较的对象能够将自己与另一个对象进行比较。 类本身必须实现java.lang.Comparable接口,以便能够比较它的实例。 比较 比较对象能够比较两个不同的对象。 该类不是比较它的实例,而是一些其他类的实例。 这个比较器类必须实现java.util.Comparator接口。 实现Comparable意

In ConcurrentHashMap how the segments are defined

Hello friends I am new to Java Concurrency. I have 2 questions as below. Q.1 In ConcurrentHashMap how the segments are defined? Means if there are 64 elements in that Map and concurrencyLevel value as 16 (there are 16 threads that can work simultaneously) then how the segments are defined? The Q is are they equal sized 16 segments with 4 elements in each? Or it will be unequal sized segment

在ConcurrentHashMap中如何定义段

你好朋友,我是Java并发新手。 我有两个问题如下。 Q.1在ConcurrentHashMap中如何定义段? 意味着如果在该Map中有64个元素,并且concurrencyLevel值为16(有16个线程可以同时工作),那么这些段如何定义? Q是他们相等大小的16个部分,每个部分有4个元素? 或者它会是不相等大小的细分市场? Q.2。 如果我将初始容量定义为62,并发定义为16的ConcurrentHashMap。如果我在该地图中放置了62个元素。 根据我的理解,将会

how to lock free use two ConcurrentHashMap in java/scala?

I headache with the problem long time ago, hope some help really. I want store many Task with ConcurrentSkipListMap which inner is ConcurrentHashMap as known as multi-segment lock. The simple example code show with scala(java also readable): val tasks = new ConcurrentSkipListMap[TaskKey, Task]() refer class simple as: class TaskKey(id: String, systemTime: Long) TaskKey class used to iden

如何锁定免费使用java / scala中的两个ConcurrentHashMap?

我很久以前就对这个问题感到头痛,希望能有一些帮助。 我想存储许多Task与ConcurrentSkipListMap该内是ConcurrentHashMap已知为多段锁。 简单的示例代码用scala显示(java也可读): val tasks = new ConcurrentSkipListMap[TaskKey, Task]()将类简单引用为: class TaskKey(id: String, systemTime: Long) 用于识别任务的TaskKey类是唯一的,任务如下: trait Task { val taskId: TaskKey //account and custom name

Safely iterate an ConcurrentHashMap that is accessed by multiple thread

I have a ConcurrentHashMap object which is shared by multiple threads to access: Map<String, MyConnection> myMap = new ConcurrentHashMap<String, MyConnection>(); The class MyConnection contains some connection to the datasource. At a later stage, I need to iterate the ConcurrentHashMap, call MyConnection.close() method and remove it from the Map. Something like this: for(String

安全地迭代多线程访问的ConcurrentHashMap

我有一个ConcurrentHashMap对象,它由多个线程共享来访问: Map<String, MyConnection> myMap = new ConcurrentHashMap<String, MyConnection>(); 类MyConnection包含到数据源的一些连接。 在稍后阶段,我需要迭代ConcurrentHashMap,调用MyConnection.close()方法并将其从Map中删除。 像这样的东西: for(String key : myMap.ketSet()) { MyConnection connection = myMap.get(key); connection.clo