Guava 的 Lists 类全路径:
com.google.common.collect.Lists
创建 java.util.ArrayList
示例1:使用不定参数构造 ArrayList
import com.google.common.collect.Lists;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 2, 3);
System.out.println(list);
}
}
运行结果:
[1, 2, 3]
示例2:将其他 Collection 类型的集合转换成 ArrayList
import com.google.common.collect.Lists;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListsTest {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
List<String> list = Lists.newArrayList(set);
System.out.println(list);
}
}
运行结果:
[a, b, c]
使用 reverse 方法翻转 List
import com.google.common.collect.Lists;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<String> originList = Lists.newArrayList("1", "2");
List<String> reversedList = Lists.reverse(originList);
System.out.println("原List: " + originList);
System.out.println("翻转后: " + reversedList);
}
}
运行结果:
原List: [1, 2]
翻转后: [2, 1]
将字符串拆分为字符组成的列表
import com.google.common.collect.Lists;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<Character> list = Lists.charactersOf("Hello");
System.out.println(list);
}
}
运行结果:
[H, e, l, l, o]
使用 partition 方法将一个List拆分为多个List
partition 方法,会将一个 List 拆分为多个 List,每个 List (最大)大小为指定大小。
示例:
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// 按大小 2 拆分
List<List<Integer>> listPartitionList = Lists.partition(list, 2);
System.out.println("原 List:");
System.out.println(list);
System.out.println("拆分后:");
for (List<Integer> partition: listPartitionList) {
System.out.println(partition);
}
}
}
运行结果如下:
原 List:
[1, 2, 3, 4, 5]
拆分后:
[1, 2]
[3, 4]
[5]
计算多个List的笛卡尔乘积
示例1:
import com.google.common.collect.Lists;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<List<String>> result = Lists.cartesianProduct(
Lists.newArrayList("1", "2"),
Lists.newArrayList("a", "b", "c")
);
for (List<String> item : result) {
System.out.println(item);
}
}
}
运行结果:
[1, a]
[1, b]
[1, c]
[2, a]
[2, b]
[2, c]
示例2:
import com.google.common.collect.Lists;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<List<String>> result = Lists.cartesianProduct(
Lists.newArrayList("1", "2"),
Lists.newArrayList("a", "b", "c"),
Lists.newArrayList("mmm", "nnn")
);
for (List<String> item : result) {
System.out.println(item);
}
}
}
运行结果:
[1, a, mmm]
[1, a, nnn]
[1, b, mmm]
[1, b, nnn]
[1, c, mmm]
[1, c, nnn]
[2, a, mmm]
[2, a, nnn]
[2, b, mmm]
[2, b, nnn]
[2, c, mmm]
[2, c, nnn]
使用 Lists.transform 处理每个List元素并生成新List
示例:
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
public class ListsTest {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 2, 3);
List<String> result = Lists.transform(list, new Function<Integer, String>() {
@Override
public String apply(@Nullable Integer input) {
return "Hello " + input;
}
});
System.out.println("list: " + list);
System.out.println("result: " + result);
}
}
运行结果:
list: [1, 2, 3]
result: [Hello 1, Hello 2, Hello 3]
如果使用的是 Java 8 或以上版本,可以使用自带的 Stream :
import com.google.common.collect.Lists;
import java.util.List;
import java.util.stream.Collectors;
public class ListsTest {
public static void main(String[] args) {
List<Integer> list = Lists.newArrayList(1, 2, 3);
List<String> result = list.stream().map(item -> "Hello " + item).collect(Collectors.toList());
System.out.println("list: " + list);
System.out.println("result: " + result);
}
}