This question already has answers here:
How to initialize an array in Java?
(11 answers)
Closed 6 years ago.
I have a Map
Map<String,String[]> data = HashMap<String,String[]>();
and want to put value in it. like
"key":["value1","value2"]
I try it but get error:
data.put("key",["value1","value2"]);//syntax error
You could try using the Array initializer for fixed size string arrays like following
Map<String, String[]> data = new HashMap<>();
data.put("key", new String[]{"val1", "val2"});
This should definitely work
Related
This question already has answers here:
Adding a value to a list to an already existing key in Map
(3 answers)
Closed 4 years ago.
How to add an Element to an ArrayList within a HashMap?
This is a question, that I have asked myself many times and forgot it after solving it. I guess many have the same one so here is the simple answer to it.
// Example
HashMap<String, ArrayList<String>> someElements = new HashMap<String, ArrayList<String>>();
// fill the HashMap with the key that you need
// initiate it with an empty ArrayList
someElements.put("keyString" , new ArrayList<String>());
// Later when wanting to add an element to the ArrayList of a key use
someElements.get("keyString").add("TheStringValue");
This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)
This question already has answers here:
Converting JSON data to Java object
(14 answers)
Closed 7 years ago.
I have one map file
Map<String,Object> personData = procReadData.execute(in);
The data is coming from this is
{CUR_GENERIC=[{PROPOSAL_NUMBER=1, TITLE=test proposal, LEAD_UNIT_NAME=University, FULL_NAME=test, STSTUS_CODE=Pending, DOCUMENT_TAKEN_BY=user1 qa, UPDATE_TIMESTAMP=2015-12-28 00:00:00.0, UPDATE_USER=test}]}
How to get PROPOSAL_NUMBER from that result.
You should check what type the result of
personData.get("CUR_GENERIC");
is (use a debugger for this if you have no documentation available). It's probably a list or an array of Map<String, Object> depending on what tool was used to convert the JSON data to a Java map. Get the first entry of this list or array and then use get("PROPOSAL_NUMBER") to retrieve the entry you want.
This question already has answers here:
What does it mean to "program to an interface"?
(33 answers)
Closed 7 years ago.
I didn't understand the difference in the two below statement :-
HashMap<Integer,String> hashmap = new HashMap<Integer,String>();
Map<Integer,String> hashmap = new HashMap<Integer,String>();
The above two statement work in the same manner , no difference in the ouptput
OR
there is any time difference on running two statement.
Map<> is the interface which HashMap<> implements. See here for more info.
This question already has answers here:
How do I convert a Map to List in Java?
(13 answers)
Closed 9 years ago.
I would be pleasure if you can suggest.
Could you suggest how properlly I can retriev all values from hashmap.
In such simplee example:
Map<String, Values> someMap = ....;
List<Values> valuesFromMap = (List<Values>) someMap.values();
After that I've got that, Java couldn't cast to java.util.List
Thank you in advanced.
The values method returns a Collection, not a List. Use
Collection<Values> valuesFromMap = someMap.values();
someMap.values() returns Collection. If you need to convert into List than do that as follows:
List<Values> valuesFromMap = new ArrayList<>(someMap.values());