-1

I have an input that is given below. How can I insert these inputs in a map, HashMap preferrably.

{1,"abc","a"} {200,"xyz","b"} {1,"ab","c"} {12,"fgh","d"}

Integer being the key and remaining are values.

shyam karwa
  • 213
  • 1
  • 3
  • 8
  • 1
    What is your mapping suppose to look like? What is key and what is value? Is your mapping ` -> `? Or maybe it is mapping of ` -> `? If you don't have mapping there maybe consider using Set instead of Map? – Pshemo Apr 09 '15 at 11:13
  • That depends on how the fields a related to each other. Can one of them (or a combination of several of them) be considered as the key? – Eran Apr 09 '15 at 11:13
  • 2
    Your question is poorly specified. Maps have a key/value relationship. Edit your post to indicate what should be the key and what should be the value. –  Apr 09 '15 at 11:16
  • Integer being the key and remaining are string values – shyam karwa Apr 09 '15 at 11:17
  • @Pshemo but how can I do that.... – shyam karwa Apr 09 '15 at 11:18
  • "but how can I do that...." It depends on what you want to achieve. Lets take a look at key `1`? At start it contains `"abc","a"` value, but then you try to set it to `"ab","c"`. What should happen here? Should `"abc","a"` be replaced with `"ab","c"`? Or maybe you want to store both `"abc","a"` and `"ab","c"`? – Pshemo Apr 09 '15 at 11:20
  • In my program replacing "abc","a" with "ab","c" is required after execution of some code – shyam karwa Apr 09 '15 at 11:22
  • In that case probably simplest way would be creating your own class like `Pair` which will store data like `"ab","c"` or `"abc","a"` and simply place new `Pair` with data you want it to have. So your map can look like `Map` or even `Map>` if you would like to make Pair generic class. – Pshemo Apr 09 '15 at 11:26

2 Answers2

1

Well, that doesn't fit to HashMap idea. You can use one of following:

mtyurt
  • 3,369
  • 6
  • 38
  • 57
1

add integer value in hasmap as key and other 2 values in list and then add this list to hashmap as key value

Map<String, String> map = new HashMap<Interger, Object>();
List<String> maplist1=new ArrayList<String>
            maplist1.add("abc");
            maplist1.add("a");
            map.put(1, maplist1)
           // otherwise clear maplist
           List<String> maplist2=new ArrayList<String>
            maplist2.add("xyz");
            maplist2.add("b");
            map.put(200, maplist2)
Sameer Kazi
  • 17,129
  • 2
  • 34
  • 46