public class Lists2 extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static interface |
Lists2.BinaryComparator<T,K> |
| 构造器和说明 |
|---|
Lists2() |
| 限定符和类型 | 方法和说明 |
|---|---|
static int[] |
_int(Map<String,Object> map,
String... names) |
static int |
_int(Object s) |
static <T> List<List<T>> |
chunkWithNumber(List<T> list,
int n)
列表均分 n 等分(部分子列表比后面的子列表可能多1个
[] --chunk3--> []
[1] --chunk3--> [[1]]
[1, 2] --chunk3--> [[1], [2]]
[1, 2, 3] --chunk3--> [[1], [2], [3]]
[1, 2, 3, 4] --chunk3--> [[1, 2], [3], [4]]
[1, 2, 3, 4, 5] --chunk3--> [[1, 2], [3, 4], [5]]
[1, 2, 3, 4, 5, 6] --chunk3--> [[1, 2], [3, 4], [5, 6]]
[1, 2, 3, 4, 5, 6, 7] --chunk3--> [[1, 2, 3], [4, 5], [6, 7]]
[1, 2, 3, 4, 5, 6, 7, 8] --chunk3--> [[1, 2, 3], [4, 5, 6], [7, 8]]
|
static <F> List<F> |
filter(Collection<F> list,
Predicate<? super F> predicate) |
static <T> Optional<T> |
findFirst(Iterable<T> items,
Predicate<T> predicate) |
static <T> T |
first(Iterable<T> items,
T defaultValue)
获取集合的第一个元素
|
static int |
firstInt(String s,
int defaultValue) |
static long |
firstLong(String s,
long defaultValue) |
static <T> T |
get(List<T> items,
int index)
从list中获取指定索引位置的对象,如果索引越界,则使用null填充
|
static <T> T |
get(List<T> items,
int index,
T defaultValue)
从list中获取指定索引位置的对象,如果索引越界,则使用默认值defaultValue填充
|
static String |
getSplit(String s,
int index,
String defaultValue) |
static String |
getSplit(String s,
String sep,
int index,
String defaultValue) |
static <T,K> Optional<T> |
indexedBinarySearch(List<? extends T> list,
K key,
Lists2.BinaryComparator<? super T,K> c)
二分查找快速排序
|
static <F> String |
joining(Collection<F> list) |
static <F> String |
joining(Collection<F> list,
Function<? super F,String> mapper) |
static <F> String |
joining(Collection<F> list,
Function<? super F,String> mapper,
String delimiter,
String prefix,
String suffix)
将一个集合转换成字符串
|
static long |
lastLong(String s,
long defaultValue)
因为有些地方有且只有两个属性 需要分别取出
|
static <F,T> List<T> |
map(Collection<F> list,
Function<? super F,? extends T> mapper) |
static <F,T> Set<T> |
mapSet(Collection<F> list,
Function<? super F,? extends T> mapper) |
static <T> T |
random(List<T> views)
随机获取列表中的一个元素
|
static <T> List<T> |
randomList(List<T> views,
int max)
随机获取子列表(伪随机算法)
目标是从N个元素随机选出M个子元素(0 <M < N), 保证每个元素的机会均等。
|
static List<String> |
splitWithTrim(String s) |
static List<String> |
splitWithTrim(String s,
String sep) |
static int[] |
toIntArray(String numbers) |
static List<Integer> |
toIntegerList(Collection<String> numbers) |
static List<Integer> |
toIntegerList(Collection<String> numbers,
boolean ignoreError) |
static List<Integer> |
toIntegerList(String numbers) |
static List<Integer> |
toIntegerList(String[] numbers) |
static List<Integer> |
toIntegerList(String numbers,
boolean ignoreError) |
static List<Long> |
toLongList(List<String> numbers) |
static List<Long> |
toLongList(List<String> numbers,
boolean ignoreError) |
static List<Long> |
toLongList(List<String> numbers,
long defaultValue) |
static List<Long> |
toLongList(String numbers) |
static List<Long> |
toLongList(String[] numbers) |
static List<Long> |
toLongList(String numbers,
boolean ignoreError) |
static List<Long> |
toLongList(String numbers,
long defaultValue) |
static <F,T> List<T> |
transform(Collection<F> list,
Function<? super F,? extends T> mapper) |
static <F,T> List<T> |
transform(Collection<F> list,
Predicate<? super F> predicate,
Function<? super F,? extends T> mapper)
将一个list转换成另一个list
|
static List<String> |
wrap(List<?> items) |
static List<String> |
wrap(List<?> items,
char wrapChar) |
static final Random random
public static int _int(Object s)
public static <T> T random(List<T> views)
views - 列表public static <T> List<T> randomList(List<T> views, int max)
目标是从N个元素随机选出M个子元素(0 <M < N), 保证每个元素的机会均等。
算法是:随机选择一个数K(0 <= K < N),每次选择K的左右各一个元素,直到都达到边界或者获取到M个元素中止
views - 集合max - 结果集最多数量public static int[] toIntArray(String numbers)
public static List<Integer> toIntegerList(Collection<String> numbers)
public static List<Integer> toIntegerList(Collection<String> numbers, boolean ignoreError)
public static <T> T get(List<T> items, int index, T defaultValue)
items - list对象,最好是RandomAccessindex - 索引值,从0开始defaultValue - 默认值public static <T> T get(List<T> items, int index)
items - list对象,最好是RandomAccessindex - 索引值,从0开始public static <T> T first(Iterable<T> items, T defaultValue)
items - 集合defaultValue - 集合为null或者空时的默认值public static int firstInt(String s, int defaultValue)
public static long firstLong(String s, long defaultValue)
public static long lastLong(String s, long defaultValue)
s - defaultValue - public static <T,K> Optional<T> indexedBinarySearch(List<? extends T> list, K key, Lists2.BinaryComparator<? super T,K> c)
T - 列表对象(必须已经按照从小到大排好序了)K - 列表对象要比较的keylist - 要排序的列表key - 要比较的key,对象不一定和列表对象相同c - 比较方法public static <T> List<List<T>> chunkWithNumber(List<T> list, int n)
[] --chunk3--> []
[1] --chunk3--> [[1]]
[1, 2] --chunk3--> [[1], [2]]
[1, 2, 3] --chunk3--> [[1], [2], [3]]
[1, 2, 3, 4] --chunk3--> [[1, 2], [3], [4]]
[1, 2, 3, 4, 5] --chunk3--> [[1, 2], [3, 4], [5]]
[1, 2, 3, 4, 5, 6] --chunk3--> [[1, 2], [3, 4], [5, 6]]
[1, 2, 3, 4, 5, 6, 7] --chunk3--> [[1, 2, 3], [4, 5], [6, 7]]
[1, 2, 3, 4, 5, 6, 7, 8] --chunk3--> [[1, 2, 3], [4, 5, 6], [7, 8]]
T - 对象类型list - 列表n - 最多均分份数public static <F,T> List<T> transform(Collection<F> list, Function<? super F,? extends T> mapper)
public static <F> List<F> filter(Collection<F> list, Predicate<? super F> predicate)
public static <F,T> List<T> transform(Collection<F> list, Predicate<? super F> predicate, Function<? super F,? extends T> mapper)
list - 原始listpredicate - 过滤条件mapper - 转换条件public static <F> String joining(Collection<F> list)
public static <F> String joining(Collection<F> list, Function<? super F,String> mapper)
public static <F,T> List<T> map(Collection<F> list, Function<? super F,? extends T> mapper)
public static <F,T> Set<T> mapSet(Collection<F> list, Function<? super F,? extends T> mapper)
Copyright © 2020. All rights reserved.