I have below list, i want to convert list to map.
SurveyAllocationUsers user1 = new SurveyAllocationUsers();
user1.setSurveyorId("1");
user1.setSurveyorTypeCode("LSR");
SurveyAllocationUsers user2 = new SurveyAllocationUsers();
user2.setSurveyorId("1");
user2.setSurveyorTypeCode("SR");
SurveyAllocationUsers user3 = new SurveyAllocationUsers();
user3.setSurveyorId("2");
user3.setSurveyorTypeCode("LSR");
SurveyAllocationUsers user4 = new SurveyAllocationUsers();
user4.setSurveyorId("2");
user4.setSurveyorTypeCode("SR");
SurveyAllocationUsers user5 = new SurveyAllocationUsers();
user5.setSurveyorId("2");
user5.setSurveyorTypeCode("BG");
List<SurveyAllocationUsers> list = new ArrayList<SurveyAllocationUsers>();
list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);
want to convert list to map like below.
Map<String,List<String>> usersMap = new HashMap<String, List<String>>();
map key will be SurveyorId and values will be correspondiing List of SurveyorTypeCode.
Thanks for the help in advance!!!
It should be something like below
userMap.put(user1.getSurveyorId(), new ArrayList<>().add(user1.getSurveyorTypeCode));
userMap.put(user2.getSurveyorId(), new ArrayList<>().add(user2.getSurveyorTypeCode));
It should be something like this:
List<SurveyAllocationUsers> list = new ArrayList<SurveyAllocationUsers>();
list.add(user1);list.add(user2);list.add(user3);list.add(user4);list.add(user5);
Map<String,List<String>> usersMap = new HashMap<String, List<String>>();
for(SurveyAllocationUsers sau: list){
if(!userMap.contains(sau.getSurveyorId())){
userMap.put(sau.getSurveyorId(), new ArrayList<>().add(sau.getSurveyorTypeCode));
}else{
userMap.get(sau.getSurveyorId()).add(sau.getSurveyorTypeCode);
}
}
Note: Code in not complied.
A little bit ugly but solves your problem:
Map<String, List<String>> map = list.stream().collect(toMap(SurveyAllocationUsers::getSurveyorId, p -> new ArrayList(singletonList(p.getSurveyorTypeCode())), (s, a) -> {
s.addAll(a);
return s;
}));
You can try this:
Map<String, List<String>> usersMap = new HashMap<String, List<String>>();
for (SurveyAllocationUsers user : list) {
List<String> typeCodes = new ArrayList<>();
typeCodes.add(user.getSurveyorTypeCode());
usersMap.put(user.getSurveyorId(), typeCodes);
}
Thank you everyone for your reply, I got the solution as below.
List <String>newUserList = null;
Map<String,List<String>> usersMap = new HashMap<String, List<String>>();
for(SurveyAllocationUsers sau: list){
if(usersMap.containsKey(sau.getSurveyorId())){
List <String>myList = usersMap.get(sau.getSurveyorId());
myList.add(sau.getSurveyorTypeCode());
usersMap.put(sau.getSurveyorId(), myList);
}else{
if(sau.getSurveyorId() != null){
newUserList = new ArrayList<String>();
newUserList.add(sau.getSurveyorTypeCode());
usersMap.put(sau.getSurveyorId(), newUserList);
}
}
}
Answer as expected
1 [LSR, SR]
2 [LSR, SR, BG]
It should be like this using Java8
resMap = list.stream().collect(Collectors.groupingBy(SurveyAllocationUsers::getSurveyorId,
Collectors.mapping(SurveyAllocationUsers::getSurveyorTypeCode,Collectors.toList())));
Output: {1=[LSR, SR], 2=[LSR, SR, BG]}
Related
public static HashMap<String,Integer> users1 = new HashMap<>();
public static HashMap<String,Integer> users2 = new HashMap<>();
public static HashMap<String,Integer> users3 = new HashMap<>();
public static HashMap<String,Integer> users4 = new HashMap<>();
// Enter code here.
I need to add them into a arrayList or set or something and then they should be called when using that data structures index. Like is there any way to solve it
Or just simply use an array:
public static HashMap<String,Integer>[] users = HashMap<>[4];
users[0] = = new HashMap<>();
users[1] = = new HashMap<>();
users[2] = = new HashMap<>();
users[3] = = new HashMap<>();
And then use them:
users[0].add( "String", 123 );
you could as simply as :
List<HashMap<String, Integer>> list = new ArrayList<>();
// And then add them to list
list.add(users1);
list.add(users2);
...
ArrayList<HashMap> list = new ArrayList<>();
list.add(users1);
I am trying to set a hashmap to have key as string and a list array as value. Is it possible? and how do I set the list into the value?
HashMap<String, List<String>> foo = new HashMap<String, List<String>>();
foo.put("key1",{"key1_value1","key1_value2"});
You can do the following
Map<String, List<String>> foo = new HashMap<String, List<String>>();
List<String> list = new ArrayList<String>();
list.add("key1_value1");
list.add("key1_value2");
foo.put("key1",list);
foo.put("key", Arrays.asList("key1_val1", "key1_val2"));
You have to use a data structure like ArrayList or just an array maybe to represent the list of strings as value.
You can use the following with a List;
foo.put("key", Arrays.asList("key1_val1", "key1_val2"));
where foo is of type Map<String, List<String>>
Or you the following with an array;
foo.put("key", new String[]{"key1_val1", "key1_val2"});
where foo is of type Map<String, String[]>
Map<String, List<String>> foo = new HashMap<String, List<String>>();
List<String> list = new ArrayList<String>();
list.add("value1");
list.add("value2");
foo.put("key1", list);
for (Entry<String, List<String>> entry : foo.entrySet()) {
String key = entry.getKey();
List<String> valueList = entry.getValue();
System.out.println("key = " + key);
for (String value : valueList) {
System.out.println("value = " + value);
}
}
Output
key = key1
value = value1
value = value2
I am wondering if there was a way to merge keys together based on same values in the Hashmap.
Ex.
A->2
B->1
C->2
The result I am looking for is:
2 - A, C
1 - B
One of the ways is to have your map defined like this:
Map<String,List<String>> multiValueMap = new HashMap();
Or if you don't want to reinvent the wheel, then use Apache MultiMap
You will have to change the data structure to store data in this way. Use a HashMap of ArrayList or Set (as appropriate for you) to do this.
Example
HashMap<Character, Integer> map = new HashMap<>();
map.put('A', 2);
map.put('B', 1);
map.put('c', 2);
// SORTED
HashMap<Integer, List<Character>> sortedMap = new HashMap<>();
ArrayList<Character> list = new ArrayList<>();
for(Map.Entry<Character, Integer> entry : map.entrySet()){
list = sortedMap.get(entry.getValue());
if(list == null){
list = new ArrayList<>();
entry.put(entry.getValue(), list);
}
list.add(entry.getKey());
}
HashMap Reversal
HashMap<String,Integer> aHM = new HashMap<String,Integer>();
HashMap<Integer,ArrayList<String>> aHMrev = new HashMap<Integer,ArrayList<String>>();
//HashMap Reversal
for (String f : aHM.keySet()){
if(aHMrev.get(aHM.get(f)) != null){
ArrayList<String> al = aHMrev.get(aHM.get(f));
al.add(f);
aHMrev.put(aHM.get(f), al);
}
else{
ArrayList<String> al = new ArrayList<String>();
al.add(f);
aHMrev.put(aHM.get(f), al);
}
}
Example
aHM = [LMWEB1B-VMH.log=Group1,PLMWEB1E-VMH.csv=Group3,
LMWEB1A-VMH.log=Group1,
LMWEB1E-VMH.log=Group2,
PLMWEB1D-VMH.csv=Group3,
LMWEB1F-VMH.log=Group2,
PLMWEB1C-VMH.csv=Group3,
PLMWEB1A-VMH.csv=Group2,
PLMWEB1B-VMH.csv=Group3,
PLMDB1-VMH.csv=Group2,
LMWEB1D-VMH.log=Group1,
PLMWEB1F-VMH.csv=Group3,
LMWEB1C-VMH.log=Group1]
aHMrev = {
Group1=[LMWEB1B-VMH.log, LMWEB1A-VMH.log, LMWEB1D-VMH.log,LMWEB1C-VMH.log],
Group2=[LMWEB1E-VMH.log, LMWEB1F-VMH.log,PLMWEB1A-VMH.csv,PLMDB1-VMH.csv],
Group3=[D:\Trash\Logs\Perfmon\PLMWEB1E-VMH.csv,PLMWEB1D-VMH.csv,PLMWEB1C-VMH.csv,PLMWEB1B-VMH.csv,PLMWEB1F-VMH.csv]}
You just create HaspMap like below:
Map<Integer, List<String>> map1 = new HashMap<>();
ArrayList<String> al = new ArrayList<>();
al.add("B");
map1.put(1, al);
al = new ArrayList<>();
al.add("A");
al.add("C");
map1.put(2, al);
I am trying to map a meterId to a list of MeterBlinks that have that Id. I'm mainly confused on how to build the list up for the HashMap.put() call. Code below:
Map<String, List<MeterBlink>> IdToMetersMap = new HashMap<>();
for (MeterBlink meterBlink : createData()) {
List<MeterBlink> meterBlinkList = new ArrayList<>();
meterBlinkList.add(meterBlink);
String meterId = meterBlink.getMeterId();
idToMetersMap.put(meterId, meterBlinkList)
}
I think the issue is that I am creating a new list each time I iterate through but I am not sure how to resolve this.
Use the computeIfAbsent method added in jre 8:
Map<String, List<MeterBlink>> idToMetersMap = new HashMap<>();
for (MeterBlink meterBlink : createData()) {
String meterId = meterBlink.getMeterId();
idToMetersMap.computeIfAbsent(meterId, k -> new ArrayList<>()).add(meterBlinks);
}
Another option in java 8:
Map<String, List<MeterBlink>> idToMetersMap = createData().stream()
.collect(Collectors.groupingBy(MeterBlink::getMeterId));
I like the java8 answer, but here without java8 (without lambda expressions):
Map<String, List<MeterBlink>> idToMetersMap = new HashMap<>();
for (MeterBlink meterBlink : createData()) {
String meterId = meterBlink.getMeterId();
List<MeterBlink> meterBlinkList = idToMetersMap.get(meterId);
//if List doesn't exist create it and put in Map
if (meterBlinkList == null) {
meterBlinkList = new ArrayList<>();
idToMetersMap.put(meterId, meterBlinksList)
}
meterBlinkList.add(meterBlink);
}
I have a map with "task(s)": ["epochdate1", "epochdate2"] how can I reverse this map?
For example
task_1 => [date1, date2, date3, date5]
task_2 => [date4, date5]
task_3 => [date2, date3, date5]
task_4 => [date4, date5]
when reversed it will be
date1 => [task_1]
date2 => [task_1, task_3]
date3 => [task_1, task_3]
date4 => [task_2, task_4]
date5 => [task_1, task_2, task_3, task_4]
Code
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, ArrayList<String>> myMap = new HashMap<String, ArrayList<String>>();
ArrayList<String> t1List = new ArrayList<String>();
t1List.add("date1");
t1List.add("date2");
t1List.add("date3");
t1List.add("date5");
ArrayList<String> t2List = new ArrayList<String>();
t2List.add("date4");
t2List.add("date5");
ArrayList<String> t3List = new ArrayList<String>();
t3List.add("date2");
t3List.add("date3");
t3List.add("date5");
ArrayList<String> t4List = new ArrayList<String>();
t4List.add("date4");
t4List.add("date5");
myMap.put("task_1", t1List);
myMap.put("task_2", t2List);
myMap.put("task_3", t3List);
myMap.put("task_4", t4List);
Map<String, ArrayList<String>> reversedMap = Test.getReversedMap(myMap);
}
Just implement it:
Map<String, ArrayList<String>> getReversedMap(Map<String, ArrayList<String>> myMap){
Map<String, ArrayList<String>> result = new HashMap<>();
for(String key : myMap.keySet()){
for(String val : myMap.get(key)){
if(!result.containsKey(val)){
result.put(val, new ArrayList());
}
result.get(val).add(key);
}
}
return result;
}
There are no short cuts. You have to iterate over the entrySet of the original map, and for each date value you find, put a new entry in the new map with the matching task as the first value, or add the task to the list of an existing entry if the date is already in the new map.
I would use Guava for this. Try something like:
Map<K, V> map = ...;
ListMultimap<V, K> inverse = Multimaps.invertFrom(Multimaps.forMap(map),
ArrayListMultimap.<V,K>create());