public void put(Range<R> range, K value) { RangeMap<R, List<K>> subRangeMap = map.subRangeMap(range); Map<Range<R>, List<K>> submap = subRangeMap.asMapOfRanges(); if (submap.isEmpty()) { ArrayList<K> list = new ArrayList<K>(); list.add(value); map.put(range, list); } else { RangeSet<R> notCoveredSpan = TreeRangeSet.create(); notCoveredSpan.add(range); List<Pair<Range<R>, List<K>>> newElements = new ArrayList<Pair<Range<R>, List<K>>>(); for (Entry<Range<R>, List<K>> mapEntry : submap.entrySet()) { List<K> newValue = new ArrayList<K>(mapEntry.getValue()); newValue.add(value); newElements.add(new ImmutablePair<Range<R>, List<K>>(mapEntry.getKey(), newValue)); notCoveredSpan.remove(mapEntry.getKey()); } for (Pair<Range<R>, List<K>> el : newElements) { map.put(el.getLeft(), el.getRight()); } if (!notCoveredSpan.isEmpty()) { for (Range<R> notYetCoveredSpan : notCoveredSpan.asRanges()) { ArrayList<K> list = new ArrayList<K>(); list.add(value); map.put(notYetCoveredSpan, list); } } } }