0. 生病需要休息的同时,不舒服的感觉也让人休息受到影响
1. collect
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实战使用</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也受到影响
// Collection<Object> unmodifiableSet = Collections.unmodifiableCollection(set);
// set.add("wcy");
// System.out.println(unmodifiableSet);
// 不是真正的不可变集合
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
}
// 以下api有点意思,但是应用场景相对有限
static void testElse(){
// 构建基于内存的、过期策略的 concurrentHashMap,详见简书
// guava.Collections作为主调的流式filter、map
// guava.Collections作为主调的指定排序(搭配guava.ordering)
}
static void testTable(){
//row、column、value 相当于二维集合了
HashBasedTable<String, Integer, String> table = HashBasedTable.create();
table.put("勇士",11,"佛祖");
table.put("篮网",11,"欧文");
table.put("勇士",0,"小佩顿");
table.put("湖人",0,"威少");
//{勇士={11=佛祖, 0=小佩顿}, 篮网={11=欧文}, 湖人={0=威少}}
//System.out.println(table);
//{11=佛祖, 0=小佩顿}
//System.err.println(table.row("勇士"));
//{勇士=小佩顿, 湖人=威少}
//System.err.println(table.column(0));
// (勇士,11)=佛祖
// (勇士,0)=小佩顿
// (篮网,11)=欧文
// (湖人,0)=威少
//table.cellSet().stream().forEach(cell -> System.out.println(cell));
//[11, 0]
//System.out.println(table.columnKeySet());
//{勇士={11=佛祖, 0=小佩顿}, 篮网={11=欧文}, 湖人={0=威少}}
//System.err.println(table.rowMap());
//{11={勇士=佛祖, 篮网=欧文}, 0={勇士=小佩顿, 湖人=威少}}
System.out.println(table.columnMap());
}
}