ZelelB
ZelelB

Reputation: 2000

Map nasted in collection.. Getting the values() Set of a Map 2D

(Redefinition of the method

public Collection<String> values();

)

I have a Map contained in another Map, so its something like that:

Map<String,Map<String,String>> centralMap

(the intern map is subjectGradeMap) I want now to use this method:

public Collection<String> values()

to get a collection with all the values of the map. I tried:

Collection<String> returncoll = centralMap.values().values();

but it didn't work. Tried this too:

Collection<Map<String,String>> collec = centralMap.values();
Collection<String> returncollection = collec.values(); 

but in vain :-S

That problem was solved thanks! Now i would like to ask you if you have an idea, how should I implement the Iterator method?

/**
* Returns an Iterator, that pass through all the entries of the map. If
* something changes in the map, when it is being passed through all its'
* entries by the Iterator, we can't determinate the behaviour that will
* happen..
*
* @return An Iterator that pass through all entries. Every entry will be
*         returned as String-Tripel with its' three Elements row, column
*         and value.
*/
@Override
public Iterator<Entry> iterator() {
return null;
}

Do you have an idea?

the Entry class is the following one, (implemented in the interface that I use to create my objects of TrueStringMap2D:

final class Entry
    {       
       /** First Key. */
        private final String key1;

       /** Second Key. */
        private final String key2;

       /** Value. */
        private final String value;

       /** Ctor for a tripel.
        * @param key1 1st key.
        * @param key2 2nd key.
        * @param value Value.
        */
        public Entry(final String key1, final String key2, final String value)
        {
            this.key1 = key1;
            this.key2 = key2;
            this.value = value;
        }

        public String getFirstKey()
        {
            return key1;
        }

        public String getSecondKey()
        {
            return key2;
        }

        public String getValue()
        {
                return value;
        }

        @Override public boolean equals(final Object anything)
        {
            if(anything == null)
                return false;
            if(getClass() != anything.getClass())
                return false;
            final Entry that = (Entry)anything;
            return Objects.equals(getFirstKey(), that.getFirstKey())
                   && Objects.equals(getSecondKey(), that.getSecondKey())
                   && Objects.equals(getValue(), that.getValue());
        }

        // CHECKSTYLE- Magic Number
        @Override public int hashCode()
        {
            int hash = 7;
            hash = 17 * hash + Objects.hashCode(getFirstKey());
            hash = 17 * hash + Objects.hashCode(getSecondKey());
            hash = 17 * hash + Objects.hashCode(getValue());
            return hash;
        }
        // CHECKSTYLE+ Magic Number

        @Override public String toString()
        {
            return String.format("(%s, %s, %s)", getFirstKey(), getSecondKey(), getValue());
            }

    }

Thankyou for your help!

Upvotes: 0

Views: 784

Answers (2)

You can use MultiKeyMap (from Commons-Collections by Apache):

MultiKeyMap<String, String> map;
map.put("Key1", "Key2", "Value");
map.put("Key1", "Key3", "Value2");

for (Entry<MultiKey<String>, String> entry : map.entrySet()) {
    // TODO something
}

It's found in two versions:

Upvotes: 0

maksimov
maksimov

Reputation: 5811

centralMap.values() returns you a Collection, Collection doesn't have values(). centralMap.values() essentially returns a list of Maps. So in order to assess each of those maps, you will need to iterate:

for (Map map : cetralMap.values()) {
    Collection values = map.values();
    // do something with your values here
}

To build a collection of all values from all the maps contained in centralMap:

List myGrandList = new ArrayList();
for (Map map : centralMap.values()) {
    myGrandList.addAll(map.values());
}
return myGrandList;

Upvotes: 1

Related Questions