google的guava工具包的确很多好东西,包括之前的字符串处理工具类的,还有大量的collection相关的,项目地址在:http://code.google.com/p/guava-libraries/
留意到其中的collection相关类中的map,简单介绍如下,更多的请大家补充挖掘或者
看原来的文档:
guava提供的是多值map!,就是说,一个key,可以对应多个value了,比如一个人会有多个联系号码等,可以表达为:
multimap<String,String> phonebook=ArrayListMultmap.create();
phonebook.put("a","43434");
phonebook.put("b","3434434");
system.out.println(phonebook,get("a"));
还有map的查询:
- Map<String, Integer> user = new HashMap<String, Integer>();
- user.put("张三", 20);
- user.put("李四", 22);
- user.put("王五", 25);
-
- Map<String, Integer> filtedMap = Maps.filterValues(user,
- new Predicate<Integer>() {
- public boolean apply(Integer value) {
- return value > 20;
- }
- });
- System.out.println(filtedMap);
Map<String, Integer> user = new HashMap<String, Integer>();
user.put("张三", 20);
user.put("李四", 22);
user.put("王五", 25);
// 所有年龄大于20岁的人员
Map<String, Integer> filtedMap = Maps.filterValues(user,
new Predicate<Integer>() {
public boolean apply(Integer value) {
return value > 20;
}
});
System.out.println(filtedMap);
再来点例子,加深了解:
-
- public class MutliMapTest {
- public static void main(String... args) {
- Multimap<String, String> myMultimap = ArrayListMultimap.create();
-
-
- myMultimap.put('Fruits', 'Bannana');
- myMultimap.put('Fruits', 'Apple');
- myMultimap.put('Fruits', 'Pear');
- myMultimap.put('Vegetables', 'Carrot');
-
-
- int size = myMultimap.size();
- System.out.println(size);
-
-
- Collection<string> fruits = myMultimap.get('Fruits');
- System.out.println(fruits);
-
- Collection<string> vegetables = myMultimap.get('Vegetables');
- System.out.println(vegetables);
-
-
- for(String value : myMultimap.values()) {
- System.out.println(value);
- }
-
-
- myMultimap.remove('Fruits','Pear');
- System.out.println(myMultimap.get('Fruits'));
-
-
- myMultimap.removeAll('Fruits');
- System.out.println(myMultimap.get('Fruits'));
- }
- }
public class MutliMapTest {
public static void main(String... args) {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put('Fruits', 'Bannana');
myMultimap.put('Fruits', 'Apple');
myMultimap.put('Fruits', 'Pear');
myMultimap.put('Vegetables', 'Carrot');
// Getting the size
int size = myMultimap.size();
System.out.println(size); // 4
Collection<string> fruits = myMultimap.get('Fruits');
System.out.println(fruits); // [Bannana, Apple, Pear]
Collection<string> vegetables = myMultimap.get('Vegetables');
System.out.println(vegetables); // [Carrot]
// 循环输出
for(String value : myMultimap.values()) {
System.out.println(value);
}
// 移走某个值
myMultimap.remove('Fruits','Pear');
System.out.println(myMultimap.get('Fruits')); // [Bannana, Pear]
//移走某个KEY的所有对应value
myMultimap.removeAll('Fruits');
System.out.println(myMultimap.get('Fruits')); // [] (Empty Collection!)
}
}
更详细的看:
http://docs.guava-libraries.googlecode.com/git-history/release09/javadoc/com/google/common/collect/Multimap.html