Map<Long, Employee.status> prevStatus = empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getEmployeeStatus));
I already have the above code, I need to add a similar operation but instead of creating a new Map I want to add the result to the existing map.
prevStatus = empRecords.stream()
.collect(Collectors.**toMap**(employeeRecord::getEmloyeeID,
employeeRecord::**getUSEmployeeStatus**));
You can create a new Map and add its entries to the existing Map:
prevStatus.putAll(
empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getUSEmployeeStatus)));
Or you can use forEach instead of collect:
empRecords.stream()
.forEach(emp -> prevStatus.put(emp.getEmloyeeID (),
emp.getEmployeeStatus()));
This is expected code
empRecords.stream()
.collect(Collectors.toMap(employeeRecord::getEmloyeeID,
employeeRecord::getEmployeeStatus,(newVal,oldVal)->newVal,()->existingMap));
Where (newVal,oldVal)->newVal logic is for what if key is already present in existing Map and
existingMap is your existing map. Make sure existingMap is not null. If it is null going with new HashMap what you shared in your code itself fine.
Related
I am working on a programm in Java which contains a part where I get a couple of information from my Python Server. It is a Hashmap that looks like this:
{hid=null, art=CWE, produktgruppe=23, objekt=Küche, betrag=714.989990234375, iban=DE0212, id=2812, varten=[{art=2, lz_min=24, lz_max=36, faktor=null, zinssatz=0.5, kondnr=84}], status=979}
How can I get the value of the key "zinssatz".
I tried it with
mymap.get("zinssatz");
But that always returns zero. Which is probably because it can't find the key inside my hashmap because it is build like a "Dictionary with a list of dictionaries" inside it.
Thanks for your help.
It seems to be a nested map. Have you tried the following:
((HashMap)mymap.get("varten")).get("zinssatz");
Or, if you're not sure if varten is really a HashMap, you can try the following to get the type:
System.out.println(mymap.get("varten").getClass());
Update
If varten is a HashMap stored inside an ArrayList, try the following:
((HashMap)((ArrayList)mymap.get("varten")).get(0)).get("zinssatz");
...which is the same as:
List list = (ArrayList) mymap.get("varten");
Map map = (HashMap) list.get(0);
Object zinssatz = map.get("zinssatz");
Officially, This is the example code using java:
BatchGetResultPageIterable batchResults = enhancedClient.batchGetItem(r -> r.addReadBatch(ReadBatch.builder(Customer.class)
.mappedTableResource(customerTable)
.addGetItem(key1)
.addGetItem(key2)
.addGetItem(key3)
.build()));
What is the way to achieve that but sending a list of keys ? The library that I have to use is: https://github.com/aws/aws-sdk-java-v2/tree/master/services-custom/dynamodb-enhanced
Create your batch get builder outside a stream. Create a stream of your keys and for each item in the stream, add it using the builder.
ReadBatch.Builder readBatchBuilder = ReadBatch.builder(QueryResultObject.class)
keys.forEach {
readBatchBuilder.addGetItem(
Key.builder().partitionValue(it.toString()).build()
)
}
So I have a Map of String/String list pairs, and what I want to do is after extraction, combine the returned lists into one list on which i can perform more assertions:
MyTest.java
Map<String, List<String>> testMap = new HashMap<>();
List<String> nameList = newArrayList("Dave", "Jeff");
List<String> jobList = newArrayList("Plumber", "Builder");
List<String> cityList = newArrayList("Dover", "Boston");
testMap.put("name", nameList);
testMap.put("job", jobList);
testMap.put("city", cityList);
assertThat(testMap).hasSize(3)
.extracting("name", "city")
//not sure where to go from here to flatten list of lists
// I want to do something like .flatMap().contains(expectedValuesList)
When I call extracting, it pulls out the list values into a list of lists, which is fine, but I cant call flatExtracting after that as there are no property names to pass in, and from what I've read it doesn't seem like a custom extractor would be appropriate(or else I'm not entirely sure how to put it together). Is there another way to flatten the list of lists im getting back? I could go a slightly longer route and do assertions on the list of lists, or use a lambda before the assert to collect the results but I'd like to keep the assertion as one(e.g. some map asserts then chain some assertions on the contents)
flatExtracting is not in the map assertions API (yet), what you can instead is:
assertThat(testMap)
.hasSize(3)
.extracting("name", "city", "job")
.flatExtracting(list -> ((List<String>) list))
.contains("Dave", "Jeff", "Plumber", "Builder", "Dover", "Boston");
I ended creating https://github.com/joel-costigliola/assertj-core/issues/1034 to support this use case
I have a Properties object with approximately 10k elements in it. And I want to remove entries(key/value) that their key start with a specific text. Currently, I am using this code to do so:
Properties temp = new Properties();
myProperties.keySet().forEach(key -> {
if (!key.toString().startsWith("specificText")){
temp.setProperty(key, myProperties.get(key));
}
});
myProperties = temp;
Another solution is using putAll instead of calling setProperty multiple times:
Properties temp = new Properties();
temp.putAll(myProperties.entrySet().stream()
.filter(entry -> !entry.getKey().toString().startsWith("specificText")
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
myProperties= temp;
But none of the above solutions are efficient enough for my work. I am sure there should be a better way to remove unwanted properties. Any help is greatly appreciated.
Modifying a Map’s keySet directly affects the Map itself:
myProperties.keySet().removeIf(key -> key.toString().startsWith("specificText"));
From the documentation of Map.keySet():
The set supports element removal, which removes the corresponding mapping from the map…
How can i read a list of users from the configuration file in play framework?
i have tried doing something like this:
users=[{uid:123,pwd:xyz},{uid:321,pwd:abc}]
from the play application
List<Object> uids = Play.application().configuration().getList("users");
will give me this a list of objects, if I iterate through the list i get each object as
{uid=123,pwd=xyz} and {uid=321,pwd=abc}
at this point i don't know how i can elegantly get the value of the uid, i can do some hacky job as omit the first and last bracket and parse for the before after equal sign, but it would be too ugly! any idea? (the application is written in java)
thanks
A Scala implementation that avoids the deprecated getConfigList method would rely on retrieving a Seq[Configuration] as follows:
case class UserConfig(uid: Int, pwd: String)
val userConfigs: Seq[UserConfig] = configuration.get[Seq[Configuration]]("users").map { userConfig =>
UserConfig(userConfig.get[Int]("uid"), userConfig.get[String]("pwd"))
}
Since I had recently the same problem and this is still unanswered,
here is my suggestion:
List<User> users = getConfig().getConfigList("users").stream().map(
config -> new User(config.getString("uid"), config.getBoolean("pwd"))
).collect(Collectors.toList());
As far as I know there are no tuples or anything in Java, you need to use either an object or a list with two elements. I decided to go for an object here, you can also return a list.
A list of uid's sounds to me like:
# List of UID's
users=[123,456,789] // every number represents a UID
Then you can get this list as:
List<Object> uids = Play.application().configuration().getList("users");
And then do what you want with this:
for (Iterator<Object> iterator = uids.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);
}
Is this what you are looking for?
BTW, you can read more about Play Framework configuration options: http://www.playframework.com/documentation/2.1.0/Configuration