Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Java Map filter key value

Hello there,


I have this Map and I just want to filter out the keys with uid1,uid2,uid3,uid4


Map<String, String> zplParameterForEachSerialLabel = new HashMap<String, String>();

Open in new window


Child serials to update in table::::{gtin3=112345678901234, gtin2=112345678901234, gtin4=112345678901234, fnc1=011234567890123421119800845884507218271723012210777, fnc2=011234567890123421119800819934009082121723012210777, fnc3=011234567890123421119800150131802530941723012210777, fnc4=011234567890123421119800814078005813581723012210777, lot3=777, lot4=777, lot1=777, uid4=19800814078005813581, lot2=777, uid2=19800819934009082121, uid3=19800150131802530941, uid1=19800845884507218271, exp4=2023-01-22, exp3=2023-01-22, exp2=2023-01-22, exp1=2023-01-22, price3=60000, price4=60000, gtin1=112345678901234, price1=60000, price2=60000}

Open in new window


I tried something like this but not working for obvious reason being I am missing the 1,2,3,4, in the uid


List<Tuple> batch = new ArrayList<>();
valuesMap.forEach((key,value) -> {
  LOG.debug(key+" : "+value);
  if(valuesMap.containsKey("uid")){
    batch.add(Tuple.of(valuesMap.get("uid")));
  }
});
LOG.debug(batch.iterator().next().deepToString());

Open in new window


Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Which Tuple api are you using?

If you use org.javatuples, you could do:

            List<Pair<String, String>> tupleKeyVals = valuesMap.entrySet().stream()
                .filter(e -> !e.getKey().matches("uid[1234]"))
                .map(e -> new Pair<String, String>(e.getKey(), e.getValue()))
                .collect(Collectors.toList());

Printing the resulting List gives:
[[gtin3, 112345678901234], [gtin2, 112345678901234], [gtin4, 112345678901234], [fnc1, 011234567890123421119800845884507218271723012210777], [fnc2, 011234567890123421119800819934009082121723012210777], [fnc3, 011234567890123421119800150131802530941723012210777], [fnc4, 011234567890123421119800814078005813581723012210777], [lot3, 777], [lot4, 777], [lot1, 777], [lot2, 777], [exp4, 2023-01-22], [exp3, 2023-01-22], [exp2, 2023-01-22], [exp1, 2023-01-22], [price3, 60000], [price4, 60000], [gtin1, 112345678901234], [price1, 60000], [price2, 60000]]

Open in new window

Avatar of Zolf

ASKER


Thanks for your comments.

I am using Vertx https://vertx.io/docs/apidocs/io/vertx/reactivex/sqlclient/Tuple.html

I just need the key/value with uid1,uid2,uid3,uid4 from all those keys.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Greetings.

Since the key is of String, you can try with below. It filters all keys starting with uid:
List<Tuple> batch = new ArrayList<>();
valuesMap.forEach((key,value) -> {
  LOG.debug(key+" : "+value);
  if(key.startsWith("uid")){
    batch.add(Tuple.of(valuesMap.get("uid")));
  }
});
LOG.debug(batch.iterator().next().deepToString());

Open in new window


if you are looking for specific keys with uid, then solution using regex given by our expert (CHEJ) is perfect.
:)