java guava使用之Collections2

package com.xasmall.guava;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

/*
 * guava Collections2中方法:orderPermutations与Permutations
 * orderPermutations先将元素排序,在排列
 * Permutations直接排列
 * 排列数据
 * filter过滤器
 * transform变换器
 */

public class Demo1 {
    public static void main(String[] args) {
        List<String> list=Lists.newArrayList("hello","world","javab","c","someone");
        Collection<List<String>> collection=Collections2.orderedPermutations(list);
        System.out.println(collection.size());
        for(List<String> temp:collection) {
            System.out.println(temp);
        }

        System.out.println("==============使用比较器=================");
        Collection<List<String>> collection5=Collections2.orderedPermutations(list,new SimpleCom());
        System.out.println(collection5.size());
        for(List<String> temp:collection5) {
            System.out.println(temp);
        }

        System.out.println();
        System.out.println();
        Collection<List<String>> collection2=Collections2.permutations(list);
        for(List<String> temp:collection2) {
            System.out.println(temp);
        }

        Collection<String> collection3=Collections2.filter(list, new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return input.length()>=5?true:false;
            }
        });
        System.out.println();
        for(String temp:collection3) {
            System.out.println(temp);
        }

        Collection<String> collection4=Collections2.transform(collection3, new Function<String, String>() {

            @Override
            public String apply(String input) {
                return input.toUpperCase();
            }
        });
        System.out.println();
        for(String temp:collection4) {
            System.out.println(temp);
        }
        Collections.sort(list);
    }
}

比较器实现类

package com.xasmall.guava;

import java.util.Comparator;

/*
 * 使用降序排列
 */
public class SimpleCom implements Comparator<String>{

    @Override
    public int compare(String o1, String o2) {
        int a1=o1.length();
        int a2=o2.length();
        if(a1!=a2) {
            return a2-a1;
        }
        if(a1==a2) {
            char[] arr1=o1.toCharArray();
            char[] arr2=o2.toCharArray();
            for(int i=0;i<a1;i++) {
                if(arr1[i]>arr2[i]) {
                    return -1;
                }
                if(arr1[i]<arr2[i]) {
                    return 1;
                }
            }
        }
        return 0;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值