Google Guava 实战之Lists篇

 
     com.google.guava
     guava
     27.0.1-jre
 
 
     org.projectlombok
     lombok
 
package com.lucifer.guava.collections;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.checkerframework.checker.nullness.qual.Nullable;

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

/**
 * author: lucifer
 * date: 2020/5/27 10:20
 * description: TODO
 */
public class test4 {

    public static void main(String[] args) {
        List users1 = new ArrayList<>();
        users1.add(new User1("a", 20));
        users1.add(new User1("b", 21));
        users1.add(new User1("c", 22));

        List list = new ArrayList<>();
        list.add(1);
        list.add(2);
        /**
         * 1、cartesianProduct
         * Lists.cartesianProduct:通过从每个给定列表中依次选择一个元素,返回可以形成的所有可能列表;列表的“ n元笛卡尔积 ”。
         */
        List> cartesianProduct = Lists.cartesianProduct(users1, list);
        System.out.println("Lists.cartesianProduct:===============cartesianProduct:" + cartesianProduct);
        /**
         * 	2、reverse(List list)
         * 返回指定列表的反向视图。
         */
        List reverse = Lists.reverse(list);
        System.out.println("Lists.reverse=============revers:" + reverse);
        /**
         * 3、transform(List fromList, Function function)
         * 返回适用function于的每个元素的列表fromList。
         */
        List transform = Lists.transform(users1, new Function() {
            @Nullable
            @Override
            public Object apply(@Nullable User1 user) {
                return user.getUserName();
            }
        });
        System.out.println("Lists.transform:================transform:" + transform);
        /**
         * 4、partition(List, int)
         * 把List按指定大小分割
         */
        List> partition = Lists.partition(users1, 2);
        System.out.println("Lists.partition:===========partition:" + partition);
        /**
         * 5、Lists.charactersOf(String string)
         * 返回指定字符串的视图作为不可变Character值列表。
         */
        ImmutableList characters = Lists.charactersOf("张三在学guava");
        System.out.println("Lists.charactersOf(String string)=================="+characters);
        /**
         * 6、Lists.charactersOf(CharSequence sequence)
         *返回指定CharSequence为的List视图sequence,以Unicode代码单元序列的形式查看 。
         */
        List characters1 = Lists.charactersOf(new StringBuffer("张三在学guava"));
        System.out.println("Lists.charactersOf(CharSequence sequence)=================="+characters1);

    }
}

@AllArgsConstructor
@Data
class User1 {
    private String userName;
    private Integer age;
} 
  

控制台打印: 

Lists.cartesianProduct:===============cartesianProduct:[[User1(userName=a, age=20), 1], [User1(userName=a, age=20), 2], [User1(userName=b, age=21), 1], [User1(userName=b, age=21), 2], [User1(userName=c, age=22), 1], [User1(userName=c, age=22), 2]]
Lists.reverse=============revers:[2, 1]
Lists.transform:================transform:[a, b, c]
Lists.partition:===========partition:[[User1(userName=a, age=20), User1(userName=b, age=21)], [User1(userName=c, age=22)]]
Lists.charactersOf(String string)==================[张, 三, 在, 学, g, u, a, v, a]
Lists.charactersOf(CharSequence sequence)==================[张, 三, 在, 学, g, u, a, v, a]

 

你可能感兴趣的:(java8,guava)