java listutils_Java的list自定义工具类ListUtils
/**
* 將list中map的key為ID的值作為KEY在套一層
*/
public static Map> keyToID(
List> datalist) {
Map> res = new HashMap>();
for (Map map : datalist) {
String id = map.get("ID");
res.put(id, map);
}
return res;
}
/**
* 移除List中所有Map的某個(gè)元素
*
* @param dataByOwnerList
* @param key
* @return
*/
public static List> removeMapElementInList(
List> dataList, String key) {
for (Map map : dataList) {
map.remove(key);
}
return dataList;
}
/**
* 將List類型map的value取出存入List>
*
* @param deallist
* @return
*/
public static List> getForlist(
List> deallist) {
List> list = new ArrayList>();
for (Map map : deallist) {
List list2 = new ArrayList(map.values());
list.add(list2);
}
return list;
}
/**
* 將List類型map的value取出存入List 如果value為null或者空字符串則為null
*
* @param deallist
* @return
*/
public static List getForlistString(
List> deallist) {
List list = new ArrayList();
for (Map map : deallist) {
for (Map.Entry string : map.entrySet()) {
String str = string.getValue();
if (str == null || str.length() <= 0) {
str = "null";
}
list.add(str);
}
}
return list;
}
/**
* 將list轉(zhuǎn)化為帶有左右括號(hào)的sql查詢條件語(yǔ)句,如 in (2,3,4)
*
* @author:mazhen
*/
public static String toSqlConditonalStatement(List list) {
if (null == list || list.size() == 0) {
try {
throw new Exception("因?yàn)閟ql查詢語(yǔ)句不能為空,所以傳入的list不能為空");
} catch (Exception e) {
e.printStackTrace();
}
}
StringBuffer conditionalStatement = new StringBuffer();
conditionalStatement.append("(");
for (String str : list) {
conditionalStatement.append(str);
conditionalStatement.append(",");
}
return conditionalStatement.toString().substring(0,
conditionalStatement.toString().length() - 1)
+ ")";
}
/**
* 判斷List里面有沒有重復(fù)的值
*
* @param list
* @return
*/
public static boolean isOverlap(List list) {
String temp = "";
for (int i = 0; i < list.size() - 1; i++) {
temp = list.get(i);
for (int j = i + 1; j < list.size(); j++) {
if (temp.equals(list.get(j))) {
return false;
// System.out.println("第" + (i + 1) + "個(gè)跟第" + (j + 1)
// + "個(gè)重復(fù),值是:" + temp);
}
}
}
return true;
}
/**
* 處理mapList中key為log_name的value,將value由路徑+名稱修改為僅有名稱
*
* @author : mazhen
*
* @param mapList
* @return
*/
public static List> deletePathOfLogName(
List> mapList) {
List> relist = new ArrayList>();
for (Map map : mapList) {
String pathAndName = map.get("log_name") + "";
String[] pathAndNames = pathAndName.split(";");
String names = "";
for (String str : pathAndNames) {
names += FileUtil.getFileNameFromPath(str) + ";";
}
names = names.substring(0, names.length() - 1);
map.put("log_name", names);
relist.add(map);
}
return relist;
}
/**
* 合并兩個(gè)list
*
* @return
*/
public static List sumList(List a, List b) {
List list = new ArrayList<>();
if (a != null && a.size() > 0) {
for (String string : a) {
list.add(string);
}
}
if (b != null && b.size() > 0) {
for (String string : b) {
list.add(string);
}
}
return list;
}
/**
* 合并兩個(gè)ListMap
*
* @return
*/
public static List> sumListMap(
List> listA, List> listB) {
List> list = new ArrayList>();
if (listA != null && listA.size() > 0) {
for (Map object : listA) {
list.add(object);
}
}
if (listB != null && listB.size() > 0) {
for (Map object : listB) {
list.add(object);
}
}
return list;
}
public static List> objectToString(
List> list) {
List> res = new ArrayList>();
for (Map map : list) {
res.add(MapUtil.objectToString(map));
}
return res;
}
/**
* 將查詢到的結(jié)果轉(zhuǎn)換成以key為鍵值的map,并將key從結(jié)果集中刪掉
* @param map
* @return
*/
public static Map ListToMapKeyId(List> map,String key,String value){
Map map1 = new HashMap();
if(map.isEmpty()){
return null;
}
for(int i = 0; i < map.size();i++){
Map idMap = new HashMap();
idMap = map.get(i);
try{
String id = String.valueOf(idMap.get(key));
if(value!=null&&!value.equals("")){
map1.put(id, idMap.get(value));
}else{
idMap.remove(key);
map1.put(id, idMap);
}
}catch(Exception e){
e.printStackTrace();
}
}
return map1;
}
//public static List> objectToString(
//List> list) {
//List> res = new ArrayList>();
//for (Map map : list) {
//res.add(MapUtil.objectToString(map));
//}
//return res;
//}
public static String ListToStrBySplit(String[] tableNameList, String split){
StringBuffer buf = new StringBuffer();
for(int i = 0; i < tableNameList.length;i++){
buf.append(tableNameList[i]);
if(i!=tableNameList.length-1){
buf.append(split);
}
}
return buf.toString();
}
public static String ListToStrBySplit(Object[] tableNameList, String split){
StringBuffer buf = new StringBuffer();
for(int i = 0; i < tableNameList.length;i++){
buf.append(tableNameList[i]);
if(i!=tableNameList.length-1){
buf.append(split);
}
}
return buf.toString();
}
public static Map ListMapToMap(List> list){
Map map = new HashMap();
if(list !=null && list.size()>0){
for(int i = 0; i < list.size();i++){
Map listMap = list.get(i);
Set sets = listMap.keySet();
for(String set:sets){
map.put((String) listMap.get(set), "Y");
}
}
}
return map;
}
總結(jié)
以上是生活随笔為你收集整理的java listutils_Java的list自定义工具类ListUtils的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux如何批量导出文件格式,Linu
- 下一篇: java 父子级json组装不用递归_2