对于我班上的一个Homeworks,我们有一个名为Pair的类的集合,我们需要根据键的值将其升序排序。
如果键是字符串或整数,我可以应用此方法,但是我如何编写代码来比较我的商品为通用商品,如下所示?
我班上的教授解释了如何处理整数或字符串,但是当我的变量是通用变量时,我完全不知所措。
下面是我代码相关部分的副本。
import java.util.*;
public class Utils {
public static<K extends Comparable<K>, V> Collection<Pair<K,V>> sortPairCollection(Collection <Pair<K,V>> col){
ArrayList <Pair<K,V>> list = new ArrayList<>();
//Code to compare
return list;
}
public static void main(String[] args) {
ArrayList <Pair<String,Integer>> list = new ArrayList<>();
Pair<String, Integer> e = new Pair<>("One", 1);
list.add(e);
Pair<String, Integer> f = new Pair<>("Two", 2);
list.add(f);
Utils help = new Utils();
help.sortPairCollection(list);
}
}
这第二部分是我的Pair类的代码。 导入java.io.Serializable; 导入java.util.Objects;
public class Pair <K,V> extends Object implements Serializable, Cloneable{
public Pair(K k, V v){
this.k = k;
this.v = v;
}
public K k(){
return k;
}
public V v(){
return v;
}
/*
... //irrelevant data omitted
*/
private final K k;
private final V v;
}
答案 0 :(得分:1)
选项1:使用比较器
public class Cmp<K extends Comparable<K>, V> implements Comparator<Pair<K, V>> {
@Override
public int compare(Pair<K, V> o1, Pair<K, V> o2) {
return o1.k.compareTo(o2.k);
}
}
public class Utils {
public static <K extends Comparable<K>, V> Collection<Pair<K, V>> sortPairCollection(
Collection<Pair<K, V>> col) {
ArrayList<Pair<K, V>> list = new ArrayList<>();
Collections.sort(list, new Cmp<>());
return list;
}
}
选项2。 实施可比
public class Pair<K extends Comparable<K>, V> implements Comparable<Pair<K, V>> {
private K k;
private V v;
@Override
public int compareTo(Pair<K, V> o) {
return k.compareTo(o.k);
}
}
public class Utils {
public static <K extends Comparable<K>, V> Collection<Pair<K, V>> sortPairCollection(Collection<Pair<K, V>> col) {
ArrayList<Pair<K, V>> list = new ArrayList<>();
Collections.sort(list);
return list;
}
}
或者只是
public class Utils {
public static <K extends Comparable<K>, V> Collection<Pair<K, V>> sortPairCollection(Collection<Pair<K, V>> col) {
ArrayList<Pair<K, V>> list = new ArrayList<>();
Collections.sort(list, (p, o) -> p.k.compareTo(o.k));
return list;
}
}
您不必为静态方法btw创建实例。只需调用
Utils.sortPairCollection(list);
答案 1 :(得分:1)
{('Apply', 'singular'): 2,
('Apply', 'value'): 4,
('Apply', 'decomposition'): 0,
('Apply', 'to'): 3,
('Apply', 'obtain'): 1,
('singular', 'value'): 47,
('singular', 'decomposition'): 43,
('singular', 'to'): 46,
('singular', 'obtain'): 44,
('singular', 'word'): 48,
('value', 'decomposition'): 54,
('value', 'to'): 57,
('value', 'obtain'): 56,
('value', 'word'): 58,
('value', 'embeddings'): 55,
('decomposition', 'to'): 23,
('decomposition', 'obtain'): 22,
('decomposition', 'word'): 24,
('decomposition', 'embeddings'): 21,
('decomposition', 'and'): 20,
('to', 'obtain'): 52,
('to', 'word'): 53,
('to', 'embeddings'): 51,
('to', 'and'): 49,
('to', 'compare'): 50,
('obtain', 'word'): 38,
('obtain', 'embeddings'): 36,
('obtain', 'and'): 34,
('obtain', 'compare'): 35,
('obtain', 'with'): 37,
('word', 'embeddings'): 65,
('word', 'and'): 63,
('word', 'compare'): 64,
('word', 'with'): 67,
('word', 'word2vec'): 68,
('embeddings', 'and'): 25,
('embeddings', 'compare'): 26,
('embeddings', 'with'): 27,
('embeddings', 'word2vec'): 28,
('and', 'compare'): 10,
('and', 'with'): 11,
('and', 'word2vec'): 12,
('compare', 'with'): 18,
('compare', 'word2vec'): 19,
('with', 'word2vec'): 62,
('This', 'is'): 6,
('This', 'another'): 5,
('This', 'sentence'): 7,
('This', 'with'): 9,
('This', 'singular'): 8,
('is', 'another'): 29,
('is', 'sentence'): 30,
('is', 'with'): 32,
('is', 'singular'): 31,
('is', 'word'): 33,
('another', 'sentence'): 13,
('another', 'with'): 16,
('another', 'singular'): 14,
('another', 'word'): 17,
('another', 'thingy'): 15,
('sentence', 'with'): 41,
('sentence', 'singular'): 39,
('sentence', 'word'): 42,
('sentence', 'thingy'): 40,
('with', 'singular'): 59,
('with', 'word'): 61,
('with', 'thingy'): 60,
('singular', 'thingy'): 45,
('word', 'thingy'): 66}
我们在这里所做的是,我们从import java.util.*;
public class Utils {
public static <K extends Comparable<K>, V> Collection<Pair<K, V>> sortPairCollection(Collection<Pair<K, V>> col) {
ArrayList<Pair<K, V>> list = new ArrayList<>(col);
//Code to compare
list.sort(Comparator.comparing(Pair::k)); //this is the only change needed
return list;
}
public static void main(String[] args) {
List<Pair<String, Integer>> listas = Arrays.asList(
new Pair<>("One", 1),
new Pair<>("Two", 2));
System.out.println(Utils.sortPairCollection(listas));
}
中提取了一个Camparable
键,并将其传递给Pair
类静态方法,该方法将为我们的Comparator
类。
请参阅Javadoc中的Comparator
here
答案 2 :(得分:0)
如果您像我一样是一个干净优雅的代码爱好者,则可以使用lambda表达式的功能在处理比较逻辑的地方立即创建一个匿名函数(无名称的函数)。从技术上讲,它与使用Comparable
或Comparator
功能接口时相同,但是使用lambda时,您不必通过仅为一个函数创建类来编写样板代码。而是当场以lambda表达式的形式创建该函数,而Java处理其余部分。 Lambda表达式是在Java 8中引入的。
Lambda表达式的格式为( <parameters> ) -> { //body of the function }
public class Pair<K ,V extends Comparable<V>> {
private K k;
private V v;
public Pair(K k, V v){
this.k = k;
this.v = v;
}
public K k(){
return k;
}
public V v(){
return v;
}
}
public static void main(String[] args) {
ArrayList <Pair<String,Integer>> list = new ArrayList<>();
Pair<String, Integer> e = new Pair<>("One", 1);
list.add(e);
Pair<String, Integer> f = new Pair<>("Two", 2);
list.add(f);
// Second argument to this sort function is a lambda expression
Collections.sort( list , (pair1 , pair2)->{
return pair1.v().compareTo(pair2.v());
});
}
}
pair1
和pair2
的数据类型将为Pair
这是有关lambda表达式和功能接口的一些入门链接
功能接口:https://www.geeksforgeeks.org/functional-interfaces-java/
Lambda表达式:https://www.geeksforgeeks.org/lambda-expressions-java-8/