In RangeSet of Guava learning, we talked about the features and usage of RangeSet. There are many differences between RangeMap and RangeSet.
On the official API of Google Guava, we can know that RangeMap is a collection type that does not intersection and is not an empty Range (key) map to a Value ). Unlike RangeSet, RangeMap cannot merge adjacent intervals even if the ing value is the same.
Like RangeSet, RangeMap is also an interface that implements only two classes, mmutableRangeMap and TreeRangeMap. TreeRangeMap is often used. The following describes RangeMap with TreeRangeMap. You can add or delete objects in RangeMap as follows:
public RangeMap<Integer, String> testRangeMap() { RangeMap<Integer, String> rangeMap = TreeRangeMap.create(); rangeMap.put(Range.closed(1, 10), "aaa"); System.out.println(rangeMap); rangeMap.put(Range.open(3, 6), "bbb"); System.out.println(rangeMap); rangeMap.put(Range.openClosed(10, 20), "aaa"); System.out.println(rangeMap); rangeMap.put(Range.closed(20, 20), "aaa"); System.out.println(rangeMap); rangeMap.remove(Range.closed(5, 11)); System.out.println(rangeMap); return rangeMap;}
The running result of the above function is as follows:
[[1‥10]=aaa][[1‥3]=aaa, (3‥6)=bbb, [6‥10]=aaa][[1‥3]=aaa, (3‥6)=bbb, [6‥10]=aaa, (10‥20]=aaa][[1‥3]=aaa, (3‥6)=bbb, [6‥10]=aaa, (10‥20)=aaa, [20‥20]=aaa][[1‥3]=aaa, (3‥5)=bbb, (11‥20)=aaa, [20‥20]=aaa]
The result shows that each Range in RangeMap corresponds to a value. Note that the running result (10 minutes 20) = aaa, [20 minutes 20] = aaa, is not merged! This is a difference between RangeMap and RangeSet.
Similarly, to traverse all the elements in rangeMap, use the following method:
public void iteratorRangeMap(RangeMap<Integer, String> integerStringRangeMap) { if(integerStringRangeMap == null){ return; } Map<Range,String> rangeStringMap = integerStringRangeMap.asMapOfRanges(); Set<Map.Entry<Range, String>> entries = rangeStringMap.entrySet(); Iterator<Map.Entry<Range, String>> iterator = entries.iterator(); while(iterator.hasNext()){ Map.Entry<Range, String> next = iterator.next(); System.out.println(next.getKey() + "\t" + next.getValue()); }}
The asMapOfRanges () method obtains the RangeMap <Range, V> View, which can be used to traverse RangeMap. The running result is as follows:
[1‥3]aaa(3‥5)bbb(11‥20)aaa[20‥20]aaa
RangeStringMap also provides the keySet () method to set the key, as follows:
Set<Range> ranges = rangeStringMap.keySet();Iterator<Range> iterator1 = ranges.iterator();while(iterator1.hasNext()){ Range next = iterator1.next(); System.out.println(next + "\t" + rangeStringMap.get(next));}
The running result is the same as the preceding one, but the first Traversal method is recommended. In the second method, rangeStringMap. get (next) is used to traverse rangeStringMap. When rangeStringMap is accessed, the efficiency is slow.
Unlike RangeSet, RangeMap does not provide the complement (), contains (), rangeContaining (), and encloses () methods.
(End)
Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
RangeMap (http://www.wypblog.com/archives/546) for Guava Learning)