@Test public void testPerformance() { Random rand = new Random(System.currentTimeMillis()); ArrayList<Double> doubleList = new ArrayList<>(); int count = 10 * 10000; for (int i = 0; i < count; i++) { doubleList.add(rand.nextDouble()); } doubleList.add(0.0); doubleList.add(0.0); doubleList.add(-1.0); doubleList.add(Double.MAX_VALUE); doubleList.add(-Double.MAX_VALUE); ArrayList<String> strNumList = listToStringList(doubleList); ArrayList<SelfDefineSortableKey> keyList = createKeyList(strNumList, (byte) SelfDefineSortableKey.TypeFlag.DOUBLE_FAMILY_TYPE.ordinal()); System.out.println("start to test str sort"); long start = System.currentTimeMillis(); Collections.sort(strNumList); System.out.println("sort time : " + (System.currentTimeMillis() - start)); System.out.println("start to test double sort"); start = System.currentTimeMillis(); Collections.sort(keyList); System.out.println("sort time : " + (System.currentTimeMillis() - start)); List<ByteArray> byteList = new ArrayList<>(); for (String str : strNumList) { byteList.add(new ByteArray(Bytes.toBytes(str))); } System.out.println("start to test byte array sort"); start = System.currentTimeMillis(); Collections.sort(byteList); System.out.println("sort time : " + (System.currentTimeMillis() - start)); List<SelfDefineSortableKey> newKeyList = new ArrayList<>(); for (String str : strNumList) { SelfDefineSortableKey key = new SelfDefineSortableKey(); key.init(new Text(str), (byte) SelfDefineSortableKey.TypeFlag.DOUBLE_FAMILY_TYPE.ordinal()); newKeyList.add(key); } System.out.println("start to test new sortable key"); start = System.currentTimeMillis(); Collections.sort(newKeyList); System.out.println("sort time : " + (System.currentTimeMillis() - start)); }
public void init(Text key, byte typeId) { this.typeId = typeId; this.rawKey = key; if (isNumberFamily()) { String valueStr = new String(key.getBytes(), 1, key.getLength() - 1, StandardCharsets.UTF_8); if (isIntegerFamily()) { this.keyInObj = Long.parseLong(valueStr); } else { this.keyInObj = Double.parseDouble(valueStr); } } else { this.keyInObj = key; } }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { public void init(Text key, byte typeId) { this.typeId = typeId; this.rawKey = key; if (isNumberFamily()) { String valueStr = new String(key.getBytes(), 1, key.getLength() - 1, StandardCharsets.UTF_8); if (isIntegerFamily()) { this.keyInObj = Long.parseLong(valueStr); } else { this.keyInObj = Double.parseDouble(valueStr); } } else { this.keyInObj = key; } } }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { public void init(Text key, byte typeId) { this.typeId = typeId; this.rawKey = key; if (isNumberFamily()) { String valueStr = new String(key.getBytes(), 1, key.getLength() - 1, StandardCharsets.UTF_8); if (isIntegerFamily()) { this.keyInObj = Long.parseLong(valueStr); } else { this.keyInObj = Double.parseDouble(valueStr); } } else { this.keyInObj = key; } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { public void init(Text key, byte typeId) { this.typeId = typeId; this.rawKey = key; if (isNumberFamily()) { String valueStr = new String(key.getBytes(), 1, key.getLength() - 1, StandardCharsets.UTF_8); if (isIntegerFamily()) { this.keyInObj = Long.parseLong(valueStr); } else { this.keyInObj = Double.parseDouble(valueStr); } } else { this.keyInObj = key; } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); void init(Text key, byte typeId); void init(Text key, DataType type); @Override int compareTo(SelfDefineSortableKey o); @Override void write(DataOutput dataOutput); @Override void readFields(DataInput dataInput); short getTypeId(); Text getText(); boolean isNumberFamily(); boolean isIntegerFamily(); byte getTypeIdByDatatype(DataType type); void setTypeId(byte typeId); }
SelfDefineSortableKey implements WritableComparable<SelfDefineSortableKey> { public void init(Text key, byte typeId) { this.typeId = typeId; this.rawKey = key; if (isNumberFamily()) { String valueStr = new String(key.getBytes(), 1, key.getLength() - 1, StandardCharsets.UTF_8); if (isIntegerFamily()) { this.keyInObj = Long.parseLong(valueStr); } else { this.keyInObj = Double.parseDouble(valueStr); } } else { this.keyInObj = key; } } SelfDefineSortableKey();  SelfDefineSortableKey(Text key, DataType type); void init(Text key, byte typeId); void init(Text key, DataType type); @Override int compareTo(SelfDefineSortableKey o); @Override void write(DataOutput dataOutput); @Override void readFields(DataInput dataInput); short getTypeId(); Text getText(); boolean isNumberFamily(); boolean isIntegerFamily(); byte getTypeIdByDatatype(DataType type); void setTypeId(byte typeId);  }