Just as an answer to your comment: I just tried it and there is no exception at all:
Map map = new LinkedHashMap<>(); map.put("AREA_DS_ID", "1,5,9,13,17,21,25,29,33"); map.put("PROJECTS_ID", "13,78,267,18,28,33,55,99"); map.put("SIGNAL_NAME", "a"); map.put("ASSESSMENTNAME", "a"); List entryList = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (Entry entry : map.entrySet()) < String value = entry.getKey() + '=' + entry.getValue(); entryList.add(value); sb.append(value); >String[] entries = entryList.toArray(new String[entryList.size()]); String mapAsString = sb.toString();
Источник
How to convert a Map> to Map> using forEach operation
I am wondering how can I do it using forEach in Java 8. Also, with the forEach lambda, will it be any better with the code performance?
3 Answers 3
will it be any better with the code performance?
No, it'll not. Iterative solutions are usually more performant.
how can I do it using forEach
There's a special operation collect() in the Stream API that is meant to populate a mutable container (e.g. Collection , StringBuilder , etc.) with the contents of the stream pipeline. Usage of forEach() for that purpose is highly discouraged by the documentation. Consider utilizing forEach() only as a last resort, when there's no other way to achieve that.
To do that with collect() , first, you need to create a stream of entries.
Based on each entry, a new entry has to be created, map() operation is utilized for that purpose. Static method Map.entry() is used to instantiate a new entry.
And then apply the terminal operation collect() by passing Collectors.toMap() as parameter, which creates a collector (object responsible for placing the stream elements into a mutable container, a map in this case) based on the two provided functions (for keys and values).
public static void main(String[] args) < Map> mapWithList = Map.of("1", List.of("1", "2", "3")); Map> result = mapWithList.entrySet().stream() .map(entry -> Map.entry(entry.getKey(), new HashSet<>(entry.getValue()))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.println(result); >
Источник
How to add Map into Map> in java
You can't. Probably best to create a custom object that holds both your patient list and the date, and use that instead of the map. You could declare your map to be of type Map but that approach is a bit questionable.
4 Answers 4
You are trying to place a string where a map expects a List.
patientMap.put("PATIENTLIST", patientList); patientMap.put("LASTKEY", date);
patientMap.put(date, patientList);
With a map where date string is key and list of patient is value you can quickly get a list of patients for a given date.
If you want to use a map to hold a date and list of objects in string form, then you would have to convert back those strings back to their original date or list of patient objects.
If this is really what you want I suggest you look into java object serialization and deserialization.
That looks like a signature or a serialized version of an object that has 2 Properties: LASTKEY and PATIENTLIST. Similar to what Robby Cornelissen suggested in the other answer.
Are you sure the method you provided is the place where you expect an object you described? Why is your method signature like public Map> ?
Here are the small changes which you can do to achieve above (Definitely you have to change the return type):
public Map getPatients(String sendingApplication,String sendingFacility) < // TODO Auto-generated method stub MappatientMap = null; List patientList = null; patientMap = new HashMap(); patientList = new ArrayList(); try < PatientInfoDAO patientInfoDAO = new PatientInfoDAOImpl(); ItemCollectionitems = patientInfoDAO.getPatients(sendingApplication, sendingFacility); for(Item item : items) < PatientInfo patient = new PatientInfo(); patient.setAdministrativeSex("Male"); patientList.add(patient); >String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); PatientMapObject pmo = new PatientMapObject(); pmo.setPatientList(patientList); pmo.setPDate(date); patientMap.put("PATIENTLIST", pmo); return patientMap; >catch(Exception ase) < throw new RuntimeException("internalServerError"); >> class PatientMapObject < private ListpatientList; private String pdate; public void setPDate(String pdate) < this.pdate = pdate; >public void setPatientList(List patientList) < this.patientList = patientList; >//getters >
You can only add the defined type of object in Map 's definition (i.e List because you are creating map like Map ) to add String also you should use Map
Источник