guava - Collection API understanding

1. Simplify work and simplify the creation and initialization of collections

api: http://tool.oschina.net/apidocs/apidoc?api=guava

1) Collection creation:

       j'd'k writing :

Map<String, Map<String, String>> map = new HashMap<String, Map<String,String>>();
List<List<Map<String, String>>> list = new ArrayList<List<Map<String,String>>>();
guava的写法:
Map<String, Map<String, String>> map = Maps.newHashMap();
List<List<Map<String, String>>> list = Lists.newArrayList();
      guava写法:
List<Person> personList= Lists.newLinkedList();
Set<Person> personSet= Sets.newHashSet();
Map<String,Person> personMap= Maps.newHashMap();
Integer[] intArrays= ObjectArrays.newArray(Integer.class,10);

 


2) Set initialization:
     jdk writing:

Set<String> set = new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
guava的写法:
Set<String> set = Sets.newHashSet("one","two","three");
List<String> list = Lists.newArrayList("one","two","three");
Map<String, String> map = ImmutableMap.of("ON","TRUE","OFF","FALSE");
     guava写法:
List<Person> personList2= Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "a", "46546", 1, 20));
Set<Person> personSet2= Sets.newHashSet(new Person(1,1,"a","46546",1,20),
new Person(2,1,"a","46546",1,20));
Map<String,Person> personMap2= ImmutableMap.of("hello",new Person(1,1,"a","46546",1,20),"fuck",new Person(2,1,"a","46546",1,20));

2. Immutability. A large part of it is the immutability provided by the Google collection.
Invariable vs. mutable: data is immutable, thread-safe, does not require synchronization logic, can be shared freely, is easy to design and implement, memory and time efficient.

Unchangeable vs. unmodifiable: Google's unchangeable is guaranteed to be truly immutable, and unmodifiable can actually modify the data.

as follows:

jdk version:

Set<Integer> data = new HashSet<Integer>();
data.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80));
Set<Integer> fixedData = Collections.unmodifiableSet(data); // fixedData - [50, 70, 80, 20, 40, 10, 60, 30]
data.add(90); // fixedData - [50, 70, 80, 20, 40, 10, 90, 60, 30]

Guava version:
How to create an immutable set:
ImmutableSet<Integer> set1 = ImmutableSet.of(10, 20, 30, 40, 50);
// Use the copyOf method
ImmutableSet<Integer> set2 = ImmutableSet.copyOf(set1);
/ / Use the builder method:
ImmutableSet<Integer> set3 = ImmutableSet.<Integer>builder().addAll(set1) .add(60) .add(70).add(80).build();

ImmutableList<Integer> list1 = ImmutableSet.of(10, 20, 30, 40, 50);
// 使用copyOf方法
ImmutableList<Integer> list2 = ImmutableSet.copyOf(list1);
// 使用builder方法:
ImmutableList<Integer> set3 = ImmutableSet.<Integer>builder().addAll(list1) .add(60) .add(70).add(80).build();

ImmutableMap<Integer,Integer> map1 = ImmutableMap.of(1,10,2, 20,3, 30,4, 40,5, 50);
// 使用copyOf方法
ImmutableMap<Integer,Integer> map2 = ImmutableMap.copyOf(map1);
// 使用builder方法:
ImmutableMap<Integer,Integer> map3 = ImmutableMap.<Integer,Integer>builder().putAll(set1) .put(6,60) .put(7,70).put(8,80).build();

3. New collection type
Guava API provides useful new collection types, which work well with existing java collections, namely MultiMap, MultiSet, Table, BiMap, ClassToInstanceMap.

1) MultiMap : A map whose key can be repeated, the subclasses are ListMultimap and SetMultimap, and the list and set are obtained respectively by key.

Multimap<String,Person> customersByType = ArrayListMultimap.create();
customersByType.put("abc", new Person("abc",0));
customersByType.put("abc", new Person("abc",1) );
customersByType.put("abc", new Person("abc",0));
customersByType.put("abcd", new Person("abcd",4));
for(Person person:customersByType.get(" abc")){     System.out.println(person.getAge()); } The result obtained will be 0 1 0 2) MultiSet: You can add repeated elements, and you can count the number of repeated elements



Multiset<Integer> multiSet = HashMultiset.create();
multiSet.add(10);
multiSet.add(30);
multiSet.add(30);
multiSet.add(40);
System.out.println( multiSet.count(30) ); // 2
System.out.println( multiSet.size() ); // 4
System.out.println( multiSet ); // [40, 10, 30 x 2]

3) Table: Equivalent to a map
Table with two keys <Integer,Integer,Person> PersonTable = HashBasedTable.create();
PersonTable.put(0, 0, new Person("a00",1));
PersonTable.put (0, 1, new Person("a01",12));
PersonTable.put(0, 2, new Person("a02",2));
PersonTable.put(1, 0, new Person("a10", 10));
Map<Integer,Person> rowMap = PersonTable.row(1); // Get the row set
Map<Integer,Person> colMap = PersonTable.column(0); // Get the column set

4) BiMap: It is a one-to-one mapping. You can get value by key or key by value
BiMap<Integer,String> biMap = HashBiMap.create();
biMap.put(1, "hello");
biMap.put (2, "world");
biMap.put(3, "!!");
System.out.println(biMap);
System.out.println( biMap.inverse().get("!!") ); // 3

5) ClassToInstanceMap
ClassToInstanceMap provides a way to use class as key and corresponding instance as value. It defines two methods, T getInstance (Class<T>) and T putInstance (Class<T> T). These two methods eliminate the process of element type conversion and ensure that the elements are type-safe in the Map.

ClassToInstanceMap<Person> classToInstanceMap = MutableClassToInstanceMap.create();
classToInstanceMap.putInstance(Person.class, new Person("a1",1));
Person person1 = classToInstanceMap.getInstance(Person.class);

 

Transfer from: https://blog.csdn.net/u010832551/article/details/74668676

Guess you like

Origin blog.csdn.net/xiaodujava/article/details/89181489