Table
When we need a data structure with multiple indexes, usually we can only use this ugly Map
[code]Test public void TableTest(){ Table<String, Integer, String> aTable = HashBasedTable.create(); for (char a = 'A'; a <= 'C'; ++a) { for (Integer b = 1; b <= 3; ++b) { aTable.put(Character.toString(a), b, String.format("%c%d", a, b)); } } System.out.println(aTable.column(2)); System.out.println(aTable.row("B")); System.out.println(aTable.get("B", 2)); System.out.println(aTable.contains("D", 1)); System.out.println(aTable.containsColumn(3)); System.out.println(aTable.containsRow("C")); System.out.println(aTable.columnMap()); System.out.println(aTable.rowMap()); System.out.println(aTable.remove("B", 3)); }
[code]{A=A2, B=B2, C=C2} {1=B1, 2=B2, 3=B3} B2 false true true {1={A=A1, B=B1, C=C1}, 2={A=A2, B=B2, C=C2}, 3={A=A3, B=B3, C=C3}} {A={1=A1, 2=A2, 3=A3}, B={1=B1, 2=B2, 3=B3}, C={1=C1, 2=C2, 3=C3}} B3
Table view:
RowMap() returns a Map
[code]import com.google.common.collect.ClassToInstanceMap; import com.google.common.collect.HashBasedTable; import com.google.common.collect.MutableClassToInstanceMap; public class OtherTest { @Test public void ClassToInstanceMapTest() { ClassToInstanceMap<String> classToInstanceMapString =MutableClassToInstanceMap.create(); ClassToInstanceMap<Person> classToInstanceMap =MutableClassToInstanceMap.create(); Person person= new Person("peida",20); System.out.println("person name :"+person.name+" age:"+person.age); classToInstanceMapString.put(String.class, "peida"); System.out.println("string:"+classToInstanceMapString.getInstance(String.class)); classToInstanceMap.putInstance(Person.class,person); Person person1=classToInstanceMap.getInstance(Person.class); System.out.println("person1 name :"+person1.name+" age:"+person1.age); } } class Person { public String name; public int age; Person(String name, int age) { this.name = name; this.age = age; } }
RangeSet
RangeSet is used to process a series of discontinuous, non-empty ranges. When adding a range to a RangeSet, any consecutive ranges will be automatically merged, and empty ranges will be automatically removed. For example:
[code] @Test public void RangeSetTest(){ RangeSet<Integer> rangeSet = TreeRangeSet.create(); rangeSet.add(Range.closed(1, 10)); System.out.println("rangeSet:"+rangeSet); rangeSet.add(Range.closedOpen(11, 15)); System.out.println("rangeSet:"+rangeSet); rangeSet.add(Range.open(15, 20)); System.out.println("rangeSet:"+rangeSet); rangeSet.add(Range.openClosed(0, 0)); System.out.println("rangeSet:"+rangeSet); rangeSet.remove(Range.open(5, 10)); System.out.println("rangeSet:"+rangeSet); }
Note that in cases like merging Range.closed(1, 10) and Range.closedOpen(11, 15), we must first pass in DiscreteDomain.integers by calling Range.canonical(DiscreteDomain) () Process it.
Views of RangeSet
The implementation of RangeSet supports a very rich view, including:
complement(): It is an auxiliary RangeSet, which itself is a RangeSet, because it Contains non-continuous, non-empty ranges.
subRangeSet(Range): Returns an intersection view.
asRanges(): Returns the view of the Set
[code] @Test public void RangeMapTest(){ RangeMap<Integer, String> rangeMap = TreeRangeMap.create(); rangeMap.put(Range.closed(1, 10), "foo"); System.out.println("rangeMap:"+rangeMap); rangeMap.put(Range.open(3, 6), "bar"); System.out.println("rangeMap:"+rangeMap); rangeMap.put(Range.open(10, 20), "foo"); System.out.println("rangeMap:"+rangeMap); rangeMap.remove(Range.closed(5, 11)); System.out.println("rangeMap:"+rangeMap); } 输出: rangeMap:[[1‥10]=foo] rangeMap:[[1‥3]=foo, (3‥6)=bar, [6‥10]=foo] rangeMap:[[1‥3]=foo, (3‥6)=bar, [6‥10]=foo, (10‥20)=foo] rangeMap:[[1‥3]=foo, (3‥5)=bar, (11‥20)=foo]
RangeMap that can be iterated
RangeMap provides two views:
asMapOfRanges(): Returns Map
The above is the content of Java-Class Library-Guava-Table. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!