Jedis HMSET map insertion order - java

Jedis has a hmset method which allows you to set a map of fields and their values at a specific key.
I use the method this way:
Map<String, String> map = new LinkedHashMap<String, String>();
// General player data
map.put("name", player.getName());
map.put("ip", player.getAddress().getAddress().getHostAddress());
map.put("rank", "none");
map.put("tokens", "0");
map.put("coins", "0");
// Arsenal player statistics
map.put("ar_score", "0");
map.put("ar_gameswon", "0");
map.put("ar_gameslost", "0");
map.put("ar_kills", "0");
map.put("ar_deaths", "0");
pipeline.hmset("uuid:" + uuid, map);
pipeline.sync();
core.redis.getJedisPool().returnResourceObject(jedis);
I decided to use a LinkedHashMap to retain the insertion order — however, when looking at the database, it still messes up the order.
Does anyone know how to insert it into the database without messing up the order?
Thank you.

The String-based Jedis client transcodes the strings using a temporary HashMap, so it kills your order. The BinaryClient iterates over the Map directly and retains the order.
The lettuce client keeps the order of your map in any case.
Alternatively, set the values one by one using HSET hash key value
HTH, Mark

Give a try to Redis based framework for Java - Redisson. It keeps map entries in insertion order during iteration using any codec (Jackson JSON, Avro, Smile, CBOR, MsgPack, Kryo, FST, LZ4, Snappy and JDK Serialization).

Related

How to create a map that takes values from other map and then maps them to its field?

So I have a map of ids to systemUsers and now I want to create a map of systemUser keys and login values. Login is a field inside systemUser class. I have a problem with how to write the mapper functions or even if it's the right way to go about it
Map<Long, PHSystemUser> systemUserMap = getPersistenceLogic()
.getSystemUsersMap(serviceClientMap.values());
Map<PHSystemUser, String> loginMap = systemUserMap.values().stream()
.map(PHSystemUser::getLogin)
.collect(Collectors.toMap(, ));
All you need is to collect directly using two functions:
systemUserMap.values().stream()
.collect(Collectors.toMap(Function.identity(), PHSystemUser::getLogin));
The problem with .map(PHSystemUser::getLogin) is that it changes the stream to Stream<String>, leaving you no chance to have the entire PHSystemUser object downstream.

get Keys from IgniteCache

I have to get all keys stored in IgniteCache, unfortunately this method is not implemented in Ignite. I'm using java client.
I thought it is a common method, what is the reason Ignite team didn't implement it?
Is there any efficient solution for getting keys?
Thanks to #alexfedotov I created a solution for my problem, I'm positng it here, since someone may find it useful.
List<K> keys = new ArrayList<>();
cache.query(new ScanQuery<>(null)).forEach(entry -> keys.add((K) entry.getKey()));
After running this code you will receive a list with keyset.
You can get all the keys using ScanQuery with a null predicate. It will return all the entries (key-value pairs).
As well you can use an SqlFieldsQuery like select _key from Entity
The easiest way for getting all keys from cache :
ICache<string, object> cache = ignite.GetCache<string, object>(cacheName);
List<string> cacheKeys = cache.Select(e => e.Key).ToList();

How can I retrieve the last record inserted into a Java Hashtable data structure?

I have the following problem working on a Java application that uses an Hashtable calcoliTable data structure.
This is the content of my calcoliTable:
{
FITTIZIO-2015=CalcoloValoreDellaGSBean [data=2015-11-27, maturato=249540.544989802560000, sommaMaturatoMovimento=249540.544989802560000],
1=CalcoloValoreDellaGSBean [data=2015-11-27, maturato=249540.544989802560000, sommaMaturatoMovimento=249540.544989802560000]
}
As you can see it contains 2 entries having id=1 and id=FITTIZIO-2015.
I want to retrieve the entry that was inserted most recently(that should be the one having id=FITTIZIO-2015).
I have tried to do in this way:
CalcoloValoreDellaGSBean calcoloPrecedente = (CalcoloValoreDellaGSBean) calcoliTable.get(calcoliTable.size())
But it won't work because in this way it is searching the entry having id=2.
How can I retrieve the last entry inserted into my HashTable? Exist a way to do it without using explitelly the key?
Use a LinkedHashMap with an access-order ordering mode :
Map<KeyType,ValueType> map = new LinkedHashMap<KeyType,ValueType> (16, 0.75F, true);
And then
map.entrySet().iterator().next()
will give you the last entry you accessed (either inserted or retrieved).

how to filter following map?

I have got these values in my HashMap ,
"ver":"a"
"ver":"b"
"ver":"c"
"os":"d"
"os":"e"
i need only:
"os":"d"
"os":"e"
My code is:
String[] eachPair = myString.split(",");
Map<String,String> pairs = new HashMap<String,String>();
for(String pair: eachPair) {
pairs.put(pair.substring(0, pair.indexOf(":")).trim(), pair.substring(pair.indexOf(":")+1));
}
pairs.get("os");
but its not working. please help
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Map should have unique keys only, If you add a value with the same key which is already exist in the map, value for that key will be override and you will lost the old value.
HahMap doesn't allows duplicate keys.So I recommand to use Guava's MultiMap or apache's MultiValueMap
Please look into
the sample code reference of MultiMap: http://tomjefferys.blogspot.in/2011/09/multimaps-google-guava.html
the sample code reference of MultiValueMap: http://java.dzone.com/articles/allowing-duplicate-keys-java

Is is possible to store a SQLite table in Shared Prefs on Android

I need to take around 100 rows from a table and put them into the shared prefs then delete the database install a fresh and compare values.
I would only need to store 2 values from each row. Column A and column B, but I don't know if this is even possible?
Cheers for any help.
you can't store a table, but if your table really is that simple (or even if it isn't... I think you can store as much data as you feel like, really), you can transfer it over to a JSON object, stringify and store that. Then pack it back into JSON and over into your new table when you're ready to use it again.
I can also suggest to use a generic lib for abstraction of local store.
Here you have an example:
Use this android lib: https://github.com/wareninja/generic-store-for-android
import com.wareninja.opensource.genericstore.GenericStore;
// step.1: transfer data fromt able into GenericStore (aka cache)
String objKey = "mytable01";
int storeType = GenericStore.TYPE_SHAREDPREF;
//keep in mind that you can also use GenericStore.TYPE_MEMDISKCACHE
// which will store on disk cache, which can be faster and better for memory allocation
LinkedHashMap<String, Object> dataMap = new LinkedHashMap<String, Object>();
// fill in all the data from you table, e.g.
dataMap.put("row1", "val1");
GenericStore.saveObject(storeType, objKey, dataMap, mContext);
// now everything is on stored/cached
// step.2: get data back
dataMap = (LinkedHashMap<String, Object>) GenericStore.getObject(storeType, objKey, mContext);
...
Social Coding # Aspiro TV

Categories

Resources