guava之Lists、Sets、Maps用法

guava处理java.util.List的工具类
guava处理java.util.Set的工具类
guava处理java.util.Map的工具类

Lists常用方法

package com.me.guava.collect;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yanyugang
 * @description
 * @date 2019-11-09 11:44
 */
public class ListsTest {
    public static void main(String[] args){
        coreFunList();
    }

    // Lists主要方法
    public static void coreFunList(){
        System.out.println("---------字符串转集合--------------");
        String str="10010";
        ArrayList<String> list=Lists.newArrayList(str);
        list.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("----------数组转集合-------------");
        String[] array={"1", "2", "3", "4"};
        ArrayList<String> strings=Lists.newArrayList(array);
        strings.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("----------数组反转-------------");
        List<String> reverse=Lists.reverse(strings);
        reverse.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("----------添加新元素-------------");
        //return an unmodifiable list containing the specified elements
        List<String> addList=Lists.asList("0", array);
        addList.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("----------获取子集合-------------");
        //a list of consecutive sublists
        List<List<String>> partition=Lists.partition(addList, 2);
        partition.forEach(n -> System.out.print(n + " "));
        System.out.println();
    }
}

Sets常用方法

package com.me.guava.collect;

import com.google.common.collect.Sets;

import java.util.HashSet;
import java.util.Set;

/**
 * @author yanyugang
 * @description
 * Static utility methods pertaining to {@link Set} instances.
 * @date 2019-11-09 15:01
 */
public class SetsTest {
    public static void main(String[] args){
        coreFunList();
    }

    // Sets主要方法
    public static void coreFunList(){
        System.out.println("---------字符串转集合--------------");
        HashSet<String> set1=Sets.newHashSet("1","8");
        set1.forEach(n -> System.out.print(n + " "));
        System.out.println();
        HashSet<String> set2=Sets.newHashSet("1","2");
        set2.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("---------set1和set2并集--------------");
        Sets.SetView<String> union=Sets.union(set1, set2);
        union.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("---------set1和set2交集--------------");
        Sets.SetView<String> intersection=Sets.intersection(set1, set2);
        intersection.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("---------set1和set2差集--------------");
        //差集 1中有而2中没有的
        Sets.SetView<String> difference=Sets.difference(set1, set2);
        difference.forEach(n -> System.out.print(n + " "));
        System.out.println();
        System.out.println("---------set1和set2相对差集--------------");
        //相对差集 1中有2中没有  2中有1中没有的 取出来做结果
        Sets.SetView<String> symmetricDifference=Sets.symmetricDifference(set1, set2);
        symmetricDifference.forEach(n -> System.out.print(n + " "));
        System.out.println();

    }
}

Maps常用方法

package com.me.guava.collect;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author yanyugang
 * @description
 * Static utility methods pertaining to {@link Map} instances
 * @date 2019-11-09 16:52
 */
public class MapsTest {
    static Book book=new Book.BookBuilder().isbn("ISBN123").title("book1").build();
    static Book book2=new Book.BookBuilder().isbn("ISBN456").title("book2").build();
    static Book book3=new Book.BookBuilder().isbn("ISBN789").title("book3").build();
    static List<Book> books=Lists.newArrayList(book, book2, book3);


    public static void main(String[] args){
        setToMap();
    }

    /**
     * List 转 Map
     */
    public static void listToMap(){

        Map<Object, Book> bookMap=Maps.uniqueIndex(books, book1 -> book1.getIsbn());

        bookMap.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value.getTitle());
        });
    }

    /**
     * Set 转 Map
     * Returns a live {@link Map} view whose keys are the contents of {@code set}
     * and whose values are computed on demand using {@code function}.
     */
    public static void setToMap(){
        Set<Book> bookSet=Sets.newHashSet(books);
        Map<Book, String> bookMap=Maps.asMap(bookSet, input -> input.getIsbn());
        System.out.println(bookMap.size());
        bookMap.forEach((key, value) -> {
            System.out.println(key.getTitle());
            System.out.println(value);
        });
    }
    
}

教程

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值