【问题标题】:Java Comparing three generic elementsJava比较三个通用元素
【发布时间】:2022-10-15 02:21:27
【问题描述】:

如果它们是相同的类型,我想比较元素的集合(ArrayList)。一开始我不知道元素是什么类型(泛型类型),所以我决定使用 Object 类型。但我仍然无法比较它们。问题出在函数triplesort() 中。
警告是:
Operator '>' cannot be applied to 'java.lang.Object', 'java.lang.Object'.
如果您对此问题有任何可能的解决方案并告诉我,我将不胜感激。 <3
三重java

import java.util.ArrayList;

public class Triple<T, S, U> {
    private T t;
    private S s;
    private U u;
    private ArrayList<Object> array = new ArrayList<Object>();

    Triple(T t, S s, U u) {
        setT(t);
        setS(s);
        setU(u);
        array.add(this.t);
        array.add(this.s);
        array.add(this.u);
    }

    public void setT(T t) {
        this.t = t;
    }

    public void setS(S s) {
        this.s = s;
    }

    public void setU(U u) {
        this.u = u;
    }

    public T getFirst() {
        return t;
    }

    public S getSecond() {
        return s;
    }

    public U getThird() {
        return u;
    }

    public String toString() {
        return t + "\n" + s + "\n" + u + "\n";
    }

    public boolean isHomogeneous() {
        return t.getClass() == s.getClass() && t.getClass() == u.getClass();
    }

    public void tripleSort() {
        try {
            for (int i = 1; i < array.size(); ++i) {
                Object key = array.get(i);
                int j = i - 1;
                while (j > -1 && array.get(i) > key) {
                    array.set(j + 1, array.get(j));
                    j--;
                }
                array.set(j + 1, key);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 您只能将原语与这些运算符进行比较。这些对象是 Comparable 吗?
  • 由于一个对象是任何东西,问某物是否“大于”它是什么意思?
  • 其实你可以忽略我的问题。您只能比较相同的类型

标签: java sorting comparison


【解决方案1】:

您提供的代码有两个主要问题:

  • 可以使用关系运算符&lt;&lt;=&gt;&gt;=只要比较数字原语类型。显然,您可以将它与对象一起使用。
  • 要比较引用类型,您可以使用Comparator 或者这些对象可以实现Comparable 接口(即他们基本上知道如何比较自己)。因为比较BigDecimalBoolean,或者StringHashMap 是不行的,你会怎么处理呢?因此,这些接口是通用的,Comparator&lt;T&gt; 不能与U 类型的对象一起使用。

也就是说,您的 Triple&lt;T, S, U&gt; 将无法对属于不同类型的这些对象做很多事情(绝对不能对它们进行排序)。

因此,如果您需要一个数据载体持有三种不同类型的引用,很好。它仍然很有用,但不要期望太多。

Java 16 record 非常适合这个角色:

public record Triple<T, S, U>(T first, S second, U third) {}

但是,如果您需要能够对这些值进行操作,然后将它们相互比较,那么请考虑将Triple 更改为仅包含T 类型的元素。

这是一个如何实现它的示例:

public static class Triple<T> {
    private List<T> list = new ArrayList<>(3);
    private Comparator<T> comp;
    
    private Triple(T first, T second, T third, Comparator<T> comp) { // no way and no need to invoke this constructor outside the class
        this.comp = comp;
        Collections.addAll(list, first, second, third);
    }
    
    public static <T> Triple<T> getInstance(T first, T second, T third, Comparator<T> comp) {
        Triple<T> triple = new Triple<>(first, second, third, comp);
        triple.init();
        return triple;
    }
    
    public void init() {
        list.sort(comp);
    }
    
    public T getFirst() {
        return list.get(0);
    }
    
    public T getSecond() {
        return list.get(1);
    }
    
    public T getThird() {
        return list.get(2);
    }

    public boolean isHomogeneous() {
        return comp.compare(getFirst(), getSecond()) == 0
            && comp.compare(getFirst(), getThird()) == 0
            && comp.compare(getSecond(), getThird()) == 0;
    }
    
    public String toString() {
        return list.stream().map(T::toString).collect(Collectors.joining("
"));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多