Table
当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map
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));
}
{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
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用来处理一系列不连续,非空的range。当添加一个range到一个RangeSet之后,任何有连续的range将被自动合并,而空的range将被自动去除。例如:
@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);
}
注意,像合并Range.closed(1, 10)和Range.closedOpen(11, 15)这样的情况,我们必须先用调用Range.canonical(DiscreteDomain)传入DiscreteDomain.integers()处理一下。
RangeSet的视图
RangeSet的实现支持了十分丰富的视图,包括:
complement():是个辅助的RangeSet,它本身就是一个RangeSet,因为它包含了非连续,非空的range。
subRangeSet(Range): 返回的是一个交集的视图。
asRanges():返回可以被迭代的Set
@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的视图
RangeMap提供了两种视图:
asMapOfRanges():返回Map