org.yx.util.CollectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sumk Show documentation
Show all versions of sumk Show documentation
A quick developing framewort for internet company
/**
* Copyright (C) 2016 - 2030 youtongluan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.yx.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.yx.common.sumk.UnmodifiableArrayList;
/**
* 本类的许多方法都会对key、value做trim()处理
*/
public final class CollectionUtil {
@SafeVarargs
public static List list(T... a) {
List list = new ArrayList<>();
Collections.addAll(list, a);
return list;
}
@SafeVarargs
public static Set hashSet(T... a) {
Set set = new HashSet<>();
Collections.addAll(set, a);
return set;
}
public static Map loadMap(InputStream in, boolean keepNullValue) throws IOException {
if (in == null) {
return null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
return loadMap(reader, keepNullValue);
}
public static Map loadMapFromText(String text, String bigDelimiter, String smallDelimiter) {
Map map = new HashMap<>();
for (String entry : text.split(bigDelimiter)) {
entry = entry.trim();
if (StringUtil.isEmpty(entry)) {
continue;
}
String[] vs = entry.split(smallDelimiter, 2);
switch (vs.length) {
case 1:
map.put(vs[0].trim(), null);
break;
case 2:
map.put(vs[0].trim(), vs[1].trim());
break;
default:
continue;
}
}
return map;
}
public static String saveMapToText(Map map, String bigDelimiter, String smallDelimiter) {
StringBuilder sb = new StringBuilder();
map.forEach((k, v) -> {
sb.append(k);
if (v != null) {
sb.append(smallDelimiter).append(v);
}
sb.append(bigDelimiter);
});
return sb.toString();
}
public static Map loadMap(Reader in, boolean keepNullValue) throws IOException {
BufferedReader reader = BufferedReader.class.isInstance(in) ? (BufferedReader) in : new BufferedReader(in);
Map map = new HashMap<>();
try {
String line = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#")) {
continue;
}
String[] vs = line.split("=", 2);
if (vs.length != 2) {
if (keepNullValue) {
map.put(vs[0].trim(), null);
}
continue;
}
map.put(vs[0].trim(), vs[1].trim());
}
} finally {
reader.close();
}
return map;
}
public static List loadList(InputStream in) throws IOException {
if (in == null) {
return Collections.emptyList();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
List list = new ArrayList<>();
try {
String line = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("#") || line.isEmpty()) {
continue;
}
list.add(line);
}
return list;
} finally {
reader.close();
}
}
public static List splitToList(String text, String delimiter) {
if (text == null || text.isEmpty()) {
return Collections.emptyList();
}
String[] arrray = text.split(delimiter);
List list = new ArrayList<>(arrray.length);
for (String data : list) {
data = data.trim();
if (data == null || data.isEmpty()) {
continue;
}
list.add(data);
}
return list;
}
public static boolean isEmpty(Map, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isEmpty(Collection> colletion) {
return colletion == null || colletion.isEmpty();
}
public static boolean isNotEmpty(Collection> colletion) {
return colletion != null && colletion.size() > 0;
}
@SuppressWarnings("unchecked")
public static Map flatMapToTree(Map map) {
Map ret = new HashMap<>();
for (Map.Entry entry : map.entrySet()) {
String k = entry.getKey();
String v = entry.getValue();
if (!k.contains(".")) {
ret.put(k, v);
continue;
}
String[] ks = k.split("\\.");
int lastIndex = ks.length - 1;
Map temp = ret;
for (int i = 0; i < lastIndex; i++) {
String k0 = ks[i];
Object obj = temp.get(k0);
if (obj == null) {
Map temp2 = new HashMap<>();
temp.put(k0, temp2);
temp = temp2;
continue;
}
temp = (Map) obj;
continue;
}
temp.put(ks[lastIndex], v);
}
return ret;
}
public static Map subMap(Map source, String prefix) {
int len = prefix.length();
Map map = new HashMap<>();
source.forEach((key, value) -> {
if (key.startsWith(prefix)) {
map.put(key.substring(len), value);
}
});
return map;
}
public static Map removeNull(Map map) {
Map ret = new HashMap<>();
map.forEach((k, v) -> {
if (k == null || v == null) {
return;
}
ret.put(k, v);
});
return ret;
}
public static List unmodifyList(Collection col, Class clz) {
if (col == null) {
return null;
}
if (col.isEmpty()) {
return Collections.emptyList();
}
if (col.size() == 1) {
return Collections.singletonList(col.iterator().next());
}
return new UnmodifiableArrayList<>(col, clz);
}
public static List unmodifyList(T[] arr) {
if (arr == null) {
return null;
}
if (arr.length == 0) {
return Collections.emptyList();
}
if (arr.length == 1) {
return Collections.singletonList(arr[0]);
}
return new UnmodifiableArrayList<>(arr);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy