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的某个元素

*

* @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转化为带有左右括号的sql查询条件语句,如 in (2,3,4)

*

* @author:mazhen

*/

public static String toSqlConditonalStatement(List list) {

if (null == list || list.size() == 0) {

try {

throw new Exception("因为sql查询语句不能为空,所以传入的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里面有没有重复的值

*

* @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) + "个跟第" + (j + 1)

// + "个重复,值是:" + 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;

}

/**

* 合并两个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;

}

/**

* 合并两个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;

}

/**

* 将查询到的结果转换成以key为键值的map,并将key从结果集中删掉

* @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;

}


版权声明:本文为weixin_39576133原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。