java Java中的排序列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324097/
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
Sorted List in Java
提问by Gnaniyar Zubair
I need to sort the list in java as below:
我需要在java中对列表进行排序,如下所示:
List contains collection of objects like this,
列表包含这样的对象集合,
List list1 = {obj1, obj2,obj3,.....};
I need the final list which has "lowest value" and "repetition of name should avoid".
我需要具有“最低值”和“应避免重复名称”的最终列表。
Ex:
前任:
List list1 = {[Nellai,10],[Gujarath,10],[Delhi,30],[Nellai,5],[Gujarath,15],[Delhi,20]}
After Sorting , I need the list like this :
排序后,我需要这样的列表:
List list1 = {[Nellai,5],[Gujarath,10],[Delhi,20]};
I have 2 Delhi (30,20) in my list. But I need only one Delhi which has lowest fare (20).
我的列表中有 2 个德里 (30,20)。但我只需要一个票价最低的德里(20)。
How to do that it in java?
如何在java中做到这一点?
Gnaniyar Zubair
格纳尼亚尔·祖拜尔
采纳答案by Przemek Kryger
Almost the same as @Visage answer, but the order is different:
与@Visage 的答案几乎相同,但顺序不同:
public class NameFare {
private String name;
private int fare;
public String getName() {
return name;
}
public int getFare() {
return fare;
}
@Override public void equals(Object o) {
if (o == this) {
return true;
} else if (o != null) {
if (getName() != null) {
return getName().equals(o.getName());
} else {
return o.getName() == null;
}
}
return false;
}
}
....
public Collection<NameFare> sortAndMerge(Collection<NameFare> toSort) {
ArrayList<NameFare> sorted = new ArrayList<NameFare>(toSort.size());
for (NameFare nf : toSort) {
int idx = sorted.getIndexOf(nf);
if (idx != -1) {
NameFare old = sorted.get(idx);
if (nf.getFare() < old.getFare()) {
sorted.remove(idx);
sorted.add(nf);
}
}
}
Collections.sort(sorted, new Comparator<NameFare>() {
public int compare(NameFare o1, NameFare o2) {
if (o1 == o2) {
return 0;
} else {
if (o1.getName() != null) {
return o1.getName().compareTo(o2.getName());
} else if (o2.getName() != null) {
return o2.getName().compareTo(o1.getName());
} else {
return 0;
}
}
}
});
}
回答by Nicolas
If order doesn't matter, a solution is to use a Map[String, Integer]
, add an entry each time you find a new town, update the value each time the stored value is less than the stored one and then zip all the pairs into a list.
如果顺序无关紧要,解决方案是使用Map[String, Integer]
,每次找到新城镇时添加一个条目,每次存储的值小于存储的值时更新值,然后将所有对压缩到列表中。
回答by PaulJWilliams
I would do it in two stages.
我会分两个阶段来做。
Firstrly sort the list using a custom comparator.
首先使用自定义比较器对列表进行排序。
Secondly, traverse the list and, for duplicate entries (which will now be adjacent to each other, provided you worte your comparator correctly), remove the entries with the higher values.
其次,遍历列表,对于重复的条目(如果您正确编写比较器,它们现在将彼此相邻),删除具有较高值的条目。
回答by aksarben
If you want to avoid duplicates, perhaps a class like TreeSet would be a better choice than List.
如果您想避免重复,也许像 TreeSet 这样的类比 List 更好。
回答by Bogdan
I would use an ArrayList like this:
我会使用这样的 ArrayList:
ArrayList<Name> listOne = new ArrayList<Name>();
listOne.add(new Name("Nellai", 10);
listOne.add(new Name("Gujarath", 10);
listOne.add(new Name("Delhi", 30);
listOne.add(new Name("Nellai", 5);
listOne.add(new Name("Delhi", 20);
Collection.sort(listOne);
Then create the Name class
然后创建 Name 类
class name implements Comparable
{
private String name;
private int number;
public Name(String name, int number)
{
this.name= name;
this.number= number;
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.number;
}
public int compareTo(Object otherName) // must be defined if we are implementing //Comparable interface
{
if(otherName instanceif Name)
{
throw new ClassCastException("Not valid Name object"):
}
Name tempName = (Name)otherName;
// eliminate the duplicates when you sort
if(this.getNumber() >tempName.getNumber())
{
return 1;
}else if (this.getNumber() < tempName.getNumber()){
return -1;
}else{
return 0;
}
}
}
I didn't compiled the code, it's edited here so you should fix the code. And also to figure out how to eliminate the duplicates and print only the lowest one.
我没有编译代码,它在这里编辑,所以你应该修复代码。还要弄清楚如何消除重复项并仅打印最低的一个。
You need to sweat too.
你也需要出汗。