1. 程式人生 > >136-137_容器_guava之_實用功能_Multiset_Multimap_BiMap_Table

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

1

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", "[email protected]");
        bimap.put("good","[email protected]");
        //通過郵箱找使用者
        String user =bimap.inverse().get("[email protected]");
        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());
        }
    }
}