Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am unable to refactor and make this snippet cleaner and smaller.
For example i want to add a new value to the listOfValues in one line, instead of first initliazing it to an empty arraylist then calling add on it on a seperate line and finally putting listofvalues in the hasmap on the third line. Also in the else statement is the hasMap.put necessary since we are getting a reference to listOfValues and if we add something to it hashMap will contain it automatically?
MyObject:
key: String
values: List<Value>
Value:
Value1: String
Value2: String
HashMap<String, MyObject> hashMap = new HashMap<>();
List<Value> listOfvalues;
if (!hashMap.containsKey(key)) {
listOfvalues = new ArrayList<>();
listOfvalues.add(Value.builder().withValue1(Value1).withValue2(Value2).build());
hashMap.put(key, MyObject.builder().withkey(key).withvalues(listOfvalues).build());
} else {
listOfvalues = hashMap.get(key).getvalues();
log.info(String.format("Duplicate key: %s. Previous Value(s): %s", key,
listOfvalues));
listOfvalues.add(Value.builder().withValue1(Value1).withValue2(Value2).build());
hashMap.put(key, MyObject.builder().withkey(key).withvalues(listOfvalues).build());
}
The common part with list and map working can be moved out from if/else:
Map<String, MyObject> hashMap = new HashMap<>();
List<Value> listOfvalues;
if (!hashMap.containsKey(key)) {
listOfvalues = new ArrayList<>();
} else {
listOfvalues = hashMap.get(key).getvalues();
log.info(String.format("Duplicate key: %s. Previous Value(s): %s", key, listOfvalues));
}
listOfvalues.add(Value.builder().withValue1(Value1).withValue2(Value2).build());
hashMap.put(key, MyObject.builder().withkey(key).withvalues(listOfvalues).build());
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 days ago.
Improve this question
I am trying to get this code to print out both the "hello" and the "there" from the dictionary. Currently it only prints out the "there". Is there a way to print out both of these?
import java.lang.*;
import java.util.*;
import java.io.*;
public class Task
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
Dictionary dictionary = new Hashtable();
dictionary.put("hello", "there");
I tried to just do
System.out.println(i.nextElement());
I thought it would print out the entire dictionary entry
but all that did was print out the "there"
You can get the first element of the entrySet from the Map using its Iterator. However, you should use LinkedHashMap which maintains insertion order, if you want the result to be meaningful.
Map<String, String> map = new LinkedHashMap<>();
map.put("hello", "there");
System.out.println(map.entrySet().iterator().next());
You have not shown how you are iterating the Dictionary. I would change from that to a Map, and you should not use raw-types. Once you have a Map you can iterate the entrySet to get both keys and values. Like,
Map<String, String> dictionary = new HashMap<>();
dictionary.put("hello", "there");
for (Map.Entry<String, String> i : dictionary.entrySet()) {
System.out.printf("key = %s, value = %s%n", i.getKey(), i.getValue());
}
Which outputs
key = hello, value = there
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My list looks something like this
List<Map<CustomClass,Integer>> sampleList = new ArrayList<>();
Here each custom class is associated with a value where the class is taken as key and the value associated to it is the value for the map. I can have more than one 1 key to have same value.
For example:
List<Map<CustomClass,Integer>> sampleList = new ArrayList<>();
CustomClass a1 = new CustomClass();
CustomClass a2 = new CustomClass();
CustomClass b1 = new CustomClass();
CustomClass b2 = new CustomClass();
Map<CustomClass, Integer> map1 = new HashMap();
map1.put(a1,3);
map1.put(a2,3);
Map<CustomClass, Integer> map2 = new HashMap();
map2.put(b1,2);
map2.put(b2,2);
sampleList.add(map1);
sampleList.add(map2);
Now I want the final sorted list to be having {b1,b2,a1,a2} i.e sorted based on integer value.
You can use streams to flatten the maps and sort by value:
List<CustomClass> result = sampleList.stream()
.map(Map::entrySet)
.flatMap(Set::stream)
.sorted(Entry.comparingByValue())
.map(Entry::getKey)
.collect(Collectors.toList());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a problem with imput some values in a hashmap,
the HashMap<String, ArrayList<Integer>> must contain the name and the relative points.I'm not able to put more values for a key.
The input file is the following.
Marco,Matteo,Luca,Giovanni;34,35,3,35
Marco,Anna,Chiara;1,5,6
A map doesn't accept duplicate keys.
You could switch to using either a list of maps (One map per row):
List<Map<String, Integer>> records = ...
Map<String, Integer> map1 = new HashMap<>(),
map2 = new HashMap<>();
map1.put("Marco", 34);
map1.put("Matteo",35);
//put values for Luca,Giovanni...
records.add(map1);
//create and add maps for other rows
OR a map of lists/arrays (one list per column)
Map<String, List<Integer>> map1 = new HashMap<>()
List<Integer> marco = ...
marco.add(34);
marco.add(1);
map1.put("Marco", marco);
//do the same for other records...
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Am new to java i have formed a set of result in
`Map<String, Map<String, List<String>>>`
now i want to get the keys and values of each set
How should get this. anyone please suggest me
thanks in advance.
You will want to look at the documentation for Maps.
myArbitrarilyNamedMap = new Map<String, Map<String, List<String>>>();
//do stuff so that myArbitrarilyNamedMap contains values
Set firstLevelKeys = myArbitrarilyNamedMap.keySet(); //this bit
For me is much easier to study with examples. I can give you a little example. May be it will be usefull.
public class MapHierarchy {
public static void main(String[] args) {
// preparation
Map<String, Map<String, List<String>>> myTestMap = new HashMap<>();
ArrayList<String> listOfValues = new ArrayList<>();
listOfValues.add("Hello");
listOfValues.add("my");
listOfValues.add("little");
listOfValues.add("friend");
HashMap<String, List<String>> innerMap = new HashMap<>();
innerMap.putIfAbsent("innerMapKey", listOfValues);
myTestMap.put("outerKey", innerMap);
// where the magic happens
System.out.println("Keys of outer map: " + myTestMap.keySet().toString());
for (Map.Entry<String, List<String>> innerMapItem : innerMap.entrySet()) {
String innerMapItemKey = innerMapItem.getKey();
System.out.println("Key of inner map: " + innerMapItemKey);
System.out.println("Values of inner map: " + innerMapItem.getValue().toString());
}
}}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I like to convert a map that contains a string and a list of object into a list.
Map<String, List<Object>>
How can I do that?
Thank you in advance!
Assuming you want a list of all the objects in the map values this would be a way to do it:
List<Object> all = new LinkedList<>();
for (Collection<Object> l : map.values()) {
all.addAll(l);
}
Map<String, List<Object>> myMap = new HashMap<String, List<Object>>();
// Assuming map containing a key with "Apple"
// This is how you get the list indicated by key Apple
List<Object> myObjects = myMap.get("Apple");
You can convert list of Object into your DTO List as follows.
ArrayList<DTOtoMAP> mylist = (ArrayList<DTOtoMAP>) (Object) hashMap.get(0);