List<Number>
进行排序?示例:
List<Number> li = new ArrayList<Number>(); //list of numbers
li.add(new Integer(20));
li.add(new Double(12.2));
li.add(new Float(1.2));
List<Number>
进行排序?List<Number> li = new ArrayList<Number>(); //list of numbers
li.add(new Integer(20));
li.add(new Double(12.2));
li.add(new Float(1.2));
Collections.sort(li,new Comparator<Number>() {
@Override
public int compare(Number o1, Number o2) {
Double d1 = (o1 == null) ? Double.POSITIVE_INFINITY : o1.doubleValue();
Double d2 = (o2 == null) ? Double.POSITIVE_INFINITY : o2.doubleValue();
return d1.compareTo(d2);
}
});
请查看Andreas_D的答案以获取解释。在上面的代码中,所有的null值和+Infinity值都被处理,以便它们移动到末尾。
更新1:
正如jarnbjo和aioobe指出上述实现方案存在一个缺陷。因此,我认为最好限制Number的实现。
Collections.sort(li, new Comparator<Number>() {
HashSet<Class<? extends Number>> allowedTypes;
{
allowedTypes = new HashSet<Class<? extends Number>>();
allowedTypes.add(Integer.class);
allowedTypes.add(Double.class);
allowedTypes.add(Float.class);
allowedTypes.add(Short.class);
allowedTypes.add(Byte.class);
}
@Override
public int compare(Number o1, Number o2) {
Double d1 = (o1 == null) ? Double.POSITIVE_INFINITY : o1.doubleValue();
Double d2 = (o2 == null) ? Double.POSITIVE_INFINITY : o2.doubleValue();
if (o1 != null && o2 != null) {
if (!(allowedTypes.contains(o1.getClass()) && allowedTypes.contains(o2.getClass()))) {
throw new UnsupportedOperationException("Allowed Types:" + allowedTypes);
}
}
return d1.compareTo(d2);
}
});
更新2:
使用 guava 的 受限制的列表 (不允许将空值或不支持的类型添加到列表中):
List<Number> li = Constraints.constrainedList(new ArrayList<Number>(),
new Constraint<Number>() {
HashSet<Class<? extends Number>> allowedTypes;
{
allowedTypes = new HashSet<Class<? extends Number>>();
allowedTypes.add(Integer.class);
allowedTypes.add(Double.class);
allowedTypes.add(Float.class);
allowedTypes.add(Short.class);
allowedTypes.add(Byte.class);
}
@Override
public Number checkElement(Number arg0) {
if (arg0 != null) {
if (allowedTypes.contains(arg0.getClass())) {
return arg0;
}
}
throw new IllegalArgumentException("Type Not Allowed");
}
}
);
li.add(Double.POSITIVE_INFINITY);
li.add(new Integer(20));
li.add(new Double(12.2));
li.add(new Float(1.2));
li.add(Double.NEGATIVE_INFINITY);
li.add(Float.NEGATIVE_INFINITY);
// li.add(null); //throws exception
// li.add(new BigInteger("22"); //throws exception
li.add(new Integer(20));
System.out.println(li);
Collections.sort(li, new Comparator<Number>() {
@Override
public int compare(Number o1, Number o2) {
Double d1 = o1.doubleValue();
Double d2 = o2.doubleValue();
return d1.compareTo(d2);
}
});
System.out.println(li);
Comparator<Number>
,因为Number
的实例可能表示大于Double.MAX_VALUE
的数字(而这恰好是Number
接口允许我们“看到”的最大值)。一个大于Double.MAX_VALUE
的Number
示例为:new BigDecimal("" + Double.MAX_VALUE).multiply(BigDecimal.TEN)
Byte
、Short
、Integer
、Long
、Float
和 Double
任意大的 BigInteger
任意大的 BigDecimal
{Double, Float}.NEGATIVE_INFINITY
和 {Double, Float}.POSITIVE_INFINITY
的实例
请注意,即使 BigDecimal.doubleValue
可能会返回 Double.NEGATIVE_INFINITY
或 Double.POSITIVE_INFINITY
,这些实例也应该在任何 BigDecimal
之前 / 之后。
null
元素
上述所有元素的混合,以及
未知实现 Comparable
接口的 Number
实现。
(这似乎是一个合理的假设,因为标准 API 中的所有 Number
都实现了 Comparable 接口。)
@SuppressWarnings("unchecked")
class NumberComparator implements Comparator<Number> {
// Special values that are treated as larger than any other.
private final static List<?> special =
Arrays.asList(Double.NaN, Float.NaN, null);
private final static List<?> largest =
Arrays.asList(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
private final static List<?> smallest =
Arrays.asList(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
public int compare(Number n1, Number n2) {
// Handle special cases (including null)
if (special.contains(n1)) return 1;
if (special.contains(n2)) return -1;
if (largest.contains(n1) || smallest.contains(n2)) return 1;
if (largest.contains(n2) || smallest.contains(n1)) return -1;
// Promote known values (Byte, Integer, Long, Float, Double and
// BigInteger) to BigDecimal, as this is the most generic known type.
BigDecimal bd1 = asBigDecimal(n1);
BigDecimal bd2 = asBigDecimal(n2);
if (bd1 != null && bd2 != null)
return bd1.compareTo(bd2);
// Handle arbitrary Number-comparisons if o1 and o2 are of same class
// and implements Comparable.
if (n1 instanceof Comparable<?> && n2 instanceof Comparable<?>)
try {
return ((Comparable) n1).compareTo((Comparable) n2);
} catch (ClassCastException cce) {
}
// If the longValue()s differ between the two numbers, trust these.
int longCmp = ((Long) n1.longValue()).compareTo(n2.longValue());
if (longCmp != 0)
return longCmp;
// Pray to god that the doubleValue()s differ between the two numbers.
int doubleCmp = ((Double) n1.doubleValue()).compareTo(n2.doubleValue());
if (doubleCmp != 0)
return longCmp;
// Die a painful death...
throw new UnsupportedOperationException(
"Cannot compare " + n1 + " with " + n2);
}
// Convert known Numbers to BigDecimal, and the argument n otherwise.
private BigDecimal asBigDecimal(Number n) {
if (n instanceof Byte) return new BigDecimal((Byte) n);
if (n instanceof Integer) return new BigDecimal((Integer) n);
if (n instanceof Short) return new BigDecimal((Short) n);
if (n instanceof Long) return new BigDecimal((Long) n);
if (n instanceof Float) return new BigDecimal((Float) n);
if (n instanceof Double) return new BigDecimal((Double) n);
if (n instanceof BigInteger) return new BigDecimal((BigInteger) n);
if (n instanceof BigDecimal) return (BigDecimal) n;
return null;
}
}
public class Main {
public static void main(String[] args) {
List<Number> li = new ArrayList<Number>();
// Add an Integer, a Double, a Float, a Short, a Byte and a Long.
li.add(20); li.add((short) 17);
li.add(12.2); li.add((byte) 100);
li.add(0.2f); li.add(19518926L);
li.add(Double.NaN); li.add(Double.NEGATIVE_INFINITY);
li.add(Float.NaN); li.add(Double.POSITIVE_INFINITY);
// A custom Number
li.add(new BoolNumber(1));
li.add(new BoolNumber(0));
// Add two BigDecimal that are larger than Double.MAX_VALUE.
BigDecimal largeDec = new BigDecimal("" + Double.MAX_VALUE);
li.add(largeDec/*.multiply(BigDecimal.TEN)*/);
li.add(largeDec.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN));
// Add two BigInteger that are larger than Double.MAX_VALUE.
BigInteger largeInt = largeDec.toBigInteger().add(BigInteger.ONE);
li.add(largeInt.multiply(BigInteger.TEN));
li.add(largeInt.multiply(BigInteger.TEN).multiply(BigInteger.TEN));
// ...and just for fun...
li.add(null);
Collections.shuffle(li);
Collections.sort(li, new NumberComparator());
for (Number num : li)
System.out.println(num);
}
static class BoolNumber extends Number {
boolean b;
public BoolNumber(int i) { b = i != 0; }
public double doubleValue() { return b ? 1d : 0d; }
public float floatValue() { return b ? 1f : 0f; }
public int intValue() { return b ? 1 : 0; }
public long longValue() { return b ? 1L : 0L; }
public String toString() { return b ? "1" : "0"; }
}
}
...它会打印出以下结果(我去掉了一些零):
-Infinity
0
0.2
1
12.2
17
20
100
19518926
1.7976931348623157E+308
17976931348623157000000000...00000000010
1.797693134862315700E+310
179769313486231570000000000000...00000100
Infinity
NaN
null
NaN
Number
进行排序的方法,而不是对Integer
、Double
、Float
、Short
、Byte
等混合类型进行排序的方法,对吗?我认为这个问题无法完全解决,但你可以接近解决。你说:“此外,我们不能从数字实例中获取大于double的数字。”这取决于你是否指的是Number
这个类。(当然,我们可以从Number
中获取大于double的数字。) - aioobe由于集合中可能存在null
值,因此您需要一个处理方案 - 您无法创建不接受null
的对象集合。
所以,您可以检查是否为null
并抛出IllegalArgumentException
异常 - 副作用是您将无法对"污染"列表进行排序,并且必须在运行时处理这些异常。
另一种想法是将null
转换为某种数字。我展示了这种方法(基于您自己的答案),通过惯例将任何null
转换为Double.NaN
。如果您希望将null
值排序到两端,则还可以考虑将它们转换为0
或Double.POSITIVE_INFINITY
或Double.NEGATIVE_INFINITY
。
Collections.sort(li,new Comparator<Number>() {
@Override
public int compare(Number o1, Number o2) {
// null values converted to NaN by convention
Double d1= (o1 == null) ? Double.NaN : o1.doubleValue();
Double d2= (o2 == null) ? Double.NaN : o2.doubleValue();
return d1.compareTo(d2);
}
});
更多信息
以下是一些代码,展示了如何通过“默认”处理这些特殊值:
Set<Double> doubles = new TreeSet<Double>();
doubles.add(0.);
// doubles.add(null); // uncommenting will lead to an exception!
doubles.add(Double.NaN);
doubles.add(Double.POSITIVE_INFINITY);
doubles.add(Double.NEGATIVE_INFINITY);
for (Double d:doubles) System.out.println(d);
没有添加null
的结果是:
-Infinity
0.0
Infinity
NaN
new BigDecimal("" + Double.MAX_VALUE).multiply(BigDecimal.TEN)
视为大于 Double.POSITIVE_INFINITY
。 - aioobe试试我的Java排序算法:
package drawFramePackage;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Random;
public class QuicksortAlgorithm {
ArrayList<AffineTransform> affs;
ListIterator<AffineTransform> li;
Integer count, count2;
/**
* @param args
*/
public static void main(String[] args) {
new QuicksortAlgorithm();
}
public QuicksortAlgorithm(){
count = new Integer(0);
count2 = new Integer(1);
affs = new ArrayList<AffineTransform>();
for (int i = 0; i <= 128; i++) {
affs.add(new AffineTransform(1, 0, 0, 1, new Random().nextInt(1024), 0));
}
affs = arrangeNumbers(affs);
printNumbers();
}
public ArrayList<AffineTransform> arrangeNumbers(ArrayList<AffineTransform> list) {
while (list.size() > 1 && count != list.size() - 1) {
if (list.get(count2).getTranslateX() > list.get(count).getTranslateX()) {
list.add(count, list.get(count2));
list.remove(count2 + 1);
}
if (count2 == list.size() - 1) {
count++;
count2 = count + 1;
} else {
count2++;
}
}
return list;
}
public void printNumbers(){
li = affs.listIterator();
while (li.hasNext()) {
System.out.println(li.next());
}
}
}
compare
方法中,检查o1和o2是否是Comparable
的实例。如果是,则Java代码已经为数字类型实现了最佳比较 - 通过调用o1.compareTo(o2)
来使用它。如果它没有实现Comparable
,我建议使用BigDecimal
而不是Double
。 - madhurtanwani