Madhu
Madhu

Reputation: 590

How to Retrieve the User Defined Objects from Collection of Collections Object

In one of my requirment i have a key and each key associated with multiple document objects these i am storing in to HashMap with key and value pair .Key is the id and values is the List of documents associated with that kEY

        Ex : HashMap<String,List<Document>> 

        I want to put all the list documents in one collection object , ie.List<Documents> (all documents of all keys)
        When i am using values() method of Hashmap i am getting Collection<List<Document>>

        If i want to get all the document objects i have to get each List<Document> ,iterate and add it into new collection object.

        other than this Is there any best way i can get all the Documents one at a time in collection Object.
        using any apache common-collections or apache commons-collections4 api ?


        ArrayList<Document> al = new ArrayList<Document>();
        Document dto1 = new Document();
        dto1.setResearchId("1");
        dto1.setIsinCode("isinCode1");
        dto1.setEquity("equity1");

        Document dto2 = new Document();
        dto2.setResearchId("2");
        dto2.setIsinCode("isinCode2");
        dto2.setEquity("equity2");

        Document dto3 = new Document();
        dto3.setResearchId("3");
        dto3.setIsinCode("isinCode3");
        dto3.setEquity("equity3");

        Document dto4 = new Document();
        dto4.setResearchId("4");
        dto4.setIsinCode("isinCode4");
        dto4.setEquity("equity4");

        al.add(dto1);
        al.add(dto2);
        al.add(dto3);
        al.add(dto4);

        Map<String ,List<Document>> mapList = 
                 new HashMap<String,List<Document>>();
        mapList.put("1", al);
        mapList.put("2", al);
        mapList.put("3", al);


      Excepted output : Collection<Document>

       For sample i have added the same arraylist object in to my Map 
       but in actual i will have different arrayList objects.

Upvotes: 2

Views: 766

Answers (2)

Mureinik
Mureinik

Reputation: 312136

Seems like you're trying to flatten the Lists in the Map's values to a single collection. Java 8 allows you do this pretty easily:

List<Document> flatDocuments = // could also be defined as a Collection<Document>
    mapList.values()
           .stream()
           .flatMap(Collection::stream)
           .collect(Collectors.toList());

Alternatively, if you just want to do something with them (such as printing them), you can skip the collecting phase and operate on them directly using forEach:

mapList.values()
       .stream()
       .flatMap(Collection::stream)
       .forEach(System.out::println);

EDIT:
For older Java versions you'd have to implement the same logic yourself using loops (or, of course, use some third party that does it for you):

List<Document> flatDocuments = new LinkedList<>();
for (List<Document> list : mapList.values()) {
    flatDocuments.addAll(list);
}

Upvotes: 1

Andreas
Andreas

Reputation: 159185

Since you're suggesting apache commons-collections4 yourself, did you actually look at it?

The values() method of the MultiValuedMap does exactly what you want.

Collection<V> values()

Gets a Collection view of all values contained in this multi-valued map.

Implementations typically return a collection containing the combination of values from all keys.

Upvotes: 0

Related Questions