Guava essays

Kenneth Brian Edmund2022-05-14 19:25:55

0. Sick need rest at the same time , Uncomfortable feelings also affect people's rest

guava A Chinese document of

1. collect

Guava Collections Practical use
gitee

import com.google.common.collect.*;
import java.util.*;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
/** * @Author: weng * @Date: 2022/5/10 * @Description: com.weng.cloud.service8881.guava * * @see <a href="https://www.jianshu.com/p/a69d72266c27">Guava Collections Practical use </a> */
public class TestCollections {

static Set<String> set = Sets.newHashSet("weng","chong","yu");
public static void main(String[] args) {

//testImmutable();
//testMulti();
//testBiMap();
testTable();
}
private static void newCollection(){

List list = Lists.newArrayList();
ConcurrentMap<Object, Object> cMap = Maps.newConcurrentMap();
CopyOnWriteArraySet<Object> set = Sets.newCopyOnWriteArraySet();
}
private static void testImmutable(){

//jdk collections -> set.add/remove -> unmodifiableList Also affected 
// Collection<Object> unmodifiableSet = Collections.unmodifiableCollection(set);
// set.add("wcy");
// System.out.println(unmodifiableSet);
// Not really immutable sets 
ImmutableSet<String> immutableSet = ImmutableSet.copyOf(set);
set.add("wcy");
System.err.println(immutableSet);
}
private static void testMulti(){

// Map<ele,times>
HashMultiset<String> multiset = HashMultiset.create(set);
multiset.add("weng");
// toString => [weng x 2, chong, yu]
System.err.println(multiset);
// Map<key,List<value>>
HashMultimap<Integer, String> multiMap = HashMultimap.create();
// ImmutableMultimap
multiMap.put(8,"kobe");
multiMap.put(6,"james");
multiMap.put(8,"d-will");
// toString => {6=[james], 8=[kobe, d-will]}
System.out.println(multiMap);
}
private static void testBiMap(){

//Map<key,Map<value,???>>
HashBiMap<Integer, String> biMap = HashBiMap.create();
biMap.put(8,"kobe");
biMap.put(13,"booker");
biMap.put(24,"kobe");
//Exception in thread "main" java.lang.IllegalArgumentException: value already present: kobe
}
// following api I little interesting , But the application scenario is relatively limited 
static void testElse(){

// Build memory based systems 、 Expiration policy concurrentHashMap, See brief book for details 
// guava.Collections As the main stream filter、map
// guava.Collections The specified sort as the main tone ( collocation guava.ordering)
}
static void testTable(){

//row、column、value It's equivalent to a two-dimensional set 
HashBasedTable<String, Integer, String> table = HashBasedTable.create();
table.put(" The warriors ",11," Sakyamuni ");
table.put(" The nets ",11," Owen ");
table.put(" The warriors ",0," Little Peyton ");
table.put(" Lakers ",0," Weishao ");
//{ The warriors ={11= Sakyamuni , 0= Little Peyton }, The nets ={11= Owen }, Lakers ={0= Weishao }}
//System.out.println(table);
//{11= Sakyamuni , 0= Little Peyton }
//System.err.println(table.row(" The warriors "));
//{ The warriors = Little Peyton , Lakers = Weishao }
//System.err.println(table.column(0));
// ( The warriors ,11)= Sakyamuni 
// ( The warriors ,0)= Little Peyton 
// ( The nets ,11)= Owen 
// ( Lakers ,0)= Weishao 
//table.cellSet().stream().forEach(cell -> System.out.println(cell));
//[11, 0]
//System.out.println(table.columnKeySet());
//{ The warriors ={11= Sakyamuni , 0= Little Peyton }, The nets ={11= Owen }, Lakers ={0= Weishao }}
//System.err.println(table.rowMap());
//{11={ The warriors = Sakyamuni , The nets = Owen }, 0={ The warriors = Little Peyton , Lakers = Weishao }}
System.out.println(table.columnMap());
}
}

thank
Similar articles

2022-05-14