我搜索过但无法找到关于如何对我自己的通用集合类进行排序的任何答案。
我有这四个课程:
第二次修改 简化代码,应用了Evgeniy的建议,但仍然没有排序,有线:\
Track类:
public class Track {
private Integer id;
private String interpreter;
private String title;
Track(int id,String interpreter,String title) {
this.id=id;
this.interpreter=interpreter;
this.title=title;
}
public String getInterpreter() {
return this.interpreter;
}
public String getTitle() {
return this.title;
}
public int getID() {
return this.id;
}
public String getCompleteName() {
return this.id+"\t"+this.interpreter.toString()+" - "+this.title.toString();
}
}
TrackContainer类:
import java.util.ArrayList;
import java.util.Iterator;
public class TrackContainer<T> extends ArrayList<T> {
private static final long serialVersionUID = 1L;
ArrayList<T> arraylist;
TrackContainer() {
arraylist = new ArrayList<T>();
}
public boolean insertTrack(T track) {
if(arraylist.add(track))
return true;
return false;
}
public void listAllTracks(java.util.Iterator<T> iterat) {
while(iterat.hasNext()) {
System.out.println(iterat.next());
}
}
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return arraylist.iterator();
}
}
TrackIDComparator类:
import java.util.Comparator;
public class TrackIDComparator implements Comparator<Track> {
@Override
public int compare(Track t1, Track t2) {
// TODO Auto-generated method stub
Comparable id1 = (Comparable)(t1.getID());
Comparable id2 = (Comparable)(t2.getID());
return id1.compareTo(id2);
}
}
最后是一个主课:
import java.util.Collections;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
TrackContainer<Track> ltc = new TrackContainer<Track>();
ltc.insertTrack(new Track(2,"trackname1","tracktitle1"));
ltc.insertTrack(new Track(1,"trackname2","tracktitle2"));
ltc.insertTrack(new Track(3,"trackname3","tracktitle3"));
System.out.println("unsorted:");
Iterator<Track> it = ltc.iterator();
while(it.hasNext()) {
System.out.println(it.next().getCompleteName());
}
System.out.println("sorted:");
Collections.sort(ltc,new TrackIDComparator());
Iterator<Track> it2 = ltc.iterator();
while(it2.hasNext()) {
System.out.println(it2.next().getCompleteName());
}
}
}
输出:
unsorted: 2 trackname1 - tracktitle1 1 trackname2 - tracktitle2 3 trackname3 - tracktitle3 sorted: 2 trackname1 - tracktitle1 1 trackname2 - tracktitle2 3 trackname3 - tracktitle3
答案 0 :(得分:3)
只需将sort()方法添加到TrackContainer
即可public void sort() {
Collections.sort(arraylist, new TrackIDComparator());
}
更新
目前尚不清楚为什么Track是通用的以及为什么T id; T口译员;标题;应该有相同的类型。我建议修改设计。无论如何,
class TrackIDComparator
public int compare(Track<T> o1, Track<T> o2) {
if(o1.getID().hashCode() > o2.getID().hashCode())
return 1;
...
不正确,请将其更改为
public int compare(Track<T> o1, Track<T> o2) {
Comparable id1 = (Comparable)(o1.getId());
Comparable id2 = (Comparable)(o2.getId());
return id1.compareTo(id2);
}
答案 1 :(得分:2)
简单的方法是更改Track Container
public class TrackContainer<T> extends ArrayList<Track<T>>
删除arraylist
成员变量,并在任何重写方法中使用this
。
答案 2 :(得分:1)
TrackContainer<T>
不是List
- 如果您想在其上使用Collections.sort(List,Comparator)
,则应将其设为implements List<T>
(当然,并实现所有接口方法)。
答案 3 :(得分:0)
TrackContainer
不是List
,因此您无法使用Collections.sort(List<T>, Comparator<? super T> c)
方法。
或者,您想要对TrackContainer.arrayList
字段进行排序,因此我建议添加一个接受Comparator<Track<T>>
的排序方法,以便在内部进行排序。
此外,您希望在将new TrackIDComparator<String>()
绑定到某个类型时执行Track
。