import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import com.google.common.base.Function; import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import lombok.Getter; import lombok.Setter; @Setter @Getter class Goods { private Integer id; private Integer groupId; private String name; public String toString() { return "{id="+id + ", groupId=" + groupId + ", name=" + name + "}"; } } public class TxAspect { public static void main(String[] args) { List<Goods> list = new ArrayList<>(); { Goods e = new Goods(); e.setId(1); e.setGroupId(1); e.setName("商品1"); list.add(e); } { Goods e = new Goods(); e.setId(3); e.setGroupId(3); e.setName("商品3"); list.add(e); } { Goods e = new Goods(); e.setId(4); e.setGroupId(2); e.setName("商品4"); list.add(e); } { Goods e = new Goods(); e.setId(5); e.setName("商品5"); list.add(e); } { Goods e = new Goods(); e.setId(2); e.setGroupId(2); e.setName("商品2"); list.add(e); } //根据groupId分组, 如果groupId为null, 则放到默认为0的group下 Function<Goods, Integer> fun = new Function<Goods, Integer>() { @Override public Integer apply(Goods input) { if (input.getGroupId() == null) { return 0; } return input.getGroupId(); } }; Multimap<Integer, Goods> index = Multimaps.index(list, fun); Map<Integer, Collection<Goods>> map = index.asMap(); for (Map.Entry<Integer, Collection<Goods>> entry : map.entrySet()) { System.out.println(entry.getKey() + " <---> " + entry.getValue()); } } }
guava集合分组
猜你喜欢
转载自sd-zyl.iteye.com/blog/2278749
今日推荐
周排行