Tôi đang sử dụng Java 8 để nhóm theo dữ liệu. Nhưng kết quả thu được không theo thứ tự hình thành.Java 8 không duy trì thứ tự trong khi nhóm
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;
}
}
Ở đây tôi đang sử dụng nhóm lớp học của mìnhTừ đó tôi tự động chuyển từ ux. Làm thế nào có thể nhận được groupByColumns này ở dạng được sắp xếp?
Thanx Holger u làm nhiệm vụ của tôi trở nên đơn giản hơn. Nhưng tôi vẫn có vấn đề rằng nếu tôi muốn áp dụng phân loại trong này chỉ mở rộng groupedColumns này sau đó wat tôi nên làm gì? – shreya