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());
}
}
}
分享到:
相关推荐
因工作的需要,利用Guave对集合进行分组。这是个最简单的例子,希望对大家有用。
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html http://macrochen.iteye.com/blog/737058 http://code.google.com/p/guava-libraries/
Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法...
guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档guava源文档...
赠送jar包:guava-17.0.jar; 赠送原API文档:guava-17.0-javadoc.jar; 赠送源代码:guava-17.0-sources.jar; 赠送Maven依赖信息文件:guava-17.0.pom; 包含翻译后的API文档:guava-17.0-javadoc-API文档-中文...
赠送jar包:guava-23.0.jar; 赠送原API文档:guava-23.0-javadoc.jar; 赠送源代码:guava-23.0-sources.jar; 赠送Maven依赖信息文件:guava-23.0.pom; 包含翻译后的API文档:guava-23.0-javadoc-API文档-中文...
guava
guava-23.0.zip guava.jar guava
赠送jar包:guava-18.0.jar; 赠送原API文档:guava-18.0-javadoc.jar; 赠送源代码:guava-18.0-sources.jar; 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...
赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...
├─Google Guava 第01讲-Joiner详细介绍以及和Java8Collector对比.wmv ├─Google Guava 第02讲-Guava Splitter详细讲解以及实战练习.wmv ├─Google Guava 第03讲-Preconditions&Objects;&assert;讲解.wmv ├─...
集合(collections) 缓存(caching) 原生的类型支持(primitives support) 并发类库(concurrency libraries) 通用注解(common annotations) 字符串处理(string processing) 输入/输出(I/O),等等
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
Guava是Google公司开发的一款适用于Java的工具类集合。
赠送jar包:guava-30.0-jre.jar; 赠送原API文档:guava-30.0-jre-javadoc.jar; 赠送源代码:guava-30.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.0-jre.pom; 包含翻译后的API文档:guava-30.0-jre-...
赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...
guava-23.6-android guava 版本23.6的 jar 包
赠送jar包:guava-16.0.1.jar; 赠送原API文档:guava-16.0.1-javadoc.jar; 赠送源代码:guava-16.0.1-sources.jar; 赠送Maven依赖信息文件:guava-16.0.1.pom; 包含翻译后的API文档:guava-16.0.1-javadoc-API...
赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...