当前位置: 代码迷 >> J2SE >> java操作指定list有关问题
  详细解决方案

java操作指定list有关问题

热度:141   发布时间:2016-04-24 02:12:24.0
java操作指定list问题
List list = new ArrayList();
list 存放了以下数据
id name parentId url iconUrl oid type
53 设备登记管理 16 /dev/a NULL 0 2
53 设备登记管理 16 /dev/a NULL 1 2
53 设备登记管理 16 /dev/a NULL 2 2
53 设备登记管理 16 /dev/a NULL 3 2
53 设备登记管理 16 /dev/a NULL 4 2
53 设备登记管理 16 /dev/a NULL 5 2
54 设备监控管理 16 /dev/b NULL 0 2
54 设备监控管理 16 /dev/b NULL 4 2
55 设备异常管理 16 /dev/c NULL 0 2
55 设备异常管理 16 /dev/c NULL 3 2
55 设备异常管理 16 /dev/c NULL 4 2
80 ffff 16 NULL NULL 0 1

java 如何用算法实现类似mysql的groupconcat的分组,结果如下,并存放在另一个list里
53 设备登记管理 16 /dev/a NULL 0,1,2,3,4,5 2
54 设备监控管理 16 /dev/b NULL 0,4 2
55 设备异常管理 16 /dev/c NULL 0,3,4 2
80 ffff 16 NULL NULL 0 1

求实现方法!!!! 


------解决方案--------------------
自己写个方法
for example
Java code
import java.util.*;public class GroupTest {    public static void main(String[] args) throws Throwable {        List<String[]> list = new ArrayList<String[]>();        list.add(new String[] {"53", "设备登记管理", "16", "/dev/a", null, "0", "2"});        list.add(new String[] {"53", "设备登记管理", "16", "/dev/a", null, "1", "2"});        list.add(new String[] {"53", "设备登记管理", "16", "/dev/a", null, "2", "2"});        list.add(new String[] {"54", "设备登记管理", "16", "/dev/b", null, "0", "2"});        list.add(new String[] {"54", "设备登记管理", "16", "/dev/b", null, "4", "2"});        list.add(new String[] {"55", "设备登记管理", "16", "/dev/c", null, "3", "2"});        list.add(new String[] {"55", "设备登记管理", "16", "/dev/c", null, "4", "2"});        list.add(new String[] {"80", "ffff", "16", null, null, "0", "1"});        List<String[]> result = group(list, new int[]{0, 1, 2, 3, 4, 6}, new int[] {5});        for (String[] record : result) {            System.out.println(Arrays.toString(record));        }    }        @SuppressWarnings("unchecked")    public static <T extends Comparable<T>> List<T[]> group(List<T[]> list, int[] groupId, int[] unionId) {        List<T[]> result = new ArrayList<T[]>();        for (T[] record : list) {            boolean found = false;            for (T[] union : result) {                boolean same = true;                for (int id : groupId) {                    same = same && (union[id] == record[id] ||                                    (union[id] != null && record[id] != null &&                                     union[id].compareTo(record[id]) == 0));                }                if (same) {                    for (int id : unionId) {                        if (union[id] instanceof String) {                            union[id] += "," + record[id];                        } else {                            //other union operation                        }                    }                    found = true;                    break;                }            }            if(!found) {                result.add(Arrays.copyOf(record, record.length));            }        }        return result;    }}
  相关解决方案