我创建了一个名为Pair
的类,它是L
和R
的泛型类型,它基本上允许我存储对。
我使用Arraylist
来存储类型Pair
,但我不确定如何根据key/value
对arraylist进行排序(并可能搜索所有元素)并打印ArrayList中。
ArrayList<Pair> a = new ArrayList<Pair>();
Pair p = new Pair(1,1);
a.add(p);
a.add(new Pair(1,3));
//System.out.println(help please);
以下是Pair
类
class Pair<L,R> {
L left;
R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
答案 0 :(得分:2)
您的Pair
类可以是implement Comparator<Pair>
接口。之后,您实现方法
@Override
public int compare(Pair o1, Pair o2) {
// here you need to implement how one Pair can be compared to another
// in the scope of ordering them
// you need to fulfil the contract of the Comparator.compare interface
}
答案 1 :(得分:2)
这是一个适合您的工作代码示例(它使用了一些Java 8功能,但如果您只限于较低版本,则可以将这些功能换出)。希望这有帮助!
谢谢, 邓肯
package com.hiveit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Pair<L extends Comparable<L>, R extends Comparable<R>> implements Comparable<Pair<L, R>> {
L left;
R right;
public Pair(final L left, final R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return left;
}
public R getRight() {
return right;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (left == null ? 0 : left.hashCode());
result = prime * result + (right == null ? 0 : right.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair<?, ?> other = (Pair<?, ?>) obj;
if (left == null) {
if (other.left != null) {
return false;
}
} else if (!left.equals(other.left)) {
return false;
}
if (right == null) {
if (other.right != null) {
return false;
}
} else if (!right.equals(other.right)) {
return false;
}
return true;
}
@Override
public int compareTo(final Pair<L, R> other) {
final int compareLeft = left.compareTo(other.left);
if (compareLeft != 0) {
return compareLeft;
}
return right.compareTo(other.right);
}
@Override
public String toString() {
return "Pair [left=" + left + ", right=" + right + "]";
}
public static String listToString(final List<?> list) {
return list.stream().map((pair) -> {
return pair.toString();
}).collect(Collectors.joining(", "));
}
public static void main(final String[] args) {
final List<Pair<Integer, Integer>> a = new ArrayList<>();
a.add(new Pair<>(1, 1));
a.add(new Pair<>(2, 1));
a.add(new Pair<>(2, 3));
a.add(new Pair<>(1, 2));
a.add(new Pair<>(1, 3));
a.add(new Pair<>(2, 2));
final List<Pair<Integer, Integer>> sortedByKey = new ArrayList<>(a);
sortedByKey.sort((o1, o2) -> {
return o1.getLeft().compareTo(o2.getLeft());
});
sortedByKey.stream().map((pair) -> {
return pair.toString();
}).collect(Collectors.joining(", "));
final List<Pair<Integer, Integer>> sortedByValue = new ArrayList<>(a);
sortedByValue.sort((o1, o2) -> {
return o1.getRight().compareTo(o2.getRight());
});
final List<Pair<Integer, Integer>> sortedByKeyAndValue = new ArrayList<>(a);
sortedByKeyAndValue.sort((o1, o2) -> {
return o1.compareTo(o2);
});
System.out.println("Original = " + listToString(a));
System.out.println("Sorted by Left = " + listToString(sortedByKey));
System.out.println("Sorted by Right = " + listToString(sortedByValue));
System.out.println("Sorted by Left then Right = " + listToString(sortedByKeyAndValue));
}
}
答案 2 :(得分:1)
Collections API为此提供了sort
实用程序。
http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
您只需编写一个类来为Comparator
课程实现Pair
。
答案 3 :(得分:0)
不要使用你的Pair类。如果您需要的是具有泛型类型的键/值对的已排序,可遍历且高效的集合,请使用TreeMap。