Card map - how to save internal cards as cards?

My goal is to create a card map so that I can get information about an external card by its key, and then access its "internal" cards by their keys.

However, when I received each internal map, the map I created initially became an object, and I cannot use the key to access its value, as is done with the external map.

To find out from you experts, I would like to know how to save all cards as cards. Or is it even possible?

here is my exercise program:

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapExample {

    public static void main(String[] args) {

        Map<Object,String> mp=new HashMap<Object, String>();

        // adding or set elements in Map by put method key and value pair
        mp.put(new Integer(2), "Two");
        mp.put(new Integer(1), "One");
        mp.put(new Integer(3), "Three");
        mp.put(new Integer(4), "Four");

        Map<Object,String> mp2=new HashMap<Object, String>();
        mp2.put(new Integer(2), "Two2");
        mp2.put(new Integer(1), "One2");
        mp2.put(new Integer(3), "Three2");
        mp2.put(new Integer(4), "Four2");

        Map<Object,String> mpMaps=new HashMap();

        mpMaps.put("Map1",mp);
        mpMaps.put("Map2",mp2);

        System.out.println("This is a map of Maps:   " + mpMaps); 

        for (int i=0;i<mpMaps.size();i++){
                     ArrayList a = new ArrayList(mpMaps.keySet());
                     Object o=a.get(i);
                     System.out.println("all together: " + mpMaps.size() + "each element is:  " + o + " value: " + mpMaps.get(o));
        }             
    }
}

SOLUTIONS:

   Map<Object,Map<Object,String>
    Map<String, Object> mpMaps=new HashMap<String, Object>(); 

from ameer and sleske

+5
source share
6 answers

, , , , <String, Object>, mp , <Object, String>.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;

public class MapExample {

    public static void main(String[] args) {

        Map<Object,String> mp=new HashMap<Object, String>();

        // adding or set elements in Map by put method key and value pair
        mp.put(new Integer(2), "Two");
        mp.put(new Integer(1), "One");
        mp.put(new Integer(3), "Three");
        mp.put(new Integer(4), "Four");

        Map<Object,String> mp2=new HashMap<Object, String>();
        mp2.put(new Integer(2), "Two2");
        mp2.put(new Integer(1), "One2");
        mp2.put(new Integer(3), "Three2");
        mp2.put(new Integer(4), "Four2");

        Map<String, Object> mpMaps=new HashMap<String, Object>();

        mpMaps.put("Map1",mp);
        mpMaps.put("Map2",mp2);

        System.out.println("This is a map of Maps:   " + mpMaps); 

        for (int i=0;i<mpMaps.size();i++){
                     ArrayList<Object> a = new ArrayList<Object>(mpMaps.keySet());
                     Object o=a.get(i);
                     System.out.println("all together: " + mpMaps.size() + "each element is:  " + o + " value: " + mpMaps.get(o));
        }             
    }
}
+8

.

:

Map<Object,String> mpMaps=new HashMap();
mpMaps.put("Map1",mp);

, (mp) , Strings.

Map<Object,Map<Object,String>, .

+5

Commons MultiKey . . http://commons.apache.org/collections/apidocs/ org.apache.commons.collections.keyvalue.MultiKey

+3
Map<Object,String> mpMaps=new HashMap();

mpMaps.put("Map1",mp);

: mp Map, String.

, , , /.

0

HashMap

    HashMap<String, HashMap<String, Object>> outerMap = new HashMap<String, HashMap<String, Object>>();

    HashMap<String, Object> innerMap1 = new HashMap<String, Object>();
    HashMap<String, Object> innerMap2 = new HashMap<String, Object>();


    innerMap1.put("Name", "az");
    innerMap1.put("Address", "7 lab");
    innerMap1.put("Age", 40);

    innerMap2.put("Name", "sab");
    innerMap2.put("Address", "bk3");
    innerMap2.put("Age", 35);

    outerMap.put("1",innerMap1);
    outerMap.put("2", innerMap2);
    System.out.println("outerMap = " + outerMap);

:

outerMap = {1={Address=7 lab, Age=40, Name=az}, 2={Address=bk3, Age=35, Name=sab}}
0

Example

.

-3

All Articles