Table
當我們需要多個索引的資料結構的時候,通常情況下,我們只能用這種醜陋的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視圖:
rowMap()返回一個Map用來處理一連串不連續,非空的range。當添加一個range到一個RangeSet之後,任何有連續的range將自動合併,而空的range將會自動移除。例如:
[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; } }
注意,像合併Range.closed(1, 10)和Range.closedOpen(11, 15)這樣的情況,我們必須先用呼叫Range.canonical(DiscreteDomain)傳入DiscreteDomain.integers()處理一下。
RangeSet的視圖
RangeSet的實現支持了十分豐富的視圖,包括:
complement():是個輔助的RangeSet,它本身就是一個非空的,因為它本身就是一個非空,了,它本身就是RangeSet,它本身就是一個非空的,
subRangeSet(Range): 回傳的是一個交集的視圖。
asRanges():傳回可以被迭代的Set
[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); }