|
@@ -23,25 +23,27 @@ public class ListUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static <T> void forEach(List<T> list, Action1<T> action1) {
|
|
public static <T> void forEach(List<T> list, Action1<T> action1) {
|
|
|
- if(list != null)
|
|
|
|
|
|
|
+ if (list != null)
|
|
|
for (T t : list) {
|
|
for (T t : list) {
|
|
|
action1.call(t);
|
|
action1.call(t);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T> T findItem(List<T> list, CallBack<T, Boolean> action1) {
|
|
public static <T> T findItem(List<T> list, CallBack<T, Boolean> action1) {
|
|
|
- if(list != null)
|
|
|
|
|
|
|
+ if (list != null)
|
|
|
for (T t : list) {
|
|
for (T t : list) {
|
|
|
- if(action1.call(t)){
|
|
|
|
|
|
|
+ if (action1.call(t)) {
|
|
|
return t;
|
|
return t;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T> List<T> findIList(List<T> list, CallBack<T, Boolean> action1) {
|
|
public static <T> List<T> findIList(List<T> list, CallBack<T, Boolean> action1) {
|
|
|
List<T> findList = new ArrayList<>();
|
|
List<T> findList = new ArrayList<>();
|
|
|
- if(list != null)
|
|
|
|
|
|
|
+ if (list != null)
|
|
|
for (T t : list) {
|
|
for (T t : list) {
|
|
|
- if(action1.call(t)){
|
|
|
|
|
|
|
+ if (action1.call(t)) {
|
|
|
findList.add(t);
|
|
findList.add(t);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -56,6 +58,7 @@ public class ListUtil {
|
|
|
int size = ListUtil.size(list);
|
|
int size = ListUtil.size(list);
|
|
|
return size > 0 ? list.get(-1 + size) : null;
|
|
return size > 0 ? list.get(-1 + size) : null;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T> T getLast(List<T> list, int index) {
|
|
public static <T> T getLast(List<T> list, int index) {
|
|
|
int size = ListUtil.size(list);
|
|
int size = ListUtil.size(list);
|
|
|
return getItem(list, size - 1 - index);
|
|
return getItem(list, size - 1 - index);
|
|
@@ -174,8 +177,9 @@ public class ListUtil {
|
|
|
Collections.addAll(list, items);
|
|
Collections.addAll(list, items);
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T> ArrayList<T> asList(List<T> itemList) {
|
|
public static <T> ArrayList<T> asList(List<T> itemList) {
|
|
|
- if(itemList instanceof ArrayList){
|
|
|
|
|
|
|
+ if (itemList instanceof ArrayList) {
|
|
|
return (ArrayList<T>) itemList;
|
|
return (ArrayList<T>) itemList;
|
|
|
}
|
|
}
|
|
|
ArrayList<T> list = emptyList();
|
|
ArrayList<T> list = emptyList();
|
|
@@ -183,6 +187,15 @@ public class ListUtil {
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static String join(List list) {
|
|
|
|
|
+ StringBuffer sb = new StringBuffer("");
|
|
|
|
|
+ for (Object obj : list) {
|
|
|
|
|
+ if (sb.length() != 0) sb.append(",");
|
|
|
|
|
+ sb.append(obj.toString());
|
|
|
|
|
+ }
|
|
|
|
|
+ return sb.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@SafeVarargs
|
|
@SafeVarargs
|
|
|
public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, T... items) {
|
|
public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, T... items) {
|
|
|
ArrayList<R> list = emptyList();
|
|
ArrayList<R> list = emptyList();
|
|
@@ -192,11 +205,12 @@ public class ListUtil {
|
|
|
}
|
|
}
|
|
|
return list;
|
|
return list;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, List<T> itemList, CallBack<T, Boolean> filterCallBack) {
|
|
public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, List<T> itemList, CallBack<T, Boolean> filterCallBack) {
|
|
|
ArrayList<R> list = emptyList();
|
|
ArrayList<R> list = emptyList();
|
|
|
for (T t : itemList) {
|
|
for (T t : itemList) {
|
|
|
R r = callBack.call(t);
|
|
R r = callBack.call(t);
|
|
|
- if(filterCallBack == null || filterCallBack.call(t))
|
|
|
|
|
|
|
+ if (filterCallBack == null || filterCallBack.call(t))
|
|
|
list.add(r);
|
|
list.add(r);
|
|
|
}
|
|
}
|
|
|
return list;
|
|
return list;
|
|
@@ -214,6 +228,7 @@ public class ListUtil {
|
|
|
list.removeAll(removeList);
|
|
list.removeAll(removeList);
|
|
|
return removeList;
|
|
return removeList;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
public static <T> T getItem(List<T> list, CallBack<T, Boolean> callBack) {
|
|
public static <T> T getItem(List<T> list, CallBack<T, Boolean> callBack) {
|
|
|
if (isEmpty(list)) {
|
|
if (isEmpty(list)) {
|
|
|
return null;
|
|
return null;
|
|
@@ -252,7 +267,7 @@ public class ListUtil {
|
|
|
//是事包含指定字符串
|
|
//是事包含指定字符串
|
|
|
public static boolean contains(String[] items, String src) {
|
|
public static boolean contains(String[] items, String src) {
|
|
|
for (String item : items) {
|
|
for (String item : items) {
|
|
|
- if(TextUtils.equals(item, src)){
|
|
|
|
|
|
|
+ if (TextUtils.equals(item, src)) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -262,17 +277,18 @@ public class ListUtil {
|
|
|
//插入第几个位置
|
|
//插入第几个位置
|
|
|
public static <T> boolean insertIndex(List<T> allList, T item, int index) {
|
|
public static <T> boolean insertIndex(List<T> allList, T item, int index) {
|
|
|
int size = size(allList);
|
|
int size = size(allList);
|
|
|
- if(index > size){
|
|
|
|
|
|
|
+ if (index > size) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
allList.add(index, item);
|
|
allList.add(index, item);
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
//插入倒数第几个位置
|
|
//插入倒数第几个位置
|
|
|
public static <T> boolean insertLastIndex(List<T> allList, T item, int index) {
|
|
public static <T> boolean insertLastIndex(List<T> allList, T item, int index) {
|
|
|
int size = size(allList);
|
|
int size = size(allList);
|
|
|
int position = size - index;
|
|
int position = size - index;
|
|
|
- if(position < 0){
|
|
|
|
|
|
|
+ if (position < 0) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
allList.add(position, item);
|
|
allList.add(position, item);
|
|
@@ -283,11 +299,12 @@ public class ListUtil {
|
|
|
public static interface CallBack<I, R> {
|
|
public static interface CallBack<I, R> {
|
|
|
public R call(I i);
|
|
public R call(I i);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
//列表拼接为字符串
|
|
//列表拼接为字符串
|
|
|
- public static String listToUrls(List<String> list){
|
|
|
|
|
|
|
+ public static String listToUrls(List<String> list) {
|
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
- if(i != 0)
|
|
|
|
|
|
|
+ if (i != 0)
|
|
|
sb.append(";");
|
|
sb.append(";");
|
|
|
sb.append(list.get(i));
|
|
sb.append(list.get(i));
|
|
|
}
|
|
}
|