How to check if element in groovy array/hash/collection/list?
Date : March 29 2020, 07:55 AM
help you fix your problem .contains() is the best method for lists, but for maps you will need to use .containsKey() or .containsValue() [a:1,b:2,c:3].containsValue(3)
[a:1,b:2,c:3].containsKey('a')
|
Selecting single hash from array where one hash element has the highest value
Tag : ruby , By : Pierre LeBoo
Date : March 29 2020, 07:55 AM
To fix this issue From the following array of hashes, how can I select a single hash where timestamp has the highest value? , Here you can see: data.max_by{|e| e["timestamp"] }
# >> {"id"=>2, "timestamp"=>1383314386, "data"=>"64ed2ed9-763d-443f-a74e-e7f10cbe783e"}
|
In Perl, how can I access a hash array element, that is itself another hash array?
Tag : perl , By : Techspirit
Date : March 29 2020, 07:55 AM
like below fixes the issue You didn't declare %thisDevice hash and $thisDevice hashref is completely another variable. Change $thisDevice{'remote_device_connect_protocol'};
$thisDevice->{'remote_device_connect_protocol'};
|
Java 1D array list collection .contains or Hash-Map.containsKey speed
Date : March 29 2020, 07:55 AM
Any of those help You can find details on how each is implemented in the java docs. I believe ArrayList will do a linear search. So HashMap is most likely better.
|
Java Hash map / Array List Count distinct values
Tag : java , By : Ricardo
Date : March 29 2020, 07:55 AM
will be helpful for those in need You could store a Setof productIds per clientId, and just take the size of that. As a Set does not allow duplicate values, this will effectively give you the distinct number of productIds. Also, I recommend that you give your variables meaningful name instead of col2, k, map... This will make your code more readable. Map<String, Set<String>> distinctProductsPerClient = new HashMap<String, Set<String>>();
// Process each CSV file line which is now contained within
// the linesList list Array
// Start from 1 to skip the first line
for (int i = 1; i < linesList.size(); i++) {
String line = linesList.get(i);
String[] data = line.split(csvSplitBy);
String productId = data[1];
String clientId = data[2];
String date = data[3];
// Determine if Column 4 has the desired date
// and count the values
if (date.contains("10/12/2017")) {
if (!distinctProductsPerClient.containsKey(clientId)) {
distinctProductsPerClient.put(clientId, new HashSet<>());
}
distinctProductsPerClient.get(clientId).add(productId);
}
}
for (final String clientId : distinctProductsPerClient.keySet()) {
System.out.println(clientId + ": " + distinctProductsPerClient.get(clientId).size());
}
private static class OrderData {
private final String productName;
private final String productId;
private final String clientId;
private final String date;
public OrderData(String csvLine) {
String[] data = csvLine.split("\t");
this.productName = data[0];
this.productId = data[1];
this.clientId = data[2];
this.date = data[3];
}
public String getProductName() {
return productName;
}
public String getProductId() {
return productId;
}
public String getClientId() {
return clientId;
}
public String getDate() {
return date;
}
}
Map<String, Set<String>> distinctProductsPerClient2 = linesList.stream()
.skip(1)
.map(OrderData::new)
.collect(groupingBy(OrderData::getClientId, mapping(OrderData::getProductId, toSet())));
|