guava之Multiset、Multimap、BiMap与Table_成绩表行转列JAVA136-137
2015-12-06 20:33
435 查看
来源:http://www.bjsxt.com/
1、S02E136_01guava之实用功能_Multiset、Multimap与BiMap
2、S02E137_01guava之Table_成绩表行转列
1、S02E136_01guava之实用功能_Multiset、Multimap与BiMap
package com.test.guava; import java.util.Set; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; /** * 统计单词出现的次数 * 1、HashMap 方法:分拣存储+面向对象思维-->>大量判断 * 对比 * 2、Multiset 特点:无序+可重复 .count()获取次数 增强了可读性+操作简单 */ public class TestGuavaMultiset { 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)); } } } /* 返回: the-->>1 a-->>2 that-->>1 and-->>1 cat-->>1 this-->>1 is-->>3 where-->>1 mice-->>1 food-->>1 */
package com.test.guava; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; 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 TestGuavaMultimap { public static void main(String[] args) { 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); } } } /* 返回: 江主席-->>[三个代表] 习主席-->>[...] 邓小平-->>[改革开放] 胡主席-->>[八荣八耻, 科学发展观, 和谐社会] */
package com.test.guava; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; /** * HashMap 键唯一,值可以重复 * BiMap:双向Map(Bidirectional Map) 键与值都不能重复(unique-valued map) */ public class TestGuavaBiMap { public static void main(String[] args) { BiMap<String, String> bimap = HashBiMap.create(); bimap.put("abc", "abc@qq.com"); bimap.put("good", "good@qq.com"); //通过邮箱找用户 String user = bimap.inverse().get("good@qq.com"); System.out.println(user);//返回good System.out.println(bimap.inverse().inverse() == bimap);//返回true } }
2、S02E137_01guava之Table_成绩表行转列
package com.test.guava; 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 * 方法: * 所有的行数据:cellSet() * 所有的学生:rowKeySet() * 所有的课程:columnKeySet() * 所有的成绩:values() * 学生对应的课程+成绩:rowMap() + get(学生) * row(学生) * 课程对应的学生+成绩:columnMap() + get(课程) * column(课程) */ public class TestGuavaTable { public static void main(String[] args) { Table<String, String, Integer> table = HashBasedTable.create(); //测试数据 table.put("a", "javase", 80); table.put("b", "javase", 90); table.put("a", "oracle", 100); table.put("c", "oracle", 95); //所有的行数据 Set<Cell<String, String, Integer>> cells = table.cellSet(); System.out.println(cells);//[(a,javase)=80, (a,oracle)=100, (b,javase)=90, (c,oracle)=95] for (Cell<String, String, Integer> cell : cells) { System.out.println("学生:" + cell.getRowKey() + "-->>课程:" + cell.getColumnKey() + "-->>成绩:" + cell.getValue()); } System.out.println(table.rowKeySet());//[a, b, c] System.out.println(table.columnKeySet());//[javase, oracle] System.out.println(table.values());//[80, 100, 90, 95] System.out.println(table.rowMap());//{a={javase=80, oracle=100}, b={javase=90}, c={oracle=95}} System.out.println(table.columnMap());//{javase={a=80, b=90}, oracle={a=100, c=95}} System.out.println("++++++++++++按学生查看成绩++++++++++++++++"); System.out.print("学生\t"); //所有课程 Set<String> cours = table.columnKeySet(); for (String cour : cours) { System.out.print(cour + "\t");//课程 } //所有学生 System.out.println(); Set<String> stus = table.rowKeySet(); for (String stu : stus) { System.out.print(stu + "\t");//学生 Map<String, Integer> courSco = table.row(stu);//课程+成绩 for (String cour : cours) { System.out.print(courSco.get(cour) + "\t");//成绩 } System.out.println(); } System.out.println("++++++++++++按课程查看成绩++++++++++++++++"); System.out.print("课程\t"); //所有学生 Set<String> stus2 = table.rowKeySet(); for (String stu : stus2) { System.out.print(stu + "\t");//学生 } //所有课程 System.out.println(); Set<String> cours2 = table.columnKeySet(); for (String cour : cours2) { System.out.print(cour + "\t");//课程 Map<String, Integer> stuSco = table.column(cour);//学生+成绩 for (String stu : stus2) { System.out.print(stuSco.get(stu) + "\t");//成绩 } System.out.println(); } System.out.println("++++++++++++++++键转换+++++++++++++++++"); Table<String, String, Integer> table2 = Tables.transpose(table); Set<Cell<String, String, Integer>> cells2 = table2.cellSet(); for (Cell<String, String, Integer> cell : cells2) { System.out.println("课程:" + cell.getRowKey() + "-->>学生:" + cell.getColumnKey() + "-->>成绩:" + cell.getValue()); } } } /* 返回: [(a,javase)=80, (a,oracle)=100, (b,javase)=90, (c,oracle)=95] 学生:a-->>课程:javase-->>成绩:80 学生:a-->>课程:oracle-->>成绩:100 学生:b-->>课程:javase-->>成绩:90 学生:c-->>课程:oracle-->>成绩:95 [a, b, c] [javase, oracle] [80, 100, 90, 95] {a={javase=80, oracle=100}, b={javase=90}, c={oracle=95}} {javase={a=80, b=90}, oracle={a=100, c=95}} ++++++++++++按学生查看成绩++++++++++++++++ 学生 javase oracle a 80 100 b 90 null c null 95 ++++++++++++按课程查看成绩++++++++++++++++ 课程 a b c javase 80 90 null oracle 100 null 95 ++++++++++++++++键转换+++++++++++++++++ 课程:javase-->>学生:a-->>成绩:80 课程:oracle-->>学生:a-->>成绩:100 课程:javase-->>学生:b-->>成绩:90 课程:oracle-->>学生:c-->>成绩:95 */
相关文章推荐
- Basic--Java基本语法
- SpringMVC中在web.xml中配置DispatcherServlet与中文乱码配置
- Day_6重载
- Day_5面向对象
- Struts2框架学习(三) 数据处理
- Struts2框架学习(二) Action
- day_34Java多线程
- Struts2框架学习(一)
- spring框架学习(六)AOP
- 5、SpringMVC
- 2、spring的对象类型
- java调用jira接口
- 1、Spring的导入
- spring框架学习(五)注解
- 4、Struts2的其他知识
- java反射详解
- 2、Struts中的action
- spring框架学习(四)自动装配
- Java41---42
- spring框架学习(三)junit单元测试