[Guava Study Notes] Collections: unchangeable set, new set type, guavacollections

Source: Internet
Author: User

[Guava Study Notes] Collections: unchangeable set, new set type, guavacollections
Immutable set

Null values are not accepted.

Create: ImmutableSet. copyOf (set); ImmutableMap. of ("a", 1, "B", 2 );

public static final ImmutableSet<Color> GOOGLE_COLORS =        ImmutableSet.<Color>builder()            .addAll(WEBSAFE_COLORS)            .add(new Color(0, 191, 255))            .build();

It can be ordered (such as ImmutableSortedSet) and sorted during construction.

All ImmutableList sets have an asList () method, which is converted to ImmutableList.

New Set type MultiSet: Set that accepts duplicate values

Get: count (E) returns 0 for no element

Set: add (E, int) remove (E, int) setCount (E, int)

View:

-Repetition: iterator () iterates the repeated element size () and returns the total number of all elements.

-No duplicates: The set entrySet () of elementSet () that does not repeat elements contains the Entry that supports the getElement () and getCount () methods.

Implementation: HashMultiset TreeMultiset can be obtained using the. subMultiset method to obtain the specified range subset LinkedHashMultiset Save the insertion sequence ConcurrentHashMultiset ImmutableMultiset

MultiMap: one-click multi-value

Get: get (key) returns List or Set (Shortest copy) containsKey (key) values. If all values are removed, false is returned.

Set: put (K, V) putAll (K, Iterable <V>) remove (K, V) removeAll (K) replaceValues (K, Iterable <V>)

View:

-Repeat:Entries returns Collection <Map. Entry <K, V>, including duplicate keys.All keys of keys, number of keys = number of duplicatesValues () All values size () All "key-single value" count

-No duplicates: asMap returnsMap <K, Collection <V>KeySet does not repeat the key

Implementation: ArrayListMultimap (value: ArrayList) HashMultimap (value: HashSet) implements listmultimap implements HashMultimap to save the insertion sequence TreeMultimapImmutableListMultimap ImmutableSetMultimap

BiMap: bidirectional key-value ing

Get: inverse () inversion, which can be returned by values ()

Set: If the value is repeated, IllegalArgumentException forcePut (key, value) is forcibly replaced.

Implementation: HashBiMap ImmutableBiMap EnumBiMap EnumHashBiMap

Table: two keys and one value

Set: put (row, column, cell)

View:

-Row: rowMap () uses row as the key rowKeySet () row (r)

-Column: The columnMap () columnKeySet () column (c) column is a little slower than the row.

-Cell: The cellSet () element is Table. Cell <R, C, V>

Implementation: HashBasedTable TreeBasedTable ImmutableTable ArrayTable requires that the number of rows and columns be specified during construction. An essential two-dimensional array

RangeSet: interval set

Adjacent intervals are merged.

Set:add(Range.closedOpen(15, 20))

Get: contains (C) rangeContaining (C) does not return null encloses (Range <C>) span () can contain the minimum Range of all intervals

View: asRanges () returns all elements in the Set <Range <C> asSet (DiscreteDomain <C>) Range.

Complement () completion set subRangeSet (Range <C>) intersection

Implementation: TreeRangeSet

RangeMap: The key is a range.

Split, not merged.

Set:put(Range.closed(1, 10), "foo")

View: asMapOfRanges () returns Map <Range <K>, V> subRangeMap (Range <K>)

Implementation: TreeRangeMap and so on

 


An abstract base class defined in CollectionBase C #. Under the SystemCollections namespace, an abstract base class is provided for a strong collection.

System. Collections. CollectionBase is the base class provided for the specified class set.
This base class is provided to make it easier for implementers to create a strongly-typed custom set, which is also the basis for generics later than 2.0.
Question 1: Can I use a custom set?
Yes, as long as the class you write inherits from him.
Question 2: To put it bluntly, this is a design model. Why do we need to write N more code than less code? Of course, it is better to write less duplicate code and write more implementation logic.


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.