一、guava collect包 集合常用案例

之前写过一个简单的使用,发现没有什么实用价值。这次又遇到实用的场景,所以完整写一个整体案例,以备后续参考

1、定义一个对象,后面使用
// 定义一个实体类,做排序时候的逻辑处理
public class Person {
    private Integer age;

    // 省略get和set方法……

    public Person() {
    }
    // 增加一个验证
    public Person(Integer age) {
        Preconditions.checkArgument(age < 0 , "年龄 %s 错误", age);
        this.age = age;
    }
}
2、ArrayList常用

// 1.1 通过工厂直接生产list;泛型约束传入参数的类型;
 List klist = Lists.newArrayList();
 List<Integer> ilist = Lists.newArrayList(1,2);
 List<String> slist = Lists.newArrayList("1","2");

// 1.2 Array转List
 List<Integer> array_to_list = Lists.newArrayList(new Integer[]{1,2});

// 1.3 Set转List
 Set<Integer> set = Sets.newHashSet(1,2);
 List<Integer> set_to_list = Lists.newArrayList(set);

 // 1.4、定义固定大小的新集合
 List<Integer> glist = Lists.newArrayListWithCapacity(4);

 // 1.5 定义预期大小的新集合,生成集合大小公式:5L + arraySize + (arraySize / 10)
 List<Integer> 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
3、ImmutableList 不可变集合
// 创建
ImmutableList<String> iList = ImmutableList.of("a", "b", "c");

// 集合、数组均可
ImmutableList<String> cList = ImmutableList.copyOf(iList);
ImmutableList<Character> c1List = ImmutableList.copyOf(Lists.charactersOf("hello"));
ImmutableList<String> 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<String> eqList1 = ImmutableList.of("a", "b", "c");
ImmutableList<String> 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;
    }
}
4、Set
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 等
5、Maps
// 创建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等
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值