前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >guava 集合和函数接口妙用

guava 集合和函数接口妙用

作者头像
用户5166556
发布2019-04-16 14:24:02
4910
发布2019-04-16 14:24:02
举报
代码语言:javascript
复制
//利用partition进行对数据进行分组
	@Test
	public void test26(){
		List<String> list = ImmutableList.of("hello", "HI", "Hey");
		List<List<String>> partition = Lists.partition(list, 2);
		System.out.println(partition);
	}

//Lists中的transform方法(通过函数式接口)能够对数据进行处理
	@Test
	public void test25(){
		List<String> list = ImmutableList.of("hello", "HI", "Hey");
		List<String> transform = Lists.transform(list, new Function<String, String>() {
			@Override
			public String apply(String input) {
				if(input.contentEquals("HI"))
					System.out.println(input);
				return input;
			}
		});
		System.out.println(transform);
	}

//通过函数式接口,把处理后的数据放到一个map中
	// 根据特征进行筛选集合中的数据
	@Test
	public void test12() {
		ImmutableSet<String> digits = ImmutableSet.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
		Function<String, Integer> lengthFunction = new Function<String, Integer>() {
			public Integer apply(String string) {
				return string.length();
			}
		};
		ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction);
		System.out.println(digitsByLength);
		/*
		 * digitsByLength maps: 3 => {"one", "two", "six"} 4 => {"zero", "four",
		 * "five", "nine"} 5 => {"three", "seven", "eight"}
		 */
	}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015年02月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档