guava 集合和函数接口妙用

//利用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"}
		 */
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值