-5

I have a List and A is defined below.

How do i add in a Map with Key as Long and values as List of Strings.

Class A
{
Long in;
List<String> out;
}
Map<Long,List<String>>
user1125829
  • 179
  • 3
  • 4
  • 13

2 Answers2

1

Create a Hashmap object, with key Long and value List. Add items with put(key,value) and retrieve them with get

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {    
    public static void main(String[] args) {
        Map<Long,List<String>> myMap=new HashMap<Long,List<String>>();
        List<String> myList=new ArrayList<String>();
        myList.add("abc");
        myList.add("xyz");
        myMap.put(new Long(1), myList);
        for(int i=0;i<myList.size();i++)
            System.out.println(myMap.get(new Long(1)).get(i));
    }
}
MihaiC
  • 1,618
  • 1
  • 10
  • 14
0

1.) Create HashMap with Key as Long and value as List<String>.

2.) Use put method of HashMap, as below.

 public static void main(String[] args) {
        Map<Long, List<String>> myMap = new HashMap<Long, List<String>>();
        myMap.put(101L, new ArrayList<String>());
    }
Pena Pintada
  • 137
  • 11
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116