Code example for TreeMap

Methods: ceilingKey, containsKey, firstKey, get, isEmpty

0
 
 
    private String findConsumerByPartition(final TreeMap<Long, String> consumerMap, final String partition) {
        final Long hash = this.alg.hash(partition);
        Long target = hash;
        if (!consumerMap.containsKey(hash)) {
            target = consumerMap.ceilingKey(hash);
            // if (hash == null) { 
            // target = consumerMap.floorKey(hash); 
            // } 
            // else { 
            // final Long floor = consumerMap.floorKey(hash); 
            // if (floor != null) { 
            // target = Math.abs(hash - floor) > Math.abs(hash - target) ? 
            // target : floor; 
            // } 
            // } 
            if (target == null && !consumerMap.isEmpty()) {
                target = consumerMap.firstKey();
            } 
        } 
        final String targetConsumer = consumerMap.get(target);
        return targetConsumer;
    } 
 
 
    private TreeMap<Long, String> buildConsumerMap(final List<String> curConsumers) {