Create map from Java stream? - java-8

I am trying to create a map from a Java stream. I am able to do this easily with javascript and am trying to create the same thing in Java. Here is my data structure:
var slips = [
{
original: 'Y',
lines: [
{
detailLines: {
detailLineId: 111
}
},
{
detailLines: {
detailLineId: 222
}
}
]
},
{
original: 'N',
lines: [
{
detailLines: {
detailLineId: 333
}
},
{
detailLines: {
detailLineId: 444
}
}
]
}
]
Here is how I did it in javascript
var test = slips.reduce((acc, slip) => {
slip.lines.map(line => line.detailLines.detailLineId)
.map(arr => acc[arr] = slip.original);
return acc;
}, {});
to get my result of
{
'111': 'Y',
'222': 'Y',
'333': 'N',
'444': 'N'
}
How do I do this using the Java 8 Stream api? The slips above really is just a POJO. I converted it to a JSON object to do figure it out in js. The real structure of the Objects are
class Slip {
private Boolean original;
private List<Line> lines;
}
class Line {
private List<DetailLine> detailLines;
}
class DetailLine {
private Long detailLine;
}
So what I have started with the Java is
Map<Long, Boolean> results = slips.stream().reduce(new Map<Long, Boolean>, ...)

For me, it looks like a line object contains a single detail line and not a list.
class Slip {
private Boolean original;
private List<Line> lines;
}
class Line {
private DetailLine detailLine;
}
class DetailLine {
private Long detailLineId;
}
Assuming each detail line id is unique, you might use flatMap to create the necessary mappings id -> Boolean, and simply collects them into a map.
import java.util.AbstractMap.SimpleEntry;
import static java.util.stream.Collectors.toMap;
...
Map<Long, Boolean> results =
slips.stream()
.flatMap(s -> s.getLines().stream().map(l -> new SimpleEntry<>(l.getDetailLine().getDetailLineId(), s.getOriginal())))
.collect(toMap(SimpleEntry::getKey, SimpleEntry::getValue))
If you indeed have the structure you claimed, you should flatMap twice:
.flatMap(s -> s.getLines().stream().flatMap(l -> l.getDetailLine().stream().map(dl -> new SimpleEntry<>(dl.getDetailLineId(), s.getOriginal()))))

Here is a working example
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<DetailLine> detailedLines1 = new ArrayList<DetailLine>();
detailedLines1.add(new DetailLine(111l));
detailedLines1.add(new DetailLine(222l));
List<DetailLine> detailedLines2 = new ArrayList<DetailLine>();
detailedLines2.add(new DetailLine(333l));
detailedLines2.add(new DetailLine(444l));
Line line1 = new Line(detailedLines1);
Line line2 = new Line(detailedLines2);
List<Line> lines1 = new ArrayList<Line>();
lines1.add(line1);
List<Line> lines2 = new ArrayList<Line>();
lines2.add(line2);
List<Slip> slips = new ArrayList<Slip>();
slips.add(new Slip(true, lines1));
slips.add(new Slip(false, lines2));
Map<Long, Boolean> myResult = new HashMap<>();
slips.stream().map(
slip ->
slip.getLines().stream().map(
line -> line.getDetailLines().stream().map(deadLine -> deadLine.getDetailLine()).collect(Collectors.toList())
).flatMap(Collection::stream)
.map(l -> new AbstractMap.SimpleEntry<>(l, slip.getOriginal()))
).flatMap(l -> l).forEach(System.out::println);
}
}
Output
111=true
222=true
333=false
444=false

Related

How to make dependent webclient calls

I need to make 3 dependent WebClient API calls. In the end, I want a Mono of FinalResponse Object.
I need to use the value from the first API response to make a call to the second API (which would return Mono of Purchase class. Purchase class would contain 2 member variables
user object
List
Now for each value in the list, I need to make a third API call. and then return the final mono object to the controller.
I'm currently stuck with how to go about with an asynchronous call to 3rd API for each value in the list(returned by 2nd API)
service.getPurchases returns Mono<Purchase>. service.getSimilarItems returns Mono<List<Item>>.
class Purchase{
private List<Item> purchasedItemsList;
}
class Item {
private int itemId;
private int categoryId;
private String itemName;
}
public Mono<FinalResponse> getEndResults(UserRequest userRequest) {
Mono<User> response1 = service.getUserResponse(userRequest);
return response1.flatMap(response -> {
int userId = response.getUserId();
FinalResponse finalResponse = new FinalResponse();
List<AllItems> itemList = new LinkedList<>();
return service.getPurchase(userRequest, userId)
.map(purchasedItem -> {
val.getPurchasedItemsList().forEach(oneItem -> { // please help me how to go about from here
service.getSimilarItemsInCategory(userRequest, userId, oneItem.getCategoryId)
.map(similarItem -> {
AllItems allItem = new AllItems();
allItem.setPurchasedItem(oneItem);
allItem.setSimilarItem(similarItem);
itemList.add(allItem);
});
});
finalResponse.setResults(itemList);
return finalResponse;
});
});
}
class FinalResponse {
private User user;
private List<AllItems> results;
}
class AllItems {
private Item purchasedItem;
private List<Item> similarItem;
}
Basically the end response I need would look like
{
"users":{//UserObject//},
"results": [
{
"purchasedItem": {// Purschased Item 1},
"similarItems": [
{//Similar Item 1},
{//Similar Item 2}
]
},
{
"purchasedItem": {// Purschased Item 1},
"similarItems": [
{//Similar Item 1},
{//Similar Item 2}
]
}
]
}
Following Toerktumlare's comment: This can be fairly simple, if the WebClient calls return Monos or Fluxes of simple values or lists.
You can use flatMapMany() or flatMapIterable().
What about this simplified example?
public Mono<FinalResponse> getEndResults(UserRequest userRequest) {
Mono<User> userResponse = service.getUserResponse(userRequest);
return userResponse.flatMap(response -> {
int userId = response.getUserId();
return service.getPurchase(userRequest, userId)
.map(Purchase::getPurchasedItemsList)
.flatMapIterable(purchasedItems -> purchasedItems)
.flatMap(oneItem -> getSimilarItemInCategory(userRequest, userId, oneItem))
.collectList();
})
.map(itemList -> {
FinalResponse finalResponse = new FinalResponse();
finalResponse.setResults(itemList);
return finalResponse;
});
}
public Mono<AllItems> getSimilarItemInCategory(UserRequest userRequest, int userId, Item oneItem) {
return service.getSimilarItemsInCategory(userRequest, userId, oneItem.getCategoryId())
.map(similarItem -> {
AllItems allItem = new AllItems();
allItem.setPurchasedItem(oneItem);
allItem.setSimilarItem(similarItem);
return allItem;
});
}

Dealing with one field that is sometimes boolean and sometimes int

I'm trying to work with the reddit JSON API. There are post data objects that contain a field called edited which may contain a boolean false if the post hasn't been edited, or a timestamp int if the post was edited.
Sometimes a boolean:
{
"edited": false,
"title": "Title 1"
}
Sometimes an int:
{
"edited": 1234567890,
"title": "Title 2"
}
When trying to parse the JSON where the POJO has the field set to int, I get an error: JsonDataException: Expected an int but was BOOLEAN...
How can I deal with this using Moshi?
I also ran into a similar problem where I had fields that were sometimes booleans, and sometimes ints. I wanted them to always be ints. Here's how I solved it with Moshi and kotlin:
Make a new annotation that you will use on fields to should convert from boolean to int
#JsonQualifier
#Retention(AnnotationRetention.RUNTIME)
#Target(AnnotationTarget.FIELD, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION)
annotation class ForceToInt
internal class ForceToIntJsonAdapter {
#ToJson
fun toJson(#ForceToInt i: Int): Int {
return i
}
#FromJson
#ForceToInt
fun fromJson(reader: JsonReader): Int {
return when (reader.peek()) {
JsonReader.Token.NUMBER -> reader.nextInt()
JsonReader.Token.BOOLEAN -> if (reader.nextBoolean()) 1 else 0
else -> {
reader.skipValue() // or throw
0
}
}
}
}
Use this annotation on the fields that you want to force to int:
#JsonClass(generateAdapter = true)
data class Discovery(
#Json(name = "id") val id: String = -1,
#ForceToInt #Json(name = "thanked") val thanked: Int = 0
)
The easy way might be to make your Java edited field an Object type.
The better way for performance, error catching, and appliaction usage is to use a custom JsonAdapter.
Example (edit as needed):
public final class Foo {
public final boolean edited;
public final int editedNumber;
public final String title;
public static final Object JSON_ADAPTER = new Object() {
final JsonReader.Options options = JsonReader.Options.of("edited", "title");
#FromJson Foo fromJson(JsonReader reader) throws IOException {
reader.beginObject();
boolean edited = true;
int editedNumber = -1;
String title = "";
while (reader.hasNext()) {
switch (reader.selectName(options)) {
case 0:
if (reader.peek() == JsonReader.Token.BOOLEAN) {
edited = reader.nextBoolean();
} else {
editedNumber = reader.nextInt();
}
break;
case 1:
title = reader.nextString();
break;
case -1:
reader.nextName();
reader.skipValue();
default:
throw new AssertionError();
}
}
reader.endObject();
return new Foo(edited, editedNumber, title);
}
#ToJson void toJson(JsonWriter writer, Foo value) throws IOException {
writer.beginObject();
writer.name("edited");
if (value.edited) {
writer.value(value.editedNumber);
} else {
writer.value(false);
}
writer.name("title");
writer.value(value.title);
writer.endObject();
}
};
Foo(boolean edited, int editedNumber, String title) {
this.edited = edited;
this.editedNumber = editedNumber;
this.title = title;
}
}
Don't forget to register the adapter on your Moshi instance.
Moshi moshi = new Moshi.Builder().add(Foo.JSON_ADAPTER).build();
JsonAdapter<Foo> fooAdapter = moshi.adapter(Foo.class);

Spring Boot send only changed data

I am building a game in Spring Boot on a server and classic Javascript on a backend.
Right now I have this:
...
#Autowired
private SimpMessagingTemplate template;
...
#Scheduled(fixedRate = 1000 / Constants.FPS)
public void renderClients() {
for(Game g : games) {
template.convertAndSend("/game/render/" + g.getId(), g);
}
}
...
Basically I have a multiple Games running and I send each with it's id to the client.
However the data I am sending (or the most of the data) is static (not changing)...
What if I want not to send the whole data but only parts which have changed.
Btw the response JSON looks like this:
{"id":"862b1dd8-48d5-4562-802a-7d669a5a5ed5","players":[{"id":"da8dcbec-7028-4a39-9547-a4e2dc321c3c","name":"John Doe","position":{"x":100.0,"y":100.0},"rotation":0.0,"hero":{"maxHealth":1300.0,"movementSpeed":4.5,"attackDamage":32.75,"width":68,"height":71,"heroName":"drowRanger","radius":34.0},"stats":{"kills":0,"lastHits":0},"lastClick":null}],"duration":380107.12}
and the only thing that is changing is duration and sometimes the x and y when the player moves...
Is it even possible?
Could I write some middleware that will do that at the time the objects are converted to JSON?
Maintain a data structure stores your changed value, and attach it to your Game Object.
When the time to send ,convert the map to a json ,and clear it.
Using this way may use more memory than before , but won't cost much time.
I DID IT!!
In my GameController I do:
#Scheduled(fixedRate = 1000 / Constants.FPS)
public void renderClients() throws Exception {
for(Game g : games) {
template.convertAndSend("/game/render/" + g.getId(), g.formatToSend());
}
}
Notice the g.formatToSend() method
here is how a Game class looks like:
public class Game {
private BandWidthOptimizer optimizer = new BandWidthOptimizer();
...
...
public String formatToSend() throws Exception {
return optimizer.optimize(this);
}
}
And Here Comes THE BandWidthOptimizer:
package com.iddqd.doto.optimization;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.tools.classfile.Opcode;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.util.Iterator;
import java.util.Set;
import java.util.function.BiConsumer;
public class BandWidthOptimizer {
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
private String[] preserveKeys;
public BandWidthOptimizer() {
this.preserveKeys = new String[0];
}
public BandWidthOptimizer(String[] preserveKeys) {
this.preserveKeys = preserveKeys;
}
public String optimize(Object obj) throws Exception {
String json = mapper.writeValueAsString(obj);
Object nobj = parser.parse(json);
Object oobj = parser.parse(lastJSON);
JSONObject newJsonObj = (JSONObject)nobj;
JSONObject oldJsonObj = (JSONObject)oobj;
JSONObject res = getJSONObjectDiff(newJsonObj, oldJsonObj);
lastJSON = json;
return res.toJSONString();
}
private JSONObject getJSONObjectDiff(JSONObject obj1, JSONObject obj2) {
JSONObject res = new JSONObject();
Set set = obj1.keySet();
for (Object key : set) {
// If doesn't exist put it in the diff
if (!obj2.containsKey(key)) {
res.put(key, obj1.get(key));
} else {
// Get the values from both objects
Object val1 = obj1.get(key);
Object val2 = obj2.get(key);
// If their instances are of the same type
if(val1 == null) {
continue;
}
if(val2 == null) {
res.put(key, val1);
continue;
}
if (val1.getClass().equals(val2.getClass())) {
// If they are JSONObject
if (val1 instanceof JSONObject) {
// Recursively parse JSONObject with all of it's properties
JSONObject nested = getJSONObjectDiff((JSONObject) obj1.get(key), (JSONObject) obj2.get(key));
// If it contains any keys
if(nested.keySet().size() > 0) {
// Store the diff into final diff
res.put(key, nested);
}
// If they are JSONArrays
} else if (val1 instanceof JSONArray) {
// If val1 contains some values (is not empty)
if(((JSONArray) val1).size() > 0) {
// Get their diff
JSONArray arr = getJSONArrayDiff((JSONArray) val1, (JSONArray) val2);
// If array is not empty
if (arr.size() > 0) {
// put it into the diff
res.put(key, arr);
}
}
// If they are just a pure values
} else {
// Compare them - If they're not equal
if(!val1.equals(val2)) {
// put the val1 into diff
res.put(key, val1);
}
}
} else {
res.put(key, val1);
}
}
}
return res;
}
private JSONArray getJSONArrayDiff(JSONArray arr1, JSONArray arr2) {
JSONArray res = new JSONArray();
// For every element
for(int i = 0; i < arr1.size(); i++) {
Object val1 = arr1.get(i);
// If i is out of arr2 bounds
if(i > arr2.size()) {
// put the arr1 item into the diff
res.add(val1);
}
Object val2 = arr2.get(i);
if(val1 == null) {
continue;
}
if(val2 == null) {
res.add(val1);
continue;
}
// If their types are equal
if(val1.getClass().equals(val2.getClass())) {
// If they are JSONObjects
if(val1 instanceof JSONObject) {
// Get their diff
JSONObject obj = getJSONObjectDiff((JSONObject) val1, (JSONObject) val2);
// If it contains any keys
if(obj.keySet().size() > 0) {
// Store the diff into final diff
res.add(obj);
}
// If they are JSONArrays
} else if (val1 instanceof JSONArray) {
// Get their diff
JSONArray arr = getJSONArrayDiff((JSONArray) val1, (JSONArray) val2);
// If array is not empty
if(arr.size() > 0) {
// put it into the diff
res.add(arr);
}
// If they are just a pure values
} else {
// Compare them - If they're not equal
if(val1 != val2) {
// add the val1 into diff
res.add(val1);
}
}
} else {
res.add(val1);
}
}
return res;
}
}
This is it, now if nothing moves on the map the result JSON looks like this:
{"duration":282964.56}
because only the duration changes
But when my Player moves on the map see what happens:
{"duration":386676.06,"players":[{"position":{"x":556.5914801003707,"y":153.55964799554002}}]}
TODO
I have to implement a preserveKeys functionallity because I always want to send some keys like id and so on...

java 8 groupingBy

I have a List of Student and each Student may register for a couple of subjects.
Therefore each Student will have a List of Subject. I would like to do a groupingBy on Subject using java 8 features.
I am not able to figure out a way. Any help will be appreciated.
This is just an example where groupBy is used. You can find many other examples on web. [This example is taken from here]
Group items by price: Collectors.groupingBy and Collectors.mapping example
public static void main(String[] args) {
//3 apple, 2 banana, others 1
List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal("9.99")),
new Item("banana", 20, new BigDecimal("19.99")),
new Item("orang", 10, new BigDecimal("29.99")),
new Item("watermelon", 10, new BigDecimal("29.99")),
new Item("papaya", 20, new BigDecimal("9.99")),
new Item("apple", 10, new BigDecimal("9.99")),
new Item("banana", 10, new BigDecimal("19.99")),
new Item("apple", 20, new BigDecimal("9.99"))
);
//group by price
Map<BigDecimal, List<Item>> groupByPriceMap =
items.stream().collect(Collectors.groupingBy(Item::getPrice));
System.out.println(groupByPriceMap);
// group by price, uses 'mapping' to convert List<Item> to Set<String>
Map<BigDecimal, Set<String>> result =
items.stream().collect(
Collectors.groupingBy(Item::getPrice,
Collectors.mapping(Item::getName, Collectors.toSet())
)
);
System.out.println(result);
}
Output
{
19.99=[
Item{name='banana', qty=20, price=19.99},
Item{name='banana', qty=10, price=19.99}
],
29.99=[
Item{name='orang', qty=10, price=29.99},
Item{name='watermelon', qty=10, price=29.99}
],
9.99=[
Item{name='apple', qty=10, price=9.99},
Item{name='papaya', qty=20, price=9.99},
Item{name='apple', qty=10, price=9.99},
Item{name='apple', qty=20, price=9.99}
]
}
//group by + mapping to Set
{
19.99=[banana],
29.99=[orang, watermelon],
9.99=[papaya, apple]
}
One of my friend suggested this solution and it worked fine
package grpBy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Pair {
Subject sub1;
Student student;
public Pair(Student student, Subject sub1 ) {
this.sub1 = sub1;
this.student = student;
}
public String getSub1() {
return sub1.name;
}
public String getStudent() {
return student.name;
}
static Pair of(Student stu, Subject sub) {
return new Pair( stu, sub);
}
}
public class Test2 {
public static void main(String[] args) {
Subject maths = new Subject("maths", 1);
Subject chemi = new Subject("chemi", 1);
Subject phy = new Subject("phy", 1);
Subject bio = new Subject("bio", 1);
List<Subject> s1 = new ArrayList<>();
s1.add(maths);
s1.add(chemi);
List<Subject> s2 = new ArrayList<>();
s2.add(maths);
s2.add(phy);
List<Subject> s3 = new ArrayList<>();
s3.add(bio);
s3.add(phy);
Student jack = new Student(1, "jack", s1);
Student jil = new Student(2, "jil", s2);
Student john = new Student(3, "john", s3);
List<Student> students = new ArrayList();
students.add(jack);
students.add(jil);
students.add(john);
Map<String, List<String>> m = students.stream().
flatMap(student -> student.subjects.stream().map(subject -> Pair.of(student, subject))).
collect(Collectors.groupingBy(e -> e.getSub1(),
Collectors.mapping(e -> e.getStudent(),
Collectors.toList())));
System.out.println(m);
}
}

File upload example for grapevine

I am new to Web API and REST services and looking to build a simple REST server which accepts file uploads. I found out grapevine which is simple and easy to understand. I couldn't find any file upload example?
This is an example using System.Web.Http
var streamProvider = new MultipartFormDataStreamProvider(ServerUploadFolder);
await Request.Content.ReadAsMultipartAsync(streamProvider);
but the grapevine Request property does not have any method to do that. Can someone point me to an example?
If you are trying to upload a file as a binary payload, see this question/answer on GitHub.
If you are trying to upload a file from a form submission, that will be a little bit trickier, as the multi-part payload parsers haven't been added yet, but it is still possible.
The following code sample is complete untested, and I just wrote this off the top of my head, so it might not be the best solution, but it's a starting point:
public static class RequestExtensions
{
public static IDictionary<string, string> ParseFormUrlEncoded(this IHttpRequest request)
{
var data = new Dictionary<string, string>();
foreach (var tuple in request.Payload.Split('='))
{
var parts = tuple.Split('&');
var key = Uri.UnescapeDataString(parts[0]);
var val = Uri.UnescapeDataString(parts[1]);
if (!data.ContainsKey(key)) data.Add(key, val);
}
return data;
}
public static IDictionary<string, FormElement> ParseFormData(this IHttpRequest request)
{
var data = new Dictionary<string, FormElement>();
var boundary = GetBoundary(request.Headers.Get("Content-Type"));
if (boundary == null) return data;
foreach (var part in request.Payload.Split(new[] { boundary }, StringSplitOptions.RemoveEmptyEntries))
{
var element = new FormElement(part);
if (!data.ContainsKey(element.Name)) data.Add(element.Name, element);
}
return data;
}
private static string GetBoundary(string contenttype)
{
if (string.IsNullOrWhiteSpace(contenttype)) return null;
return (from part in contenttype.Split(';', ',')
select part.TrimStart().TrimEnd().Split('=')
into parts
where parts[0].Equals("boundary", StringComparison.CurrentCultureIgnoreCase)
select parts[1]).FirstOrDefault();
}
}
public class FormElement
{
public string Name => _dispositionParams["name"];
public string FileName => _dispositionParams["filename"];
public Dictionary<string, string> Headers { get; private set; }
public string Value { get; }
private Dictionary<string, string> _dispositionParams;
public FormElement(string data)
{
var parts = data.Split(new [] { "\r\n\r\n", "\n\n" }, StringSplitOptions.None);
Value = parts[1];
ParseHeaders(parts[0]);
ParseParams(Headers["Content-Disposition"]);
}
private void ParseHeaders(string data)
{
Headers = data.TrimStart().TrimEnd().Split(new[] {"\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries).Select(header => header.Split(new[] {':'})).ToDictionary(parts => parts[0].TrimStart().TrimEnd(), parts => parts[1].TrimStart().TrimEnd());
}
private void ParseParams(string data)
{
_dispositionParams = new Dictionary<string, string>();
foreach (var part in data.Split(new[] {';'}))
{
if (part.IndexOf("=") == -1) continue;
var parts = part.Split(new[] {'='});
_dispositionParams.Add(parts[0].TrimStart(' '), parts[1].TrimEnd('"').TrimStart('"'));
}
}
}
If you are looking for something async to use immediately, you can try to implement the answer to this stackoverflow question, which has not been tested by me.

Resources