Guava Study Notes: Guava new set-Table, etc.

Source: Internet
Author: User

Table

 When we need data structures with Multiple indexes, we can only use this ugly Map <FirstName, Map <LastName, Person>. Therefore, Guava provides a new set type-Table set type to Support the use scenario of this data structure. Table supports "row" and "column" and provides multiple views.

 <String, Integer, String> aTable = ( a = 'A'; a <= 'C'; ++ (Integer b = 1; b <= 3; ++"%c%d"2"B""B", 2"D", 13"C""B", 3

Output:

{A=A2, B=B2, C=1=B1, 2=B2, 3=1={A=A1, B=B1, C=C1}, 2={A=A2, B=B2, C=C2}, 3={A=A3, B=B3, C=={1=A1, 2=A2, 3=A3}, B={1=B1, 2=B2, 3=B3}, C={1=C1, 2=C2, 3=

Table View:
RowMap () returns a Map <R, Map <C, V> View. RowKeySet () returns a Set <R>.
Row (r) returns a non-null Map <C, V>. Modifying this view Map will also cause modifications to the original table.
The column-related methods include columnMap (), columnKeySet (), and column (c ). (Column-based operations are less efficient than Row-based operations)
The returned result of cellSet () is a Set with Table. Cell <R, C, V> as the element. The Cell here is similar to Map. Entry, but it is distinguished by rows and columns.

  Table has the following implementation:
HashBasedTable: implemented based on HashMap <R, HashMap <C, V>.
TreeBasedTable: implemented based on TreeMap <R, TreeMap <C, V>.
ImmutableTable: Based on ImmutableMap <R, ImmutableMap <C, V>. (Note that ImmutableTable has been optimized for sparse and dense sets)
ArrayTable: ArrayTable is a table that needs to be set at the time of building. This type of table is implemented by a two-dimensional array, which can improve the efficiency of time and space in the case of intensive data tables.

  ClassToInstanceMap

Sometimes, the key of your map is not a type, they are many types, You Want To map them to get this type, guava provides ClassToInstanceMap to meet this purpose.
In addition to inheriting from the Map interface, ClassToInstaceMap provides Methods T getInstance (Class <T>) and T putInstance (Class <T>, T), eliminating forced type conversion.
This class has a simple type parameter, usually called B, which represents the upper-layer binding of map control. For example:

ClassToInstanceMap<Number> numberDefaults =, Integer.valueOf(0));

Technically, ClassToInstanceMap <B> implements Map <Class <? Extends B>, B>, or, This Is A ing from the subclass of B to the object B, which may make the generic type of ClassToInstanceMap slightly messy, but remember that B is always the upper-layer binding type of Map. Generally, B is just an object.
Guava provides useful implementations, MutableClassToInstanceMap and ImmutableClassToInstanceMap.
Key points: for other Map <Class, Object> and ClassToInstanceMap native type projects, a native type and its corresponding packaging Class can be mapped to different values;

   <String> classToInstanceMapString =<Person> classToInstanceMap ==  Person("peida",20"person name :"+person.name+" age:"+, "peida""string:"+classToInstanceMapString.getInstance(String.=classToInstanceMap.getInstance(Person."person1 name :"+person1.name+" age:"+ .name =.age = 

  RangeSet

RangeSet is used to process a series of non-consecutive, non-empty range values. After a range is added to a RangeSet, any continuous range is automatically merged, and the empty range is automatically removed. For example:

 <Integer> rangeSet =1, 10"rangeSet:"+11, 15"rangeSet:"+15, 20"rangeSet:"+0, 0"rangeSet:"+5, 10"rangeSet:"+
1‥101‥10][11‥151‥10][11‥15)(15‥201‥10][11‥15)(15‥201‥5][10‥10][11‥15)(15‥20)}

Note, such as merging Range. closed (1, 10) and Range. in closedOpen (11, 15), we must first call Range. canonical (DiscreteDomain) is passed into DiscreteDomain. integers.

  RangeSet View
RangeSet supports a wide range of views, including:
Complement (): It is an auxiliary RangeSet, which is itself a RangeSet, because it contains non-continuous, non-empty range.
SubRangeSet (Range <C>): returns an intersection view.
AsRanges (): returns the Set <Range <C> View that can be iterated.
AsSet (DiscreteDomain <C>) (ImmutableRangeSet only): returns an ImmutableSortedSet <C> type view with elements in the range instead of the range itself. (This operation is not supported if the upper or lower limits of DiscreteDomain and RangeSet are infinite)

  Queries
In addition to various views, RangeSet also supports various direct query operations, the most important of which is:
Contains (C): This is the most basic operation of RangeSet. It can query whether a given element is in RangeSet.
RangeContaining (C): returns the Range containing the given element. If it does not exist, null is returned.
Encloses (Range <C>): used to determine whether a given Range is included in RangeSet.
Span (): returns the union of all ranges contained in this RangeSet.

  RangeMap
RangeMap represents the set of non-consecutive non-null range. Unlike RangeSet, RangeMap does not merge adjacent mappings, and even adjacent ranges correspond to the same values. For example:

 <Integer, String> rangeMap =1, 10), "foo""rangeMap:"+3, 6), "bar""rangeMap:"+10, 20), "foo""rangeMap:"+5, 11"rangeMap:"+1‥10]=1‥3]=foo, (3‥6)=bar, [6‥10]=1‥3]=foo, (3‥6)=bar, [6‥10]=foo, (10‥20)=1‥3]=foo, (3‥5)=bar, (11‥20)=foo]

  RangeMap View
RangeMap provides two views:
AsMapOfRanges (): returns the Map <Range <K>, V> type view. This operation can be used as an iterative operation.
SubRangeMap (Range <K>) provides the intersection of a given Range. This operation can be promoted to traditional headMap, subMap, and tailMap.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.