*********************************************** Declaration *********************** *******************************
Original works, from the "Xiaofeng Moon XJ" blog, Welcome to reprint, please be sure to indicate the source (HTTP://BLOG.CSDN.NET/XIAOFENGCANYUEXJ).
Due to various reasons, there may be many shortcomings, welcome treatise!
***************
******************************************************************************************
RangeMap is a collection type that maps disjoint, non-empty range (key) to a value (value), RangeMap cannot merge adjacent intervals, even if the value of the interval map is the same, the implementation RangeMap is also an interface. There are only two classes to implement it, Mmutablerangemap and Treerangemap, respectively. Where Treerangemap is key in order. The specific interval sorting rules are no longer said.
private static void Testrangemap () {rangemap<integer, string> RangeMap = Treerangemap.create (); Rangemap.put (range.closed (1, ten), "AAA"); System.out.println (RangeMap); Rangemap.put (Range.open (3, 6), "BBB"); System.out.println (RangeMap); Rangemap.put (range.openclosed (Ten), "AAA"); System.out.println (RangeMap); Rangemap.put (range.closed (), "AAA"); System.out.println (RangeMap); Rangemap.remove (range.closed (5, 11)); System.out.println (RangeMap); The input results are: [[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] method of traversing RangeMap private static void Printtoconsole (Treerangemap<item, integer> rangemap) {map<range<item>, Integer> rang Estringmap = Rangemap.asmapofranges (); Set<map.entry<range<item>, integer>> Entries = Rangestringmap.entryset (); for (MAP.ENTRY<RANGE<ITEM>, integer> next:entries) {Item-left = Next.getkey (). Lowerendpoint (); Item right = Next.getkey (). Upperendpoint (); String lines = left + "~" + Right + "" + next.getvalue (); SYSTEM.OUT.PRINTLN (lines); } }
the custom object for range range must implement the comparable interface so that interval partitioning can be ensured. as follows
/** * Date class, storage date */public class Item implements comparable<item> {private int year, month, day; public Item (int ye, int mon, int da) {this.year = ye; This.month = Mon; This.day = da; Public item () {} public void SetItem (item item) {this.year = Item.year; This.month = Item.month; This.day = Item.day; } @Override public int compareTo (item item) {if (This.year < item.year) return-1; else if (this.year = = item.year) {if (This.month < item.month) return-1; else if (This.month = = Item.month) {if (This.day < item.day) return-1; else if (This.day = = Item.day) return 0; else return 1; } else return 1; } else return 1; Public String toString () {String date = ""; if (0 = = This. month/10) Date = This.year + "-" + "0" + this.month; else date = This.year + "-" + this.month; if (0 = = THIS.DAY/10) Date = date + "-" + "0" + this.day; else Date = date + "-" + this.day; return date; }}
due to the limited time, in the process of writing a few references to some of the literature, thank you, at the same time, given the level of reasons, you inevitably have shortcomings, welcome treatise!
RangeMap custom range range in guava