当前位置:网站首页>Googgle guava ImmutableCollections

Googgle guava ImmutableCollections

2022-07-04 11:44:00 Brother Wang_


Googgle guava ImmutableCollections

Reference documents


 ​Google Guava ImmutableCollections Official documents ​


Immutable objects have many advantages( Immutable classes have many benefits )


  1. Safe for use by untrusted libraries( Use untrusted libraries safely ), I remember Effective Java I saw , Because what is passed is a set or object , This will lead to many situations , The external may change the incoming object , Or the collection or object returned by the current class may also destroy the currently returned reference information , So protect , What you get and what you go out will not be destroyed by outsiders .
  2. Thread-safe: can be used by many threads with no risk of race conditions.( Thread safety : It can be used by many threads , There is no risk of competitive conditions ) Each thread gets a copy of an immutable class .
  3. Can be used as a constant , Expect it to remain fixed .
  4. Making immutable copies of objects is a good defense programming technique a good defensive programming technique
  5. When you don't want to modify the set , Or expect the set to remain unchanged , It is a good practice to copy it defensively into an immutable set .

How to create immutable classes

  • copyOf()
       
public static void usingCopyOf(){
Set<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(2);
ImmutableSet<Integer> immutableSet = ImmutableSet.copyOf(set);
// Adding elements is not supported here
/*immutableSet.add(4);
Set<Integer> addOne = new HashSet<>();
addOne.add(5);
immutableSet.addAll(addOne);*/
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • of()
    of Methods are overloaded with many numbers , The last one is the optional extension parameter .
       
public static void usingOf(){
ImmutableSet<Integer> ofSet = ImmutableSet.of(1,2,3,4,5,6,7);
System.out.println(ofSet);
}
  • 1.
  • 2.
  • 3.
  • 4.
  • Use builder mode to create
       
public static void usingBuilder(){
ImmutableSet<Integer> builderSet = ImmutableSet.<Integer>builder()
.add(1)
.add(2)
.build();
System.out.println(builderSet);
}

/**
* Returns a new builder. The generated builder is equivalent to the builder
* created by the {@link Builder} constructor.
*/
public static <E> Builder<E> builder() {
return new Builder<E>();
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • asList()
    All immutable sets provide a ImmutableList View asList() Even if you have data stored as one ImmutableSortedSet, You can get k The smallest element sortedSet.asList().get(k).

Main set

Variable set type

Variable collection sources :JDK or Guava

Guava Immutable set

Collection

JDK

ImmutableCollection

List

JDK

ImmutableList

Set

JDK

ImmutableSet

SortedSet/NavigableSet

JDK

ImmutableSortedSet

Map

JDK

ImmutableMap

SortedMap

JDK

ImmutableSortedMap

Multiset

Guava

ImmutableMultiset

SortedMultiset

Guava

ImmutableSortedMultiset

Multimap

Guava

ImmutableMultimap

ListMultimap

Guava

ImmutableListMultimap

SetMultimap

Guava

ImmutableSetMultimap

BiMap

Guava

ImmutableBiMap

ClassToInstanceMap

Guava

ImmutableClassToInstanceMap

Table

Guava

ImmutableTable

The next step is to understand these collections



原网站

版权声明
本文为[Brother Wang_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141347503950.html