Hash map iteration - iterator

How do I iterate through a hash map to find the first 10 elements for eg if my map contains string as key and int as value, I want to fetch the first 10 values with highest integer?

Let's say we have Map and we are allowed to use external library - Guava:
Map<String, Integer> map = Maps.newTreeMap();
map.put("A", 13);
map.put("B", 11);
map.put("C", 27);
map.put("D", 38);
map.put("E", 25);
map.put("F", 12);
map.put("G", 25);
map.put("D", 35);
map.put("H", 28);
map.put("R", 13);
map.put("N", 24);
map.put("T", 37);
Create Guava MultiMap and add entries from original map
Multimap<String, Integer> multiMap = ArrayListMultimap.create();
for(String key : map.keySet()){
multiMap.put(key, map.get(key));
}
Inverse multiMap and copy to TreeMultiMap
TreeMultimap<Integer, String> reversed = TreeMultimap.create();
Multimaps.invertFrom(multiMap, reversed);
Create List from entries and get first 10 elements:
Lists.newArrayList(reversed.entries()).subList(0,10)

If it's a one time thing, you can sort the HashMap by converting to a List then back to a LinkedHashMap:
Map<String, Integer> map = new HashMap<>();
map.put("A", 13);
map.put("B", 11);
map.put("C", 27);
map.put("D", 38);
map.put("E", 25);
map.put("F", 12);
map.put("G", 25);
map.put("D", 35);
map.put("H", 28);
map.put("R", 13);
map.put("N", 24);
map.put("T", 37);
// Take a List copy of the Map
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(map.entrySet());
// Sort the list by the Value
Collections.sort(list, new Comparator<Entry<String, Integer>>() {
#Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return (o1.getValue()).compareTo(o2.getValue());
}
});
// Create new Map (use LinkedHashMap to maintain order)
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}

Most HashMaps don`t preserve any kind of order so you might need to read all the keys, sort them and then get the corresponding values from the Hash. If you can tell us what language and provide some sample code, someone might be able to help further.

Related

How to get elements in one Flux that does not appear in another Flux

I am new to Spring Reactive Project. There was a problem in use.
I have two Flux, One has more elements, such as
Flux<Integer> bigFlux = Flux.range(1, 10);
And another likes
Flux<Integer> smallFlux = Flux.just(3, 7);
How can I get the elements in bigFlux that not appears in smallFlux?
I don't know which operator to use.
I have tried:
Flux<Integer> flux = bigFlux.filterWhen(one -> smallFlux.hasElement(one).map(a->!a));
But this is not wise, I got smallFlux through complex operations, such as querying the database, flatMap operations. In this way, how many elements in bigFlux, how many times these operations will be repeated.
In fact, smallFlux is obtained in this way.
Flux<File> usedFile = repository.findAll()
.flatMap(one -> {
List<File> used = someMethods(one);
return Flux.fromIterable(used);
});
Are there other better solutions, thanks.
I think this will be a cleaner and faster solution
final Flux<Integer> bigListFlux = Flux.just(1, 2, 3);
final Flux<Integer> smallListFlux = Flux.just(3, 5, 6);
Mono.zip(bigListFlux.collectList(), smallListFlux.collectList(), (bigList, smallList) -> {
bigList.removeAll(smallList);
return bigList;
}).flatMapMany(Flux::fromIterable).map(element -> {
System.out.println("element = " + element);
return element;
}).subscribe(); // do not use subscribe/block in actual production code.
I've used the following variant of Mono.zip
https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#zip-reactor.core.publisher.Mono-reactor.core.publisher.Mono-java.util.function.BiFunction-

Using Apache Velocity for template SQL

I want to use Apache Velocity Template Engine to generate SQL query based on the input.
Any sample snippet to get started would be helpful.
JSONObject keysObject = new JSONObject();
keysObject.put("HistoryId", "1");
keysObject.put("TenantName", "Tesla");
Iterator<?> keys = keysObject.keys();
ArrayList list = new ArrayList();
Map map = new HashMap();
while( keys.hasNext() ) {
String key = (String)keys.next();
map.put(key, keysObject.get(key));
}
list.add( map );
int keyObjectSize = keysObject.length();
JSONObject can have more keys, but in this example i am using 2.
I want to use the keys historyId and tenantName to generate below SQL query, Where keys are used as the column name and keys size can be used to generate the value parameter(?1, ?2).
INSERT INTO "Alert" (historyid, tenantname) VALUES (?1, ?2)

How do I create a map from 2 arrays?

I have a string array and an integer array. How do I create a map using the first as keys and the second as values?
val keys = arrayOf("butter", "milk", "apples")
val values = arrayOf(5, 10, 42)
val map: Map<String, Int> = ???
How to convert List to Map in Kotlin? doesn't solve this problem; I have 2 arrays and want a single map.
You can zip together the arrays to get a list of pairs (List<Pair<String, Int>>), and then use toMap to get your map.
Like this:
val keys = arrayOf("butter", "milk", "apples")
val values = arrayOf(5, 10, 42)
val map: Map<String, Int> =
keys.zip(values) // Gives you [("butter", 5), ("milk", 10), ("apples", 42)]
.toMap() // This is an extension function on Iterable<Pair<K, V>>
According to kotlin
Constructing Collections
-> creating a short-living Pair object, is not recommended only if performance isn't critical and to quote: "To avoid excessive memory usage, use alternative ways. For example, you can create a mutable map and populate it using the write operations. The apply() function can help to keep the initialization fluent here."
since I'm not much of an expert I run on to these code and maybe this should work better?:
val numbersMap = mutableMapOf<String,Int>()
.apply{ for (i in 1.. 5) this["key$i"] = i }
println(numbersMap)
//result = {key1=1, key2=2, key3=3, key4=4}
or to adjust it to question above - something like this:
val keys = arrayOf("butter", "milk", "apples")
val values = arrayOf(5, 10, 42)
val mapNumber = mutableMapOf<String, Int>()
.apply { for (i in keys.indices) this[keys[i]] = values[i] }
println(mapNumber)

how to connect and read values from kepware using OPCAutomation.dll

I am creating a small c# program to connect and read value from kepware server using OPCAutomation.dll, but unable to get its syntax?
OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
_OPCServer.connect("", ""......);
what values will come inside these brackets?
OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
_OPCServer.connect("Kepware.KEPServerEX.V5", "");
The second parameter is the OPC Server node and can be left String.Empty.
From Reflector:
public virtual extern void Connect([In, MarshalAs(UnmanagedType.BStr)] string ProgID, [In, Optional, MarshalAs(UnmanagedType.Struct)] object Node);
I'm adding an explample to read and write values:
// set up some variables
OPCServer ConnectedOpc = new OPCServer();
Array OPCItemIDs = Array.CreateInstance(typeof(string), 10);
Array ItemServerHandles = Array.CreateInstance(typeof(Int32), 10);
Array ItemServerErrors = Array.CreateInstance(typeof(Int32), 10);
Array ClientHandles = Array.CreateInstance(typeof(Int32), 10);
Array RequestedDataTypes = Array.CreateInstance(typeof(Int16), 10);
Array AccessPaths = Array.CreateInstance(typeof(string), 10);
OPCGroup OpcGroupNames;
// connect to KepServerEX
ConnectedOpc.Connect("Kepware.KEPServerEX.V5", "");
// Add tags and OPC group.
// set up the tags
OPCItemIDs.SetValue("Counting.PLC.Station1.LoggedON", 1);
OPCItemIDs.SetValue("Counting.PLC.Station2.LoggedON", 2);
OPCItemIDs.SetValue("Counting.PLC.Station3.LoggedON", 3);
OPCItemIDs.SetValue("Counting.PLC.Station1.Operator", 4);
OPCItemIDs.SetValue("Counting.PLC.Station2.Operator", 5);
OPCItemIDs.SetValue("Counting.PLC.Station3.Operator", 6);
// set up the opc group
OpcGroupNames = ConnectedOpc.OPCGroups.Add("Group01");
OpcGroupNames.DeadBand = 0;
OpcGroupNames.UpdateRate = 100;
OpcGroupNames.IsSubscribed = true;
OpcGroupNames.IsActive = true;
OpcGroupNames.OPCItems.AddItems(6, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);
// Read the values from the server for those tags.
// read
Array ItemServerReadValues = Array.CreateInstance(typeof(string), 10);
object a;
object b;
OpcGroupNames.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 6, ref ItemServerHandles, out ItemServerReadValues, out ItemServerErrors, out a, out b);
Console.WriteLine((string)ItemServerReadValues.GetValue(4));
Console.WriteLine((string)ItemServerReadValues.GetValue(5));
Console.WriteLine((string)ItemServerReadValues.GetValue(6));
// Write some values into the server for those tags.
// write
Array ItemServerWriteValues = Array.CreateInstance(typeof(object), 7);
ItemServerWriteValues.SetValue(1, 1);
ItemServerWriteValues.SetValue(1, 2);
ItemServerWriteValues.SetValue(1, 3);
ItemServerWriteValues.SetValue("Test Op 1", 4);
ItemServerWriteValues.SetValue("Test Op 2", 5);
ItemServerWriteValues.SetValue("Test Op 3", 6);
OpcGroupNames.SyncWrite(6, ref ItemServerHandles, ref ItemServerReadValues, out ItemServerErrors);
This example is adapted from: http://lifeisunderconstruction.blogspot.mx/2011/03/opc-client-in-asp-net-c.html, I have added it just in case the link gets broken.

removeAll Java Issue

I have an issue where I am creating an arraylist and adding multiple arraylists to this one.
At some points in the program I need to remove these lists from the one central list. I have been using removeAll(); but this removes all instances of one element. For example the arraylist can contain (1,2,3,4,5) and one can add the list (1,2,3) to it. Yet when you go to remove this list the resultant list now contains (4,5) while it is desired for it to contain (1,2,3,4,5). How can that be accomplished? Thanks for any help.
Sounds like you should just use remove instead of removeAll. You can put it in a loop to remove all the elements from a collection:
ArrayList<Integer> bigList = new ArrayList<Integer>();
// Put multiple smaller lists into big list
bigList.addAll(list1);
bigList.addAll(list2);
bigList.addAll(list3);
// Remove list2's elements from bigList
for (Integer i : list2) {
bigList.remove(i);
}
Update:
Runnable version:
import java.util.*;
public class RemoveTest {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> list3 = Arrays.asList(9);
ArrayList<Integer> bigList = new ArrayList<Integer>();
// Put multiple smaller lists into big list
bigList.addAll(list1);
bigList.addAll(list2);
bigList.addAll(list3);
// Remove list2's elements from bigList
for (Integer i : list2) {
bigList.remove(i);
}
System.out.println(bigList);
// Result:
// [1, 2, 3, 9]
}
}