/**
     * 将二维数据进行x轴与y轴互换
     * 
     * @param list
     * @return
     */
    public static <T> List<List<T>> rowToCol(List<List<T>> list) {

        if (Utils.isEmpty(list)) {
            return list;
        }

        List<List<T>> toList = new ArrayList<List<T>>();

        Map<Integer, List<T>> map = new HashMap<Integer, List<T>>();

        for (int i = 0; i < list.size(); i++) {
            List<T> row = list.get(i);

            for (int j = 0; j < row.size(); j++) {
                T cell = row.get(j);

                List<T> col = map.get(j);
                if (col == null) {
                    col = new ArrayList<T>();
                }
                col.add(cell);
                map.put(j, col);
            }
        }

        List<Integer> sort = new ArrayList<Integer>(map.keySet());

        Collections.sort(sort);

        for (Integer key : sort) {
            toList.add(map.get(key));
        }

        return toList;
    }

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐