我有一个字符串链表,每个字符串都有一个计算的整数值。为了更容易解释,我有一个表示节点的字符串链表。每个节点都有一个距离。为了获得距离,您可以使用我在程序中创建的方法来返回该节点的距离。我希望链接列表按字符串的每个距离排序,从最低到最高。我该怎么办?这是一个psedocode
Queue<String> someStringList = new LinkedList<String>();
...
for each of the nodes in the list
String node = ((LinkedList<String>) someStringList).get(i);
distance = theNodesDistance(node);
sort the linked list by the node's distance
...
public int theNodesDistance(String str){
return distance;
}
答案 0 :(得分:1)
第一个选项是创建比较器,然后对集合进行排序。我会做类似的事情:
public static void main(String... arg) {
LinkedList<String> someStringList = new LinkedList<>();
someStringList.add("2");
someStringList.add("1");
System.out.println(someStringList);
Collections.sort(someStringList, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return theNodesDistance(s1).compareTo(theNodesDistance(s2));
}
});
System.out.println(someStringList);
}
public static Integer theNodesDistance(String str){
return Integer.parseInt(str); // here return the distance
}
另一种选择是创建一个具有id和距离的类Node:
public class Node implements Comparable<Node> {
String id;
Integer distance;
public Node(String id) {
this.id = id;
this.distance = theNodesDistance(id);
}
@Override
public int compareTo(Node node) {
return this.distance.compareTo(node.distance);
}
public String toString() {
return id;
}
public Integer theNodesDistance(String str){
return Integer.parseInt(str);
}
}
然后,对列表进行排序:
LinkedList<Node> nodes = new LinkedList<>();
nodes.add(new Node("2"));
nodes.add(new Node("1"));
System.out.println(nodes);
Collections.sort(nodes);
System.out.println(nodes);
最后,您可以使用PriorityQueue,它可以在您插入新节点时组织元素。我的意思是,您可以按顺序删除每个节点。
Queue<Node> nodes = new PriorityQueue<>();
nodes.add(new Node("10"));
nodes.add(new Node("3"));
nodes.add(new Node("2"));
nodes.add(new Node("1"));
nodes.add(new Node("4"));
System.out.println(nodes.remove());
System.out.println(nodes.remove());
System.out.println(nodes.remove());
System.out.println(nodes.remove());
System.out.println(nodes.remove());
在这种情况下,输出将是:
1
2
3
4
10
答案 1 :(得分:1)
如果你能够在重复的距离计算中浪费CPU,那么你可以使用JDK排序方法和Comparator实现,它可以计算任意2个字符串的距离并进行比较。这是最简单的。
如果您希望只计算每个字符串的距离一次(假设这是昂贵的),那么您可以:
a)构造一个新的元组集合(字符串和距离),并按距离对元组进行排序(再次使用比较器,或使元组类别可比)。
b)或者您可以尝试在比较器依赖的字符串到距离的散列图中缓存距离。
答案 2 :(得分:0)
您可以使用TreeMap ..以下是正常演示 -
TreeMap<Integer, String> tmap = new TreeMap<Integer, String>();
/*Adding elements to TreeMap*/
tmap.put(1, "Data1");
tmap.put(23, "Data2");
tmap.put(70, "Data3");
tmap.put(4, "Data4");
tmap.put(2, "Data5");
/* Display content using Iterator*/
Set set = tmap.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print("key is: "+ mentry.getKey() + " & Value is: ");
System.out.println(mentry.getValue());
}
输出::
key is: 1 & Value is: Data1
key is: 2 & Value is: Data5
key is: 4 & Value is: Data4
key is: 23 & Value is: Data2
key is: 70 & Value is: Data3
答案 3 :(得分:0)
您可以使用排序算法。喜欢selectionsort,bubblesort ... 这是insertionsort
double temp;
for(int i = 1;i<yourList.size();i++)
{
temp = thenodedistance(yourlist.get(i));
int j = i;
while(j>0&&thenodedistance(yourlist.get(j-1))> temp)
{
yourlist.add(j,yourlist.remove(j-1);
j--;
}
yourlist.add(j,yourliste.remove(i));
}
这样你可以对列表进行排序(没有尝试代码......)
答案 4 :(得分:0)
拥有Node类怎么样?
class Node{
String str;
int distance;
public Node(String str){
this.str = str;
this.distance = theNodesDistance(str);
}
}
然后你可以覆盖比较器。我建议你使用PriorityQueue而不是LinkedList,这样你对有序列表的插入可以更有效(O(logn))。在这种情况下,您实际上不需要调用sort或heapify函数,因为优先级队列始终保持节点的顺序。
PriorityQueue que = new PriorityQueue(new Comparator<Node>(){
public int compare(Node n1, Node n2){
if(n1.distance < n2. distance) return -1;
else if(n1.distance > n2.distance) return 1;
return 0;
}
});
您可以按如下方式添加到队列中:
for(each str){
que.add(new Node(str));
}