所需要的包:
com.google.common google-collect 1.0-rc1下载不了在这里下,
http://dist.wso2.org/maven2/com/google/common/google-collect/1.0-rc1/
导进去
实体:
public class Fruit {
private String name;
private Double price;
public Fruit(String name, Double price) {
this.name = name;
this.price = price;
}
/*
* @Override public boolean equals(Object o) { if (this == o) return true; if (o
* == null || getClass() != o.getClass()) return false; Fruit fruit = (Fruit) o;
* return java.util.Objects.equals(name, fruit.name) &&
* java.util.Objects.equals(price, fruit.price); }
*
* @Override public int hashCode() { return java.util.Objects.hash(name, price);
* }
*/
// 注意equals和hashCode必须成对出现
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Fruit [name=" + name + ", price=" + price + "]";
}
}
测试:
1.分组统计
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
public class FruitDemo {
public static void main(String[] args) {
List<Fruit> fruitList = new ArrayList<Fruit>();
fruitList.add(new Fruit("apple", 6.0));
fruitList.add(new Fruit("apple", 6.0));
fruitList.add(new Fruit("banana", 7.0));
fruitList.add(new Fruit("banana", 7.0));
fruitList.add(new Fruit("banana", 7.0));
fruitList.add(new Fruit("grape", 8.0));
Map<String, Long> map = fruitList.stream()
.collect(Collectors.groupingBy(Fruit::getName, Collectors.counting()));
System.out.println(map);
}
}
{banana=3, apple=2, grape=1}
换种写法:
Map<String, Long> map1 = fruitList.stream().map(Fruit::getName).
collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(map1);
2.排序
现在要按照水果map中value的数量逆序打印每个entry
Map<String, Long> map = fruitList.stream()
.collect(Collectors.groupingBy(Fruit::getName, Collectors.counting()));
System.out.println(map);
map.entrySet().stream().sorted(Map.Entry.<String, Long>comparingByValue().reversed()).forEachOrdered(System.out::println);
结果:
banana=3
apple=2
grape=1
3.累加求和
Map<String, Double> sumMap = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName, Collectors.summingDouble(Fruit::getPrice)));
System.out.println(sumMap);
结果:
{banana=21.0, apple=12.0, grape=8.0}
4.分组
Map<String, List<Fruit>> groupMap =
fruitList.stream().collect(Collectors.groupingBy(Fruit::getName));
System.out.println(groupMap);
结果:
{banana=[Fruit [name=banana, price=7.0], Fruit [name=banana, price=7.0], Fruit [name=banana, price=7.0]], apple=[Fruit [name=apple, price=6.0], Fruit [name=apple, price=6.0]], grape=[Fruit [name=grape, price=8.0]]}
上述代码根据name将list分组,如果name是唯一的,那么上述代码就会显得啰嗦。我们需要知道,Guava补JDK之不足,现在改Guava一显身手了。
List<Fruit> fruitList = new ArrayList<Fruit>();
fruitList.add(new Fruit("apple", 6.0));
fruitList.add(new Fruit("apple1", 6.0));
fruitList.add(new Fruit("banana", 7.0));
fruitList.add(new Fruit("banana2", 7.0));
fruitList.add(new Fruit("banana3", 7.0));
fruitList.add(new Fruit("grape", 8.0));
Map<String, List<Fruit>> groupMap =
fruitList.stream().collect(Collectors.groupingBy(Fruit::getName));
System.out.println(groupMap);
// 上述代码根据name将list分组,如果name是唯一的,那么上述代码就会显得啰嗦。我们需要知道,Guava补JDK之不足,现在改Guava一显身手了。
Map<String, Fruit> map = Maps.uniqueIndex(fruitList, Fruit::getName);
System.out.println(map);
结果:
{banana=[Fruit [name=banana, price=7.0]], apple=[Fruit [name=apple, price=6.0]], apple1=[Fruit [name=apple1, price=6.0]], banana2=[Fruit [name=banana2, price=7.0]], banana3=[Fruit [name=banana3, price=7.0]], grape=[Fruit [name=grape, price=8.0]]}
{apple=Fruit [name=apple, price=6.0], apple1=Fruit [name=apple1, price=6.0], banana=Fruit [name=banana, price=7.0], banana2=Fruit [name=banana2, price=7.0], banana3=Fruit [name=banana3, price=7.0], grape=Fruit [name=grape, price=8.0]}
生成的Map是ImmutableMap,不可更改里面的值。比如map.remove(“apple”)会抛出异常
Map<String, List<Fruit>> groupMap =
fruitList.stream().collect(Collectors.groupingBy(Fruit::getName));
System.out.println(groupMap);
// 上述代码根据name将list分组,如果name是唯一的,那么上述代码就会显得啰嗦。我们需要知道,Guava补JDK之不足,现在改Guava一显身手了。
Map<String, Fruit> map = Maps.uniqueIndex(fruitList, Fruit::getName);
System.out.println(map);
// 生成的Map是ImmutableMap,不可更改里面的值。比如map.remove("apple")会抛出异常:java.lang.UnsupportedOperationException
map.remove("apple");
根据不同的名字分为若干组
Map<String, List<Double>> groupMap = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName,Collectors.mapping(Fruit::getPrice, Collectors.toList())));
System.out.println(groupMap);
结果:
{banana=[7.0], apple=[6.0], apple1=[6.0], banana2=[7.0], banana3=[7.0], grape=[8.0]}
上面一段代码可以用Guava代替
Map<String, List<Double>> groupMap = fruitList.stream().collect(Collectors.groupingBy(Fruit::getName,Collectors.mapping(Fruit::getPrice, Collectors.toList())));
System.out.println(groupMap);
// //上面一段代码可以用Guava代替
Multimap<String, Integer> multiMap = ArrayListMultimap.create();
fruitList.forEach(fruit -> multiMap.put(fruit.getName(), fruit.getPrice().intValue()));
System.out.println(multiMap);
结果:
{banana=[7.0], apple=[6.0], apple1=[6.0], banana2=[7.0], banana3=[7.0], grape=[8.0]}
{banana=[7], apple=[6], apple1=[6], banana2=[7], banana3=[7], grape=[8]}
相关的讲解:
https://my.oschina.net/LiuLangEr/blog/1632378