之前写过一个简单的使用,发现没有什么实用价值。这次又遇到实用的场景,所以完整写一个整体案例,以备后续参考
// 定义一个实体类,做排序时候的逻辑处理
public class Person {
private Integer age;
// 省略get和set方法……
public Person() {
}
// 增加一个验证
public Person(Integer age) {
Preconditions.checkArgument(age < 0 , "年龄 %s 错误", age);
this.age = age;
}
}
// 1.1 通过工厂直接生产list;泛型约束传入参数的类型;
List klist = Lists.newArrayList();
List ilist = Lists.newArrayList(1,2);
List slist = Lists.newArrayList("1","2");
// 1.2 Array转List
List array_to_list = Lists.newArrayList(new Integer[]{1,2});
// 1.3 Set转List
Set set = Sets.newHashSet(1,2);
List set_to_list = Lists.newArrayList(set);
// 1.4、定义固定大小的新集合
List glist = Lists.newArrayListWithCapacity(4);
// 1.5 定义预期大小的新集合,生成集合大小公式:5L + arraySize + (arraySize / 10)
List ylist = Lists.newArrayListWithExpectedSize(3);
// 1.6 将指定元素加入数组,转型为固定长度list,该list不可变。
// 多用于将一个标示加入数组的头部,来判断该数组的状态
List op1list = Lists.asList("a", "b", new String[]{"1","2"});
List op2list = Lists.asList("a", new String[]{"1","2"});
// [a, b, 1, 2]
// [a, 1, 2]
// 1.7 笛卡尔积 展示所有排列组合
List clist = Lists.cartesianProduct(Lists.newArrayList(1,2,3)
,Lists.newArrayList(4,5,6)
,Lists.newArrayList(new Integer[]{7,8,9}));
// [[1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 6, 7], [1, 6, 8], ...]
// 1.8 list分区
List plist = Lists.partition(clist, 2);
// [[[1, 4, 7], [1, 4, 8]], [[1, 4, 9], [1, 5, 7]], [[1, 5, 8], [1, 5, 9]], [[1, 6, 7], [1, 6, 8]],...]]
// 1.9 字符串转list,注意返回为char类型的list,*注意:该list不可变
List imlist = Lists.charactersOf("world");
// [w, o, r, l, d]
// 1.10 排序-颠倒,如果传入的list是不可变的,则颠倒以后的也不可变
List relist1 = Lists.reverse(imlist);
// [d, l, r, o, w]
List relist2 = Lists.reverse(Lists.newArrayList(1, 2, 3, 4, 5));
relist2.add(6);
// [6, 5, 4, 3, 2, 1]
// 其他一些方法可以参考源码,例如该包内还包括:LinkedList、CopyOnWriteArrayList处理
//其他例如内置:size、get、add、clear、remove、removeIf、removeRange、listIterator、isEmpty、indexOf、lastIndexOf、subList、iterator
// 创建
ImmutableList iList = ImmutableList.of("a", "b", "c");
// 集合、数组均可
ImmutableList cList = ImmutableList.copyOf(iList);
ImmutableList c1List = ImmutableList.copyOf(Lists.charactersOf("hello"));
ImmutableList c2List = ImmutableList.copyOf(Lists.newArrayList("h", "e", "l", "l", "o"));
// 字符串转list,注意返回为Character类型的list
ImmutableList imtlist = Lists.charactersOf("hello");
// 自然排序
ImmutableList sortList = ImmutableList.sortedCopyOf(Lists.newArrayList("h", "e", "l", "l", "o"));
ImmutableList sort1List = ImmutableList.sortedCopyOf(Lists.newArrayList(2,3,41,2,5,6234));
// 排序,可以转入自己的Comparator函数,见下方方法
ImmutableList sort2List = ImmutableList.sortedCopyOf(new MyComparator(), new Person().getPerson());
// 颠倒
ImmutableList sort3List = sort1List.reverse();
// 比较
ImmutableList eqList1 = ImmutableList.of("a", "b", "c");
ImmutableList eqList2 = ImmutableList.of("a", "b", "c");
boolean b = eqList1.equals(eqList2);
// true
// 查看源码还有一些其他方法:iterator、forEach、indexOf、lastIndexOf、get、size、subList等
// 自己的比较函数
public class MyComparator implements Comparator<Person> {
public int compare(Person o1, Person o2) {
int age1 = o1.getAge();
int age2 = o2.getAge();
return age1 - age2;
}
}
HashSet setA = Sets.newHashSet(1, 2, 3, 4, 5);
HashSet setB = Sets.newHashSet(4, 5, 6, 7, 8);
// 并集
SetView union = Sets.union(setA, setB);
// 差集
SetView difference = Sets.difference(setA, setB);
// 交集
SetView intersection = Sets.intersection(setA, setB);
// 源码还有一些其他方法:EnumSet、CopyOnWriteArraySet、newTreeSet、newLinkedHashSet、newConcurrentHashSet 等
// 创建hashMap
Map<String, String> map = Maps.newHashMap();
// 初始化不可变
Map<String, String> map1 = ImmutableMap.of("ON","TRUE","OFF","FALSE");
Map<String, String> map3 = ImmutableMap.of("ON","TRUE","OFF","FALSE1", "OUT", "YES");
Map<String, Person> map2 = ImmutableMap.of("hello",new Person(1),"world",new Person(2));
// 2个map比较,获取交集、差集、并集
MapDifference md = Maps.difference(map1, map3);
Map m = md.entriesOnlyOnLeft(); // 只有左边存在
Map m1 = md.entriesOnlyOnRight(); // 只有右边存在
Map m2 = md.entriesDiffering(); // key相同value不同
Map m3 = md.entriesInCommon(); // 相同
// 源码还有一些其他方法:asMap、equals、newLinkedHashMap、newConcurrentMap、newTreeMap、newEnumMap、newIdentityHashMap等