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";
}
});
}
}
TestGuava
最新推荐文章于 2022-10-10 14:30:43 发布