There are 4 different teams in 8 different groups that I created with Hashmap. I want 4 teams in each group to play a total of 12 matches, GetAway(away game) and GetHome(home game). I created a fixtur generator. I want to make separate rounds for each 8 groups. How do I create fixtures with the 8 groups I created?In the matches between the teams, the goals will be random.
Map<String, String> bagOne = Map.of("Bayern Munich", "Germany", "Sevilla", "Spaın", "Real Madrid", "Spaın", "Liverpool", "England", "Juventus", "Italy", "Paris Saint", "France", "Zenit", "Rusia", "Porto", "Rusia");
Map<String, String> bagTwo = Map.of("Barselona", "Spaın", "Atletico Madrid", "Spaın", "Manchester City", "England", "Manchester United", "England", "Borussia Dortmund", "Germany", "Shakhtar Donetsk", "Ukrayna", "Chelsea", "England", "Ajax", "Hollanda");
Map<String, String> bagThree = Map.of("Dynamo Kiev", "Ukrayna", "Redbull Salzburg", "Germany", "RB Leibzig", "Germany", "Internazionale", "Italy", "Olympiacos", "Italy", "Lazio", "Italy", "Krasnodar", "Rusia", "Atalanta", "Italy");
Map<String, String> bagFour = Map.of("Lokomotiv M", "Rusia", "Marseille", "France", "Club Brugge", "Belçika", "BorMönchengladbach", "Germany", "Galatasaray", "Türkiye", "Midtjylland", "Danimarka", "Rennes", "France", "Ferenevaros", "Macaristan");
List<Map<String, String>> remainingBags = new ArrayList<>();
remainingBags.add(bagTwo);
remainingBags.add(bagThree);
remainingBags.add(bagFour);
List<String> bagOneTeams = new ArrayList<String>(bagOne.keySet());
Collections.shuffle(bagOneTeams);
List<List<String>> remainingTeams = new ArrayList<>();
remainingTeams.add(new ArrayList<>(bagTwo.keySet()));
remainingTeams.add(new ArrayList<>(bagThree.keySet()));
remainingTeams.add(new ArrayList<>(bagFour.keySet()));
Collections.shuffle(remainingTeams.get(0));
Collections.shuffle(remainingTeams.get(1));
Collections.shuffle(remainingTeams.get(2));
List<LinkedHashMap<String, String>> resultingTeams = new ArrayList<>();
IntStream.range(0, 8).forEach(i-> {
LinkedHashMap<String, String> team = new LinkedHashMap<>();
team.put(bagOneTeams.get(i), bagOne.get(bagOneTeams.get(i)));
IntStream.range(0, 3).forEach(j -> {
String nextTeam = getNonDuplicateCountryTeam(remainingTeams.get(j), team.keySet(), remainingBags.get(j), team.values());
team.put(nextTeam, remainingBags.get(j).get(nextTeam));
});
resultingTeams.add(team);
});
Related
So I have the following item where testId is the sort key:
{
"userId": "admin",
"testId": "1234",
"picture": "abstract",
"numOfUsers": 1,
"Tests": [
{
"duration": 1234,
"typeId": "2345",
"interval": 450000,
"quantity": 333333
},
{
"duration": 1234,
"typeId": "6789",
"interval": 450000,
"quantity": 333333
},
{
"duration": 1234,
"typeId": "2020",
"interval": 450000,
"quantity": 333333
}
]
}
As you can see, there are a few identical items under tests. My question is how do I update all of them with the same value? What I mean by that is that, for example, I would like to update duration on all three objects from 1234 to 2222.
Here is the code I've tested:
Table table = dynamoDB.getTable(tableName);
Map<String, String> expressionAttributeNames = new HashMap<String, String>();
expressionAttributeNames.put("#A", "Duration");
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val1", "2222");
UpdateItemOutcome outcome = table.updateItem(
"testId",
"1234",
"set #A :val1",
expressionAttributeNames,
expressionAttributeValues);
Any help would be appreciated :)
Based on my understanding of DynamoDb and Expressions, is that you need to tell dynamodb to drill into the attribute you want.
Your code is telling it to update Duration...but where is Duration? It is not at the top level of the item.
See if this would work:
Table table = dynamoDB.getTable(tableName);
Map<String, String> expressionAttributeNames = new HashMap<String, String>();
expressionAttributeNames.put("#Dur", "duration");
expressionAttributeNames.put("#Test", "Tests");
Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val1", "2222");
UpdateItemOutcome outcome = table.updateItem(
"testId",
"1234",
"set #Test.#Dur :val1",
expressionAttributeNames,
expressionAttributeValues);
=== Edited ===
ValueMap valueMap = new ValueMap().withNumber(":dur", 2222));
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("key", year)
.withUpdateExpression("set Tests.duration = :dur")
.withValueMap(valueMap)
.withReturnValues(ReturnValue.UPDATED_NEW);
UpdateItemOutcome updateItemOutcome = table.updateItem(updateItemSpec);
=== Edited ====
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("key", "primaryKey")
.withUpdateExpression("set Tests.#duration = :value")
.withNameMap(new NameMap().with("#duration", "duration"))
.withValueMap(new ValueMap().withNumber(":value", 2222))
.withReturnValues(ReturnValue.UPDATED_NEW);
I have a list of java objects as below:
[
{
id: "frwfhfijvfhviufhbviufg",
country_code: "DE",
message_key: "key1",
translation: "This is the deutsch translation"
},
{
id: "dfregregtegetgetgttegt",
country_code: "GB",
message_key: "key1",
translation: "This is the uk translation"
},
{
id: "frffgfbgbgbgbgbgbgbgbg",
country_code: "DE",
message_key: "key2",
translation: "This is the again deutch translation"
}
]
How can I convert this into a Map<String, Map<String, String>> like below:
{
"DE": {
"key1": "This is the deutsch translation",
"key2": "This is the again deutch translation"
},
"GB": {
"key1": "This is the uk translation"
}
}
I am new to java and below is my code but the code is not correct:
Map<String, Translations> distinctTranslations = customTranslationsEntities
.stream().collect(Collectors.groupingBy(
CustomTranslationsEntity::getCountryCode,
Collectors.toMap(
CustomTranslationsEntity::getMessageKey,
CustomTranslationsEntity::getTranslation),
)))
where Translations is proto buffer message like below:
message Translations {
map<string, string> translations = 1;
}
Here map<string, string> translations means map like "key1", "This is the deutsch translation"...like this.
The output should be Map<String, Map<String,String>>:
Map<String, Map<String,String>>
distinctTranslations = customTranslationsEntities
.stream()
.collect(Collectors.groupingBy(CustomTranslationsEntity::getCountryCode,
Collectors.toMap(
CustomTranslationsEntity::getMessageKey,
CustomTranslationsEntity::getTranslation,
(v1,v2)->v1)));
I added a merge function, in case there are duplicate keys.
If you want to do it without using streams then
private List<MyObject> list = // Your object List
private Map<String, Map<String, String>> map = new HashMap<>();
for(MyObject object : list){
Map<String, String> localMap;
localMap = map.getOrDefault(object.country_code, new HashMap<>());
localMap.put(object.message_key, object.translation);
if(!map.containsKey(object.country_code)){
map.put(object.country_code, localMap);
}
}
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 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 have a JSON array like this,
JSONArray content=
"[
{"+"\"pageid\":\"19\","+"\"company\":"+"\"C1\","+"\"pageview\":"+"\"10\","+"\"visitfreq\":"+"\"2\"},{"+"\"pageid\":\"19\","+"\"company\":"+"\"C2\","+"\"pageview\":"+"\"20\","+"\"visitfreq\":"+"\"4\"},{"+"\"pageid\":\"200\","+"\"company\":"+"\"C3\","+"\"pageview\":"+"\"30\","+"\"visitfreq\":"+"\"3\"}
]";
Code for JSONArray:
JSONObject jObj1 = new JSONObject();
jObj1.put("pageid", "19");
jObj1.put("company", "C1");
jObj1.put("pageview", "10");
jObj1.put("visitfreq", "2");
JSONObject jObj2 = new JSONObject();
jObj2.put("pageid", "19");
jObj2.put("company", "C2");
jObj2.put("pageview", "20");
jObj2.put("visitfreq", "4");
JSONObject jObj3 = new JSONObject();
jObj3.put("pageid", "200");
jObj3.put("company", "C3");
jObj3.put("pageview", "30");
jObj3.put("visitfreq", "3");
JSONArray jArr = new JSONArray();
jArr.put(jObj1);
jArr.put(jObj2);
jArr.put(jObj3);
Visual representation:
[
{
"company": "C1",
"visitfreq": "2",
"pageview": "10",
"pageid": "19"
},
{
"company": "C2",
"visitfreq": "4",
"pageview": "20",
"pageid": "19"
},
{
"company": "C3",
"visitfreq": "3",
"pageview": "30",
"pageid": "200"
}
]
I have worked on it to get an out put like below
the out put like this
[{pageid},[{rest of details}] ,{pageid},[{rest of details}] ]
if same pageid occur more than once
it should be like this
[{pageid},[{rest of details1},{rest of details2},.. ],{pageid},[{rest of details}] ]
Looks like you need to create a Map that maps pageid : [{rest of details1},{rest of details2},.. ].
Once you have that Map, you can transform it into your desired output.
Here is the code that will take your JSONArray (the one in the code above) and turn it into Map<String, JSONArray>.
public Map<String, JSONArray> getJsonArrayAsMapOnPageId(JSONArray inputArr) throws JSONException {
//Map holding pageid : [{company:"",pageview:"",visitfreq:""}, ... , ...] mappings
Map<String, JSONArray> idMap = new HashMap<String, JSONArray>();
for (int i=0; i < inputArr.length(); i++) {
JSONObject inputObj = inputArr.getJSONObject(i);
String id = inputObj.getString("pageid");
JSONArray jObjList;
if (idMap.containsKey(id)) {
//append to existing list in the Map
jObjList = idMap.get(id);
} else {
//create new list
jObjList = new JSONArray();
}
JSONObject newJsonObj = new JSONObject();
newJsonObj.put("company", inputObj.get("company"));
newJsonObj.put("pageview", inputObj.get("pageview"));
newJsonObj.put("visitfreq", inputObj.get("visitfreq"));
jObjList.put(newJsonObj);
idMap.put(id, jObjList);
}
return idMap;
}
Here is what it'll look like:
System.out.println("Map looks like:\n" + pageidMap);
Here is what I got:
{
200=[
{
"company": "C3",
"visitfreq": "3",
"pageview": "30"
}
],
19=[
{
"company": "C1",
"visitfreq": "2",
"pageview": "10"
},
{
"company": "C2",
"visitfreq": "4",
"pageview": "20"
}
]
}
I'm not sure exactly in what form (Object, print to output, etc) you want this output to be in, so you can do the final bit of transforming this Map<String, JSONArray> into [{pageid},[{rest of details1},{rest of details2},.. ],{pageid},[{rest of details}] ] without problems.