I'm using Java 8 for grouping by data. But the results obtained are not in the order formed.
Map<GroupingKey, List<Object>> groupedResult = null; if (!CollectionUtils.isEmpty(groupByColumns)) { Map<String, Object> mapArr[] = new LinkedHashMap[mapList.size()]; if (!CollectionUtils.isEmpty(mapList)) { int count = 0; for (LinkedHashMap<String, Object> map : mapList) { mapArr[count++] = map; } } Stream<Map<String, Object>> people = Stream.of(mapArr); groupedResult = people .collect(Collectors.groupingBy(p -> new GroupingKey(p, groupByColumns), Collectors.mapping((Map<String, Object> p) -> p, toList()))); public static class GroupingKey public GroupingKey(Map<String, Object> map, List<String> cols) { keys = new ArrayList<>(); for (String col : cols) { keys.add(map.get(col)); } } // Add appropriate isEqual() ... you IDE should generate this @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final GroupingKey other = (GroupingKey) obj; if (!Objects.equals(this.keys, other.keys)) { return false; } return true; } @Override public int hashCode() { int hash = 7; hash = 37 * hash + Objects.hashCode(this.keys); return hash; } @Override public String toString() { return keys + ""; } public ArrayList<Object> getKeys() { return keys; } public void setKeys(ArrayList<Object> keys) { this.keys = keys; } } Here I am using my class GroupingKey by which I'm dynamically passing from UX. How can get this groupByColumns in sorted form?