The list contains Arrays of (String,String,BigDecimal).
I want to convert the List to Map<String, Map<String, BigDecimal>>
grouped by the first String using java 8 Stream class.
List<Object[]> list = ss.createCriteria(PayrollDeduction.class)
.createAlias("payroll", "pay")
.add(Restrictions.le("pay.paymentDate", atDate))
.createAlias("payroll.employee", "employee")
.add(Restrictions.in("employee.id", Arrays.asList(empId)))
.setProjection(Projections.projectionList()
.add(Projections.property("gouvAgencyCode"))
.add(Projections.property("code"))
.add(Projections.sum("amount"))
.add(Projections.groupProperty("gouvAgencyCode"))
.add(Projections.groupProperty("code")))
.setReadOnly(true)
.list();
list.stream().collect(Collectors.groupingBy(r->r[0], Collectors.mapping(mapper, downstream)));
Do it like this:
List<Object[]> list = Arrays.asList(
new Object[] { "A", "X", new BigDecimal("1") },
new Object[] { "A", "Y", new BigDecimal("2.0") },
new Object[] { "B", "X", new BigDecimal("3.00") },
new Object[] { "C", "Z", new BigDecimal("4.000") }
);
Map<String, Map<String, BigDecimal>> map = list.stream()
.collect(Collectors.groupingBy(r -> (String) r[0],
Collectors.toMap(r -> (String) r[1],
r -> (BigDecimal) r[2])));
System.out.println(map);
Output
{A={X=1, Y=2.0}, B={X=3.00}, C={Z=4.000}}
To me, it looks like a XY Problem:
You seem to be using Hibernate;
You want to transform the results from your projection into some other format.
Hibernate's own ResultTransformer could have solved your problem in a cleaner way.
Related
Given a org.bson.Document
{
"doneDate":"",
"todoEstimates":"",
"forecastDate":"",
"cardType":{
"projectData":[
{
"color":"#ffcd03",
"boardId":"30022"
},
{
"color":"#ffcd03",
"boardId":"1559427"
}
],
"cardFields":[
{
"fieldName":"id",
"fieldLabel":"Unique ID",
"fieldType":"Integer",
"itemType":"Long",
"isRequired":"NO",
"isReadOnly":"Yes",
"isDisabled":"NO",
"inputMethod":"System Generated",
"defaultValue":null,
"isUserType":"No"
},
{
"fieldName":"name",
"fieldLabel":"Title",
"fieldType":"Single-Line Text",
"itemType":"String",
"isRequired":"Yes",
"isReadOnly":"NO",
"isDisabled":"NO",
"inputMethod":"Manual Entry",
"defaultValue":null,
"isUserType":"No"
}
]
}
How do I extract the values of fieldName and fieldLabel via streams into the following?
{
"id": "Unique ID",
"name:" "Title",
...
}
I tried the following but I get stuck at the part where I get value of the cardFields list.
document.entrySet().stream().filter(e -> e.getKey().equals("cardType"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
.entrySet().stream().filter(e -> e.getKey().equals("cardFields"))
.map(e -> (Map)e.getValue()).toList();
Here is a working solution with streams:
Map<String, Object> fields = ((List<Map<String, Object>>) ((Map<String, Object>) document.entrySet()
.stream()
.filter(entry -> entry.getKey().equals("cardType"))
.findFirst()
.orElseThrow(() -> new RuntimeException("card type not found"))
.getValue())
.entrySet()
.stream()
.filter(entry -> entry.getKey().equals("cardFields"))
.findFirst()
.orElseThrow(() -> new RuntimeException("card fields not found"))
.getValue())
.stream()
.collect(Collectors.toMap(el -> el.get("fieldName").toString(), element -> element.get("fieldLabel")));
Document result = new Document(fields);
System.out.println(result.toJson());
That's probably the worst code i have written - absolutely unreadable and you can't debug it. I would suggest that you do not use stream for this particular task, it isn't the right tool for it. So here is another working solution using Map.get(key):
Map<String, Object> cardType = (Map<String, Object>) document.get("cardType");
List<Map<String, Object>> cardFields = (List<Map<String, Object>>) cardType.get("cardFields");
Document result = new Document();
cardFields.forEach(cardField -> result.put((String) cardField.get("fieldName"), cardField.get("fieldLabel")));
System.out.println(result.toJson());
This is shorter, readable, you can debug it if needed and probably it's more performant. I'd say it's much better overall.
You may be able to parse your document like this:
Document cardType = document.get("cardType", Document.class);
final Class<? extends List> listOfMaps = new ArrayList<Map<String, String>>().getClass();
List<Map<String, String>> fields = cardType.get("cardFields", listOfMaps);
fields.stream().map(f -> {
System.out.println(f.get("fieldName") + ": " + f.get("fieldLabel"));
// here you can construct your new object
}).collect(Collectors.toList());
If you don't mind casting a lot, you could try following:
List cardFields = (List) ((Map) document.get("cardType")).get("cardFields");
Map<String, String> map = (Map) cardFields.stream()
.collect(Collectors.toMap(cf -> ((Document) cf).getString("fieldName"),
cv -> ((Document) cv).getString("fieldLabel")));
System.out.println(map);
or you can emit omit the casting with the following:
List<Document> carFields = document.get("cardType", Document.class)
.getList("cardFields", Document.class);
Map<String, String> map = carFields.stream()
.collect(Collectors.toMap(k -> k.getString("fieldName"),
v -> v.getString("fieldLabel")));
System.out.println(map);
Here is the complete example running with java 17:
import org.bson.Document;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Bson {
private String json=
"""
{
"doneDate": "",
"todoEstimates": "",
"forecastDate": "",
"cardType": {
"projectData": [
{
"color": "#ffcd03",
"boardId": "30022"
},
{
"color": "#ffcd03",
"boardId": "1559427"
}
],
"cardFields": [
{
"fieldName": "id",
"fieldLabel": "Unique ID",
"fieldType": "Integer",
"itemType": "Long",
"isRequired": "NO",
"isReadOnly": "Yes",
"isDisabled": "NO",
"inputMethod": "System Generated",
"defaultValue": null,
"isUserType": "No"
},
{
"fieldName": "name",
"fieldLabel": "Title",
"fieldType": "Single-Line Text",
"itemType": "String",
"isRequired": "Yes",
"isReadOnly": "NO",
"isDisabled": "NO",
"inputMethod": "Manual Entry",
"defaultValue": null,
"isUserType": "No"
}
]
}
}
""";
public static void main(String[] args) {
Bson bson = new Bson();
Document document = Document.parse(bson.json);
List cardType = (List) ((Map) document.get("cardType")).get("cardFields");
Map<String, String> map = (Map) cardType.stream()
.collect(Collectors.toMap(cf -> ((Document) cf).getString("fieldName"),
cv -> ((Document) cv).getString("fieldLabel")));
System.out.println(map);
List<Document> carFields = document.get("cardType", Document.class).getList("cardFields", Document.class);
Map<String, String> map1 = carFields.stream()
.collect(Collectors.toMap(k -> k.getString("fieldName"), v -> v.getString("fieldLabel")));
System.out.println(map1);
}
}
I have a question regarding Java streams. I built below maps.
One map includes country name as key and list of cities as value.
Another includes Continent name as key and list of countries as value.
List<String> inCities = new ArrayList<String>(Arrays.asList("Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai"));
List<String> jpCities = new ArrayList<String>(Arrays.asList("Tokyo", "Osaka", "Kyoto"));
List<String> usCities = new ArrayList<String>(Arrays.asList("Dallas", "Chicago", "NewYork"));
List<String> ukCities = new ArrayList<String>(Arrays.asList("London", "Cardiff", "Oxford"));
List<String> frCities = new ArrayList<String>(Arrays.asList("Paris", "Marseille", "Lyon"));
Map<String, List<String>> countryWiseCities = new HashMap<String, List<String>>();
countryWiseCities.put("India", inCities);
countryWiseCities.put("Japan", jpCities);
countryWiseCities.put("USA", usCities);
countryWiseCities.put("UK", ukCities);
countryWiseCities.put("France", frCities);
List<String> asiaCountries = new ArrayList<String>(Arrays.asList("India", "Japan"));
List<String> northAmericaCountries = new ArrayList<String>(Arrays.asList("USA", "Canada"));
List<String> europeCountries = new ArrayList<String>(Arrays.asList("UK", "France"));
Map<String, List<String>> continentWiseCountries = new HashMap<String, List<String>>();
continentWiseCountries.put("Asia", asiaCountries);
continentWiseCountries.put("NorthAmerica", northAmericaCountries);
continentWiseCountries.put("Europe", europeCountries);
Existing maps.
{"India":["Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai"], "Japan":["Tokyo", "Osaka", "Kyoto"], "USA":["Dallas", "Chicago", "NewYork"], "Canada":["Ontario", "Toronto", "vancouver"],
"UK":["London", "Cardiff", "Oxford"], "France":["Paris", "Marseille", "Lyon"]}
{"Asia":["India", "Japan"], "NorthAmerica":["USA", "Canada"], "Europe":["UK", "France"]}
I am looking for a way to build below map using java streams. What is the simplest way to achieve below output using Java streams?.
Expected output:
{"ASIA":["Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai", "Tokyo", "Osaka", "Kyoto"],
"NorthAmerica":["Dallas", "Chicago", "NewYork", "Ontario", "Toronto", "vancouver"],
"Europe":["London", "Cardiff", "Oxford", "Paris", "Marseille", "Lyon"]
Here key is continent name.
Value is list cities in countries in a particular continent.
Below is the code which I tried.
Map<String, List<Map.Entry<String, List<String>>>> collect1 = countryWiseCities.entrySet().stream()
.collect(Collectors.groupingBy(Map.Entry::getKey));
Here's one thing you can do:
Map<String, List<String>> continentCities = continentWiseCountries.entrySet()
.stream()
.map(continent -> new AbstractMap.SimpleEntry<>(
continent.getKey(),
continent.getValue()
.stream()
.flatMap(country ->
countryWiseCities.getOrDefault(country, Collections.emptyList())
.stream())
.collect(Collectors.toList())))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
All it's doing is using an intermediate stream to join from continents to cities (through countries), then collecting cities and continent pairs.
Note that getOrDefault(country, Collections.emptyList()) is needed because you have countries in your continent list that do not exist in your country-list mapping.
The output of the above code is something like this:
{
"Asia" : [ "Delhi", "Mumbai", "Hyderabad", "Banglore", "Chennai", "Tokyo", "Osaka", "Kyoto" ],
"Europe" : [ "London", "Cardiff", "Oxford", "Paris", "Marseille", "Lyon" ],
"NorthAmerica" : [ "Dallas", "Chicago", "NewYork" ]
}
Here a solution without the intermediate stream, just using the flatMap in the value-function of the toMap-collector:
Map<String, List<String>> continentCityMap = continentWiseCountries.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey,
e -> e.getValue().stream().flatMap(
country -> countryWiseCities.getOrDefault(country, Collections.emptyList()).stream())
.collect(Collectors.toList())));
I have list having fields Name, Currency and Amount.
I want to group it by Name, Currency and Sum of Amount.
List<myPojo> rows= ................
public class myPojo
{
private String name;
private String currency;
private BigDecimal amount;
......................
}
I want result/output in list
You can use Stream API.
Use Collectors.toMap and use AbstractMap.SimpleEntry as key of map. Then define merge function for multiple values of the same key.
List<myPojo> res = new ArrayList<>(rows.stream()
.collect(Collectors.toMap(
e -> new AbstractMap.SimpleEntry<>(e.getName(), e.getCurrency()),
Function.identity(),
(a, b) -> new myPojo(a.getName(), a.getCurrency(), a.getAmount().add(b.getAmount()))))
.values());
Demo:
List<myPojo> list = new ArrayList<>();
list.add(new myPojo("A", "USD", new BigDecimal(1.0)));
list.add(new myPojo("A", "USD", new BigDecimal(2.0)));
list.add(new myPojo("A", "USD", new BigDecimal(3.0)));
list.add(new myPojo("B", "USD", new BigDecimal(1.0)));
list.add(new myPojo("B", "USD", new BigDecimal(2.0)));
list.add(new myPojo("B", "USD", new BigDecimal(3.0)));
list.add(new myPojo("A", "US", new BigDecimal(1.0)));
list.add(new myPojo("A", "US", new BigDecimal(2.0)));
list.add(new myPojo("A", "US", new BigDecimal(3.0)));
List<myPojo> res = new ArrayList<>(list.stream()
.collect(Collectors.toMap(
e -> new AbstractMap.SimpleEntry<>(e.getName(), e.getCurrency()),
Function.identity(),
(a, b) -> new myPojo(a.getName(), a.getCurrency(), a.getAmount().add(b.getAmount()))))
.values());
System.out.println(res.toString());
Output:
[myPojo [name=B, currency=USD, amount=6],
myPojo [name=A, currency=USD, amount=6],
myPojo [name=A, currency=US, amount=6]]
Note: Try to capitalize the name of the class like MyPojo for better convention
You could use groupingBy to generate a Map<String,Map<String,BigDecimal>> corresponding to Map<Name,Map<Currency,sum of Amount>>
List<myPojo> rows = List.of(new myPojo("ABCD", "USD", new BigDecimal(20)),
new myPojo("XYZ", "GBP", new BigDecimal(60)),
new myPojo("XYZ", "THB", new BigDecimal(35)),
new myPojo("ABCD", "INR", new BigDecimal(90)),
new myPojo("ABCD", "USD", new BigDecimal(80)),
new myPojo("XYZ", "THB", new BigDecimal(45)));
Map<String,Map<String,BigDecimal>> map =
rows.stream()
.collect(Collectors.groupingBy(myPojo::getName,
Collectors.groupingBy(myPojo::getCurrency,
Collectors.reducing(BigDecimal.ZERO, myPojo::getAmount, BigDecimal::add))));
System.out.println(map);
// output: {XYZ={GBP=60, THB=80}, ABCD={USD=100, INR=90}}
I have a List of Map that i want to group it by the key nom using java streams.
[
{
"dateDebut": "2018-07-01T00:00:00.000+0000",
"nom": "Julien Mannone",
"etat": "Impayé"
},
{
"dateDebut": "2018-08-01T00:00:00.000+0000",
"nom": "Julien Mannone",
"etat": "Impayé"
},
{
"dateDebut": "2018-10-01T00:00:00.000+0000",
"nom": "Mathiew Matic",
"etat": "payé"
},
{
"dateDebut": "2018-10-01T00:00:00.000+0000",
"nom": "Ash Moon",
"etat": "payé"
}
]
so i want as a result something like this
{
"Julien Mannone":[
{
"dateDebut":"2018-07-01T00:00:00.000+0000",
"etat":"Impayé"
},
{
"dateDebut":"2018-08-01T00:00:00.000+0000",
"etat":"Impayé"
}
],
"Mathiew Matic":[
{
"dateDebut":"2018-10-01T00:00:00.000+0000",
"etat":"payé"
}
],
"Ash Moon":[
{
"dateDebut":"2018-10-01T00:00:00.000+0000",
"etat":"payé"
}
]
}
As a beginner in using streams I have made some research I found some codes like that
Map<String, List<Map>> afterFormatting =
beforeFormatting.stream()
.flatMap(m -> m.entrySet().stream())
.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())));
but that doesn't do the job for me
Seems like you're simply looking for:
Map<String, List<Map<String, String>>> afterFormatting =
beforeFormatting.stream()
.collect(Collectors.groupingBy(map -> map.get("nom")));
or if you don't want each Map<String, String> in the result set to contain the "nom" entry then you can do as follows:
Map<String, List<Map<String, String>>> afterFormatting =
beforeFormatting.stream()
.collect(Collectors.groupingBy(map -> map.get("nom"),
Collectors.mapping(map -> {
Map<String, String> temp = new HashMap<>(map);
temp.remove("nom");
return temp;
}, Collectors.toList())));
If I understand you correct you have a maps like
{
"dateDebut": "2018-07-01T00:00:00.000+0000",
"nom": "Julien Mannone",
"etat": "Impayé"
},
but then, when you call
.flatMap(m -> m.entrySet().stream())
You get a stream of all entry sets for all maps.
But actually, your maps are not maps itself. They are rather POJO objects.
I suggest creating a class like
class Value {
String dateDebut, String nom, Etring etat;
// constructor
}
then convert each map to this class:
beforeFormatting.stream()
.map(m-> new Value(m.get("dateDebut"), m.get("nom"),m.get("etat"))
So now, you have a stream<Value> and you can easily group by in by "nom"
.collect(groupingBy(Value::getNom)
You just need to map stream to change the format to needed, and then collect:
list.stream().map(it -> {
Map<String, Map<String, String>> newMap = new HashMap<>();
String nom = it.get("nom");
it.remove("nom");
newMap.put(nom, it);
return newMap;
}
).collect(Collectors.toList())
Testable code:
Map<String, String> m = new HashMap<>();
m.put("dateDebut", "2018-07-01T00:00:00.000+0000");
m.put("nom", "Julien Mannone");
m.put("etat", "Impayé");
Map<String, String> m2 = new HashMap<>();
m2.put("dateDebut", "2018-10-01T00:00:00.000+0000");
m2.put("nom", "Mathiew Matic");
m2.put("etat", "payé");
Map<String, String> m3 = new HashMap<>();
m3.put("dateDebut", "2018-07-01T00:00:00.000+0000");
m3.put("nom", "Ash Moon");
m3.put("etat", "payé");
List<Map<String, String>> list = new ArrayList<>();
list.add(m);
list.add(m2);
list.add(m3);
List<Map<String, Map<String, String>>> res = list.stream().map(it -> {
Map<String, Map<String, String>> newMap = new HashMap<>();
String nom = it.get("nom");
it.remove("nom");
newMap.put(nom, it);
return newMap;
}
).collect(Collectors.toList());
System.out.println(res);
I am using GSON to serialize Java object.
I have a Java class with following properties.
String property1;
Map<String, HashMap> property2 = new HashMap<>();
Map<String, ArrayList<String>> property3 = new HashMap<>();
Map<String, String[]> property4 = new HashMap<>();
I want to convert this to Json. Because of the maps with HashMaps inside, it has become difficult. I know I can get Json of a map with gsonObject.toJson(map). But I want all these properties in the Json Object. (all in one. not Concatenating many objects)
Can anyone help me to get this done?
I don't see what the problem is. Gson can serialize Maps just fine.
Assuming your class is named Test
Test test = new Test();
test.property1 = "some value";
HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("fourty two", 42);
test.property2.put("property2-key", map);
ArrayList<String> strings = new ArrayList<>(Arrays.asList("string1",
"string2", "string3"));
test.property3.put("property3-key", strings);
String[] stringArray = { "array1", "array2", "array3" };
test.property4.put("property4-key", stringArray);
Gson gson = new Gson();
String json = gson.toJson(test);
System.out.println(json);
It generates the following
{
"property1": "some value",
"property2": {
"property2-key": {
"fourty two": 42,
"one": 1
}
},
"property3": {
"property3-key": [
"string1",
"string2",
"string3"
]
},
"property4": {
"property4-key": [
"array1",
"array2",
"array3"
]
}
}