TestGuava

package facilitator.guava;


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


import org.junit.Test;


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



public class TestGuava {


    private static class LengthLessThanPredicate implements Predicate<String> {
        private final int length;


        private LengthLessThanPredicate(final int length) {
            this.length = length;
        }


        public boolean apply(final String s) {
            return s.length() < length;
        }
    }


    public static Predicate<String> lengthLessThan(final int length) {
        return new LengthLessThanPredicate(length);
    }


    @Test
    public void testReplace() {
        //Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long, List<String>>>();
        //Map<String, Map<Long, List<String>>> map1 = Maps.newHashMap();


        Map<String, Double> cnPrices = Maps.newHashMap();
        cnPrices.put("1", 12.00);
        cnPrices.put("2", 52.00);
        cnPrices.put("3", 99.99);
        Map<String, Double> topMap = Maps.transformValues(cnPrices, new Function<Double, Double>() {
            Double cn2us = 6.0;


            @Override
            public Double apply(final Double arg0) {
                Double d = (Double) (arg0);
                return d * cn2us;
            }
        });


        if (topMap == null || topMap.isEmpty()) {
            System.out.println("EMPTY");
        }
        for (Double d : topMap.values()) {
            System.out.println(d);
        }
        /*        Iterable<String> filtered = filter(names,
                    or(or(equalTo("Aleksander"), equalTo("Jaran")), lengthLessThan(5)));*/


    }


    @Test
    public void testSort() {


        Comparator<Person> byLastName = new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getLastName().compareTo(o2.getLastName());
            }
        };
        Comparator<Person> byFirstName = new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getFirstName().compareTo(o2.getFirstName());
            }
        };


        Person p1 = new Person();
        p1.setFirstName("zhang");
        p1.setLastName("zhenpeng");
        Person p2 = new Person();
        p2.setFirstName("bai");
        p2.setLastName("xue");
        Person p3 = new Person();
        p3.setFirstName("zhang");
        p3.setLastName("keqiang");
        Person p4 = new Person();
        p4.setFirstName("wang");
        p4.setLastName("tao");
        Person p5 = new Person();
        p5.setFirstName("li");
        p5.setLastName("wei");


        List<Person> persons = Lists.newArrayList(p1, p2, p3, p4, p5, p2, p5, p1);
        List<Person> sortedCopy = Ordering.from(byFirstName).compound(byLastName)
            .sortedCopy(persons);
        for (Person p : sortedCopy) {
            System.out.println(p.getFirstName() + " " + p.getLastName());
        }


        Collections.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return ComparisonChain.start().compare(o1.getFirstName(), o2.getFirstName())
                    .compare(o1.getLastName(), o2.getLastName()).result();
            }
        });
        for (Person p : persons) {
            System.out.println(p.getFirstName() + " " + p.getLastName());
        }


        Collection<Person> filterCollection = Collections2.filter(persons, new Predicate<Person>() {
            @Override
            public boolean apply(Person input) {
                return input.getFirstName() == "zhang";
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值