136-137_容器_guava之_实用功能_Multiset_Multimap_BiMap_Table

时间:2022-03-14 20:43:32

136-137_容器_guava之_实用功能_Multiset_Multimap_BiMap_Table

Multiset

  • Test05_MultiSet.java
package guava.collection;

import java.util.Set;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
/**
* 统计单词出现的次数
* 1.HashMap 分拣存储+面向对象思维 -->判断
* 2.Multiset: 无序+可重复/增强了可读性 +操作简单
* 创建:Multiset HashMultiset.create();
* 获取:Set Multiset.elementSet();
* 计数: temp+"-->"+Multiset.count(temp)
*/

public class Test05_MultiSet {
public static void main(String[] args) {
String str ="this is a cat and that is a mice where is the food";
//分割字符串
String[] strArray =str.split(" ");
//存储到Multiset中
Multiset<String> set =HashMultiset.create();
for(String strTemp:strArray){
set.add(strTemp);
}

//获取所有的单词 Set
Set<String> letters =set.elementSet();
for(String temp:letters){
System.out.println(temp+"-->"+set.count(temp));
}
}
}

Multimap

  • Test06_MultiMap.java
package guava.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
/**
* 分析查看 教师 教授的每门课程
* Multimap :key-value key可以重复
*/

public class Test06_MultiMap {
public static void main(String[] args) {
test();
// test2();
}

public static void test(){
Multimap<String,String> teachers =ArrayListMultimap.create();
teachers.put("邓爷爷", "改革开放");
teachers.put("*", "三个代表");
teachers.put("*", "科学发展观");
teachers.put("*", "八荣八耻");
teachers.put("*", "和谐社会");
teachers.put("习主席", "....");
Set<String> keyset=teachers.keySet();
for(String key:keyset){
List<String> list=(List<String>) teachers.get(key);
System.out.println(key+"-->"+list);
}
}
public static void test2(){
Map<String,String> cours =new HashMap<String,String>();
//加入测试数据
cours.put("改革开放", "邓爷爷");
cours.put("三个代表", "*");
cours.put("科学发展观", "*");
cours.put("和谐社会", "*");
cours.put("八荣八耻", "*");
cours.put("....", "习主席");

//Multimap
Multimap<String,String> teachers =ArrayListMultimap.create();

//迭代器
Iterator<Map.Entry<String,String>> it =cours.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String,String> entry =it.next();
String key =entry.getKey(); //课程
String value =entry.getValue(); //教师

//教师 -->课程
teachers.put(value, key);
}

//查看Multimap
Set<String> keyset =teachers.keySet();
for(String key:keyset){
Collection<String> col =teachers.get(key);
System.out.println(key+"-->"+col);
}
}
}

BiMap

  • Test07_BiMap.java
package guava.collection;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
/**
* HashMap 键唯一,值可以重复
* BiMap:双向Map(Bidirectional Map) 键与值都不能重复(unique -valued map)
*/

public class Test07_BiMap {
public static void main(String[] args) {
BiMap<String,String> bimap=HashBiMap.create();
bimap.put("bjsxt", "bjsxt@sina.com");
bimap.put("good","good@qq.com");
//通过邮箱找用户
String user =bimap.inverse().get("good@qq.com");
System.out.println(user);
System.out.println(bimap.inverse().inverse()==bimap);
}
}

Table

  • Test08_Table.java
package guava.collection;

import java.util.Map;
import java.util.Set;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.Tables;
/**
* 双键的Map -->Table -->rowKey+columnKey+value
* Table<R, C, V> ->HashBasedTable<R, C, V>.create()
* 方法
* 1.所有的行数据:.cellSet()->Set<Cell<R, C, V>>
* 单行:<Cell<R, C, V>> 获取.getRowKey() .getColumnKey() .getValue()
* 2.获取rowkey列:.rowKeySet()
* 3.获取columnkey列:.columnKeySet()
* row(rowKey)->Map<rowkey,value>
* column(columnKey)->Map<columnkey,value>
* 4.获取value列: values()
* 5.转换(将rowKey和columnKey对调位置)
* Tables.transpose(tables);
*/

public class Test08_Table {
public static void main(String[] args) {
Table<String,String,Integer> tables=HashBasedTable.create();
//测试数据
tables.put("a", "javase", 80);
tables.put("b", "javase", 90);
tables.put("a", "oracle", 100);
tables.put("c", "oracle", 95);

//所有的行数据
Set<Cell<String,String,Integer>> cells =tables.cellSet();
for(Cell<String,String,Integer> temp:cells){
System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
}

System.out.println("==========学生查看成绩==============");
System.out.print("学生\t");
//所有的课程
Set<String> cours =tables.columnKeySet();
for(String t:cours){
System.out.print(t+"\t");
}
System.out.println();
//所有的学生
Set<String> students =tables.rowKeySet();
for(String stu:students){
System.out.print(stu+"\t");
Map<String,Integer> scores =tables.row(stu);
for(String c:cours){
System.out.print(scores.get(c)+"\t");
}
System.out.println();
}

System.out.println("==========课程查看成绩==============");
System.out.print("课程\t");
//所有的学生
Set<String> stuSet =tables.rowKeySet();
for(String stu:stuSet){
System.out.print(stu+"\t");
}
System.out.println();
//所有的课程
Set<String> courSet =tables.columnKeySet();
for(String c:courSet){
System.out.print(c+"\t");
Map<String,Integer> scores =tables.column(c);
for(String s:stuSet){
System.out.print(scores.get(s)+"\t");
}
System.out.println();
}

System.out.println("===========转换===========");
Table<String,String,Integer> tables2 =Tables.transpose(tables);
//所有的行数据
Set<Cell<String,String,Integer>> cells2 =tables2.cellSet();
for(Cell<String,String,Integer> temp:cells2){
System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
}
}
}