Java 如何创建整数和字符串对的排序列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4353572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a sorted list of integer and string pairs?
提问by FerCa
How can I create a list (or some other type of container) of integer and strings pairs that allows duplicates in both pairs and can be sorted by the integer value?
如何创建整数和字符串对的列表(或其他类型的容器),以允许两对中的重复项并且可以按整数值排序?
I need to fill a container with names (string) and scoring (integer) pairs, the container must allow duplicated values in both name and scoring, and i need to sort this list by the scoring value.
我需要用名称(字符串)和评分(整数)对填充容器,容器必须允许名称和评分中的重复值,我需要按评分值对这个列表进行排序。
I tried with a SortedMap but doesn't allow duplicated values:
我尝试使用 SortedMap 但不允许重复值:
SortedMap<Integer,String> sm=new TreeMap<Integer, String>();
sm.put(23, "Peter");
sm.put(11, "Tony");
sm.put(110, "Claire");
sm.put(13, "ferca");
sm.put(55, "Julian");
sm.put(13, "Pedro");
In this example, ferca and Pedro have the same scoring value, this is something I need to allow, but the SortedMap overwrites "ferca" with "Pedro".
在这个例子中,ferca 和 Pedro 具有相同的得分值,这是我需要允许的,但是 SortedMap 用“Pedro”覆盖了“ferca”。
What is the best container type to do this?
执行此操作的最佳容器类型是什么?
采纳答案by aioobe
Since you want your collection to be ordered, I suggest you use a List
and Collections.sort
. If you decide to go for this approach you still have two options:
由于您希望订购您的收藏,我建议您使用 aList
和Collections.sort
。如果您决定采用这种方法,您仍然有两种选择:
- Create a custom
Comparator
that can be passed as an argument tosort
, or - Let the auxiliary
Score
class implementComparable<Score>
- 创建
Comparator
可以作为参数传递给 的自定义sort
,或 - 让辅助
Score
类实现Comparable<Score>
Here is an example and ideone demoof the latter approach:
这是后一种方法的示例和ideone 演示:
import java.util.*;
class Score implements Comparable<Score> {
int score;
String name;
public Score(int score, String name) {
this.score = score;
this.name = name;
}
@Override
public int compareTo(Score o) {
return score < o.score ? -1 : score > o.score ? 1 : 0;
}
}
public class Test {
public static void main(String[] args){
List<Score> scores = new ArrayList<Score>();
scores.add(new Score(23, "Peter"));
scores.add(new Score(11, "Tony"));
scores.add(new Score(110, "Claire"));
scores.add(new Score(13, "ferca"));
scores.add(new Score(55, "Julian"));
scores.add(new Score(13, "Pedro"));
Collections.sort(scores);
}
}
回答by Jon Skeet
If you want a list, use a list...
如果你想要一个列表,使用一个列表...
The best option would probably be to create your own type to encapsulate the string and the integer, add your own comparison, and put them in an ArrayList<T>
.
最好的选择可能是创建您自己的类型来封装字符串和整数,添加您自己的比较,并将它们放在ArrayList<T>
.
Sort it when you need to with Collections.sort
.
需要时对其进行排序Collections.sort
。
If you don't need to allow duplicates which have the same name andscore, you could use a SortedSet
instead, so long as your comparison order sorts on both score andname.
如果您不需要允许具有相同名称和分数的重复项,您可以使用 aSortedSet
代替,只要您的比较顺序对分数和名称进行排序。
回答by skaffman
Sounds like a job for Guava'sMultimaptypes, specifically TreeMultimap.
听起来像是Guava 的Multimap类型的工作,特别是TreeMultimap。
回答by Jigar Joshi
- Create a
class
that enclose these two field - create a custom
Comparator
that compare two Objects based on int value. - Create a
list
of that objects Collection.sort();
pass obj ofcomparator
hereclass MyEntity{ int val; String name; } List<MyEntity> list = new ArrayList<MyEntity>(); list.add(new MyEntity(1,"a")); list.add(new MyEntity(4,"z")); list.add(new MyEntity(2,"x")); Collections.sort(list,new MyComparator()); class MyComparator implements Comparator<MyEntity>{ public int compare(MyEntity ob1, MyEntity ob2){ return ob1.getVal() - ob2.getVal() ; } }
- 创建一个
class
包含这两个字段的 - 创建一个
Comparator
基于 int 值比较两个对象的自定义。 - 创建一个
list
该对象 Collection.sort();
通过comparator
这里的对象class MyEntity{ int val; String name; } List<MyEntity> list = new ArrayList<MyEntity>(); list.add(new MyEntity(1,"a")); list.add(new MyEntity(4,"z")); list.add(new MyEntity(2,"x")); Collections.sort(list,new MyComparator()); class MyComparator implements Comparator<MyEntity>{ public int compare(MyEntity ob1, MyEntity ob2){ return ob1.getVal() - ob2.getVal() ; } }
Note: This is just model to show the basic idea
注意:这只是展示基本思想的模型
回答by Jason
After you create a holding type, an alternative structure is PriorityQueueto hold the items. This differs from Collections.sort()
because the items are inserted in order, with either the high or low values rising to the top.
创建馆藏类型后,另一种结构是PriorityQueue来保存物品。这不同于Collections.sort()
因为项目是按顺序插入的,高值或低值上升到顶部。
The only thing you have to do is write a Comparator to pass onto the PriorityQueue on instanciation, so it knows to sort the items based on the integer value.
您唯一需要做的就是编写一个 Comparator 以在实例化时传递给 PriorityQueue,因此它知道根据整数值对项目进行排序。
Both this method and Collections.sort()
deliver the same results with different ways of going about it. They also run in O(N log N) time.
这种方法并Collections.sort()
通过不同的方式提供相同的结果。它们的运行时间也是 O(N log N)。