使用Comparable比较通用变量 [英] Using Comparable to compare generic variables

查看:66
本文介绍了使用Comparable比较通用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我班上的一本家庭作业,我们有一个名为Pair的类的集合,我们需要根据键的值对它进行升序排序.

For one of the Homeworks in my class, we have a collection of a class titled Pair and we need to sort it in ascending order based on the value of the key.

如果键是字符串或整数,我可以应用此方法,但是我如何编写代码来比较我的项目为通用时的情况,如下所示?

I could apply this if the keys were strings or integers, but how do I write code that would compare my items when they're Generic as seen below?

我班上的教授解释了如何处理整数或字符串,但是当我的变量是通用变量时,我完全不知所措.

The professor in my class explained what to do for integers or strings but when my variables are generic I'm at a complete loss.

下面是我代码中相关部分的副本.

Below are copies of the relevant parts of my code.

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;

This second part here is the code for my Pair class. import java.io.Serializable; import 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;
}

推荐答案

选项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创建实例.只需调用

You don't have do create an instance for your static method btw. just invoke

Utils.sortPairCollection(list);

这篇关于使用Comparable比较通用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆