jjzjj

comparable

全部标签

java - 如何在我的泛型方法中限制只接受一种类型?

我有一个通用函数foo,它接受任何类型并将它们打印出来。publicstaticTfoo(T...arg){Listfoo=Arrays.asList(arg);for(Tt:foo){System.out.println(t);}returnnull;}如何确保收到的参数只有一种类型。例如,{1,'a',3}应该是无效的。它应该全是数字或全是字符。我想接受所有整数或所有字符。 最佳答案 实际上你可以这样做:static>voidf(T...args){System.out.println(java.util.Arrays.toSt

java - Comparable 接口(interface)前无接口(interface)词

这是细节,但我想知道为什么会这样。示例代码:Classklasa=Enum.class;for(Typet:klasa.getGenericInterfaces())System.out.println(t);程序输出:java.lang.Comparableinterfacejava.io.Serializable为什么在输出java.lang.Comparable之前没有接口(interface)词.是界面,是吗?在我看来输出应该是:**interface**java.lang.Comparableinterfacejava.io.Serializable比较特殊处理?

Java 8 通用函数应该是模棱两可的,但在运行时失败

我正在尝试将Java7代码迁移到Java8,所以我的代码类似于:packagetests;importjava.util.Arrays;importjava.util.Map;publicclassTests{privatestaticinterfaceComparableMapextendsMap,Comparable{}publicstaticvoidmain(String[]args){func(getString());}privatestaticvoidfunc(Comparable...input){System.out.println(Arrays.toString(in

java - 无法将列表转换为 Java 泛型中的列表错误

以下是我简化的图形实现importjava.util.ArrayList;importjava.util.List;publicclassTreeNode>{privateEdata;privateList>children;publicTreeNode(Evalue){data=value;children=newArrayList();}publicEgetData(){returndata;}publicvoidsetData(Edata){this.data=data;}publicList>getChildren(){returnchildren;}publicvoidset

java - 转换为 Comparable,然后进行比较

我真的很想喜欢泛型,但到目前为止,它们造成的麻烦超过了任何好处。请告诉我我错了。我理解在使用非通用框架(Spring、Hibernate)时添加@SuppressWarnings("unchecked")的必要性。仅这一点就真正降低了泛型的值(value),因为要求将类传递给构造函数以避免删除的陷阱。然而,真正的刺似乎总是在类型转换。我通常会尝试一段时间来获得正确的语法,但随后放弃纯洁的尝试,添加一个@SuppressWarnings,然后继续我的生活。这是一个例子:我正在反射(reflection)一个bean以寻找两个实例之间的差异。一些属性实现Comparable使得(a.equ

Java 8 : implementing Comparable

我喜欢Comparator的新静态工厂方法,因为它们允许以非常简洁且不易出错的方式实现比较器。但是实现Comparable的推荐方法是什么?我们应该在Comparable实现中使用Comparators吗?publicMyClassimplementsComparable{...publicintcompareTo(MyClassother){ComparatornaturalOrderComparator=Comparator.comparing(MyClass::getFoo).thenComparing(MyClass::getBar);returnnaturalOrderCom

Java:方法参数中的协变通配符边界

我对通配符边界的规则感到困惑。好像有时候声明一个bound不满足类声明的bound的方法参数是可以的。在下面的代码中,方法foo(...)可以正常编译,但bar(...)不能。我不明白为什么允许其中任何一个。publicclassTestSomething{privatestaticclassA{}publicstaticvoidfoo(A>a){}publicstaticvoidbar(A>a){}} 最佳答案 让我们首先考虑方法voidfoo(A>a).A>与A“兼容”因为存在通配符类型P,和一个类似的通配符类型Q满足以下条件:

java - 如何让 Comparator.comparing 正确推断类型参数?

我有以下类(class):importjava.util.ArrayList;importjava.util.Comparator;importjava.util.List;importorg.apache.commons.lang3.tuple.Pair;publicclassSorter{privateList>dtoPairs=newArrayList();publicSorter(){Comparator>bySize=Comparator.comparing(Pair::getLeft);Comparator>byName=Comparator.comparing(p->p.

Java:使用 Collat​​orKey 对集合进行排序

我想实现的是按字符串值对对象集合进行排序。但是,以依赖于语言环境的方式使用整理器。由于性能原因,我不想使用Collat​​orcompare()方法(如下面的代码),而是使用Collat​​ionKey类,因为javaAPI声明使用Collat​​ionKey更快。但是如何使用Collat​​ionKey实现compareTo()方法呢?据我了解,如果我要使用Collat​​ionKey,我必须自己完全编写所有比较方法。所以我什至无法再使用Collections.sort()方法...我非常感谢一个易于理解的示例和使用Collat​​ionKey对Person对象的Collection

java - 从另一个函数返回 `Comparator`

首先,请让我清楚,我受API设计的限制,所以请不要更改API,但是可以添加私有(private)函数。publicclassPointimplementsComparable{publicPoint(intx,inty)//constructsthepoint(x,y)publicvoiddraw()//drawsthispointpublicvoiddrawTo(Pointthat)//drawsthelinesegmentfromthispointtothatpointpublicStringtoString()//stringrepresentationpublicintcomp