Compare both keys and Values of two Maps containing <String, Object> [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
I have 2 Maps,
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
map1 has {SD="BBBBBB", DNN="Internet", SST=2}
map2 has {SD="BBBBBB", DNN="Internet", SST=2}
map1.equals(map2) => false
I need the output as true. What I need to use for it ?

I need the output as true. What I need to use for it ?
Well, equal maps.
According to the Javadoc for Map.equals(Object):
Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()).
HashMap has a correct equals implementation; so the conclusion you can draw is that the entry sets actually aren't equal.
It's easy for the string representation of a Java object to look the same as another (e.g. "0", Integer.valueOf(0) and Long.valueOf(0) all print as 0); it's also possible for Strings to contain non-printable characters, e.g. "0\0" and "0" will print the same, but are not equal. And, of course, if you're using custom objects, you need to ensure that equals and hashCode are implemented correctly.
Check both keys and values in both sets, to see which one causes them not to be equal.
For example:
Set<Map.Entry<?, ?>> entrySet1 = new HashSet<>(map1.entrySet());
entrySet1.removeAll(map2.entrySet());
if (!entrySet1.isEmpty()) {
System.out.println("Entries only in map1: " + entrySet1);
}
Set<Map.Entry<?, ?>> entrySet2 = new HashSet<>(map2.entrySet());
entrySet2.removeAll(map1.entrySet());
if (!entrySet2.isEmpty()) {
System.out.println("Entries only in map2: " + entrySet2);
}

Related

If I have a HashMap where the values are a list, can I stream a certain index of that list into a new list? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am looking to take a Hashmap that has many key/value pairs where each value has a list. I want to take an index, and make a list based on that index.
Essentially I am looking to take this code and turn it into a stream.
HashMap < Integer , List< Object >> map = new HashMap();
//name //age
map.put("1", new List("Bob",20));
map.put("2", new List("Jim",37));
map.put("3", new List("Dan",30));
map.put("3", new List("Rick",40));
List < Integer > s = new ArrayList();
map.values().forEach(e - >
{
s.add(( Integer ) e.get(1)); //looking to extract all of the ages into
}); // a new list.
In my use case, each index of the list is a different type of object, so in this case I tried to use a String and an Integer. I mention this in case there is a way to select an item based on an object's type to put into the new list. I had found this example that mentioned "groupingBy" as a Collector's option, but It doesn't seem to work for my use-case
Shortcut for adding to List in a HashMap
Thank you for any help
Make a stream of the map's values collection, use Stream.map to extract the values you care about, and then Stream.collect to make a new collection.
List<Integer> ages = map.values().stream()
.map(list -> (Integer) list.get(1))
.collect(Collectors.toList());
I agree with the commenter on the question who said you should really make these into actual POJO objects - the code will be a lot clearer and less error prone if you do. If you go that route then you can use a method reference to get the ages:
.map(Person::getAge)

How do I stop HashMap from overwriting previous value for a key? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
With my code, the new value overwrites the value stored previously against the same key.
This is my code:
HashMap<String, String> meMap = new HashMap<String, String>();
meMap.put(p.getName(), selState);
If the key is same for all then you should map key to list of values: Map<String, List<String>>
And then to update list of values mapped to a specific key:
List<String> values = map.get(key);
values.add("new");
map.put(key, values);
The HashMap is a pair key/value object. It can be only one value for a given key.
If you try to put the same key that already exists, you will be replacing the existing value for that key.
If you want to add a new value, you have to give a new key to that value.

Map<String, Integer> foo - how do I get the value of the integer - JAVA [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a method:
public void updateBoard (Map<String, Integer> foo)
How do I find out the value of the Integer? I'm trying foo.get() but it just gives me the key.
Thanks!
Here is how map works
Map <String,Integer> myMap = new HashMap<String,Integer>();
myMap.put("manikant",123);
// Many more put..
System.out.println(myMap.get("manikant"));
// In case you are using java 8. you can also try this.
myMap.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v));
output
123
in your case you can use foo.get(/*enter your key*/);
For more Information see how map works in java
You get all Integer values of the Map with:
foo.values();
Because a Map is a key->value construct.
With
foo.keySet();
you get all keys.
And with
foo.get("key1");
you get the appropriate value for the key key1.
If you are trying to get all the values in the map, use foo.values().
If you are trying to get the value to a specific key in the map, use foo.get(<key>).
Hope it helps!

how to put values into HashMap<int,List<String>> [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to put and get the below values in/from HashMap as Key and values
Key Value
1 "a"
1 "b"
1 "C"
2 "x"
2 "y"
2 "z"
Well the general idea would be to put the String values in your List<String>. The add method is good for that.
Later you should put the key (1 or 2 according to your example) with the List<String> in your map. The put method is good for that.
It expects a key (your integer number) and a value (your list of strings).
try this..
HashMap<Integer, List<String>> map = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
map.put(1, list);
If you need some direction here is a link with all you need to know about HashMap.
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Java Collections Map to Set [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wanting to convert a HashMap to a Set.I am trying to find common elements between two maps by first putting that to a set and use retainAll.How to convert a Map to a Set.
If you want a set containing the keys use:
Set<KEY_TYPE> set = map.keySet();
If you want a set containing the values use:
Set<VALUE_TYPE> set = new HashSet<VALUE_TYPE>(map.values());
if you want a set containing both elements use:
Set<Map.Entry<KEY_TYPE, VALUE_TYPE>> set = map.entrySet();
You access the elements of an Entry using getKey() and getValue()
HashMap has a key set and a value set, to keep the associativity, HashMap has a method called
entrySet()
you can find more info about it here
As I see from the comments, you want key-value pairs. This you can easily get from the map. Here is an example:
Map<Integer, String> myMap = new HashMap<Integer, String>();
// ... put values into your map
Set<Entry<Integer, String>> entrySet = myMap.entrySet();
Although from your question I'm not sure this is all you want. Maybe you should rephrase your question and post your code what you did so far, so that we can understand where exactly you need help.

Categories