6
को सूची मैं की तरहजावा 8 नेस्टेड नक्शा
class A {
private Integer keyA;
private Integer keyB;
private String text;
}
कक्षा A
की एक सूची hava मैं keyA
और keyB
तो मैं नीचे दिए गए कोड को बनाने के द्वारा नेस्ट Map
मैप किया गया करने के लिए aList
स्थानांतरित करना चाहते हैं।
Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream()
.collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> {
Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>();
result.entrySet().stream().forEach(e -> {nestedMap.put(e.getKey(), e.getValue().stream().collect(Collectors.groupingBy(A::getKeyB)));});
return nestedMap;}));
लेकिन मुझे यह कोड पसंद नहीं है।
मुझे लगता है कि अगर मैं flatMap
का उपयोग करता हूं, तो मैं इससे बेहतर कोड कर सकता हूं।
लेकिन मुझे नहीं पता कि इस व्यवहार के लिए flatMap
का उपयोग कैसे करें।