Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: How to get a value from a list contained as a map value?

Tags:

java

java-8

I have the following situation: I have a LinkedHashMap<> where the key type is a String and the values types varies: double, String, LinkedHashMap, etc. I am trying to extract a value from a key of one of the LinkedHashMaps values which are a value of the main map. For example, I'd like to get the result 1 from the following code (obviously it is a mess since it doesn't even compile):

Map<String, Object> input = new HashMap<>();     
input.put("a", "1234");
input.put("b", "2345");
input.put("c", "3456");
input.put("d", new HashMap<String, String>());

HashMap<String, Object> input2 = (HashMap<String, Object>)(input.get("d"));
input2.put("d1", 1);
input2.put("d2", 2);

Optional<Integer> result = input.entrySet().stream()
            .filter(e -> e.getKey().equals("d"))
            .map(Map.Entry::getValue)
            .filter(e -> e.getKey().equals("d1"))
            .findFirst();

Where do I go wrong, and of course, what is the best way to get the result?

Thanks.

like image 608
dushkin Avatar asked Apr 02 '19 12:04

dushkin


People also ask

How to get all keys and values from map in Java?

Java - How to Get All Keys and Values from Map 1. Using forEach (Java 8+) Starting from Java 8, forEach is easiest and most convenient way to iterate over all keys and... 2. Using Map.entrySet () method Map.entrySet () method returns a Set whose entries are key,value pair of the mappings... 3. Using ...

What is a map in Java?

A Java Map implementation is an collection that maps keys to values. Every Map Entry contains key/value pairs, and every key is associated with exactly one value. The keys are unique, so no duplicates are possible. A common implementation of the Map interface is a HashMap: We've created a simple map of students (Strings) and their respective IDs:

How to create a map from a list in Java?

A Map is created, when you collect a stream of elements using either Collectors.toMap () or Collectors.groupingBy (). Let’s stream the List and collect it to a Map using Collectors.toMap (keyMapper, valueMapper) where key is unique id of user and value is name of the user which may duplicate:-

What is a list in Java?

A Java List implementation is a collection that sequentially stores references to elements. Each element has an index and is uniquely identified by it: The key difference is: Maps have two dimensions, while Lists have one dimension. Though, this doesn't stop us from converting Maps to Lists through several approaches.


1 Answers

Once you use a Map with different value (and even key) types (and worse, nested maps). Then I suggest taking a step back and try to analyse what you've done. It seems that you're way better with a class than a Map. An example with your keys:

class YourClass {
    String a;
    String b;
    String c;
    YourOtherClass d;
}

class YourOtherClass {
    Integer d1;
    Integer d2;
}

I've omitted getters, setters and constructors for simplicity.

You can then create instances of those objects, like this:

YourOtherClass yoc = new YourOtherClass(1, 2);
YourClass yc = new YourClass("1234", "2345", "3456", yoc);

And then call the specific getter to receive a value with typesafety:

String a = yc.getA(); // works
Integer i = yc.getA(); // doesn't work

Or setting a new value via the setter:

yoc.setD1(4); // works
yoc.setD1("4"); // doesn't work
like image 85
Lino Avatar answered Oct 13 '22 21:10

Lino