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;
}
}