google的guava工具包的确很多好东西,包括之前的字符串处理工具类的,还有大量的collection相关的,项目地址在:http://code.google.com/p/guava-libraries/
留意到其中的collection相关类中的map,简单介绍如下,更多的请大家补充挖掘或者
看原来的文档:
guava提供的是多值map!,就是说,一个key,可以对应多个value了,比如一个人会有多个联系号码等,可以表达为:
multimap phonebook=ArrayListMultmap.create();
phonebook.put("a","43434");
phonebook.put("b","3434434");
system.out.println(phonebook,get("a"));
还有map的查询:
Java代码
Map user =newHashMap();
user.put("张三",20);
user.put("李四",22);
user.put("王五",25);
// 所有年龄大于20岁的人员
Map filtedMap = Maps.filterValues(user,
newPredicate() {
publicbooleanapply(Integer value) {
returnvalue >20;
}
});
System.out.println(filtedMap);Map user = new HashMap();
user.put("张三", 20);
user.put("李四", 22);
user.put("王五", 25);
// 所有年龄大于20岁的人员
Map filtedMap = Maps.filterValues(user,
new Predicate() {
public boolean apply(Integer value) {
return value > 20;
}
});
System.out.println(filtedMap);
再来点例子,加深了解:
Java代码
publicclassMutliMapTest {
publicstaticvoidmain(String... args) {
Multimap 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
intsize = myMultimap.size();
System.out.println(size);// 4
Collection fruits = myMultimap.get('Fruits');
System.out.println(fruits);// [Bannana, Apple, Pear]
Collection 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!)
}
}public class MutliMapTest {
public static void main(String... args) {
Multimap 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 fruits = myMultimap.get('Fruits');
System.out.println(fruits); // [Bannana, Apple, Pear]
Collection 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