I have an Map Map1 and another Map Map2..
Map<Key,ProductEntity> Map1;
Map<Key, ProductEntitySecond> Map2;
I want to iterate in map1 and check if that particular key of Map1 is present in Map2. if it is then return the value .
Can someone please give me an idea to solve that type of problem.
CodePudding user response:
Maybe you can do something like this:
map1
.entrySet()
.stream()
.filter(e -> map2.containsKey(e.getKey()))
.findFirst()
.map(e -> e.getValue())
.orElse(null);
So if the key is present in map2
it'll return the value from map1
otherwise, it'll return null
CodePudding user response:
You can use the Stream API.
public class Application {
public static void main(String[] args) {
var map1 = new HashMap<Square, String>();
map1.put(new Square(2), "two");
map1.put(new Square(3), "three");
map1.put(new Square(4), "four");
map1.put(new Square(5), "five");
var map2 = new HashMap<Square, String>();
map2.put(new Square(2), "two");
map2.put(new Square(3), "three");
var valuesAlsoInMap2 = map1.entrySet().stream()
.filter(it -> map2.containsKey(it.getKey()))
.map(Map.Entry::getValue)
.toList();
System.out.println(valuesAlsoInMap2);
}
}
Expected result:
[two, three]
This is using a simple POJO Square
to show you it does work with non-primitive types. To see which methods HashMap relies on have a look at this thread.
import java.util.Objects;
class Square extends shape{
private int side;
public Square(int side) {
this.side = side;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Square square = (Square) o;
return side == square.side;
}
@Override
public int hashCode() {
return Objects.hash(side);
}
@Override
public double Area() {
return side*side;
}
public int getSide() {
return side;
}
public void setSide(int side) {
this.side = side;
}
}
CodePudding user response:
Here's another approach using an intersection operation (retainAll()
) on the two maps' key sets.
Map<Key, ProductEntity> map1 = ...;
Map<Key, ProductEntitySecond> map2 = ...;
Map<Key, ProductionEntity> tmp = new HashMap<>(map1);
tmp.keySet().retainAll(map2.keySet());
Collection<ProductionEntity> shared = tmp.values();
This is simple and clean, but it's not the most efficient solution, if that matters in this case.
CodePudding user response:
I will leave you with a very, very simplified example.
If you choose to go with a solution similar to this one I would strongly recommend you to read about how to overwrite Equals and Hashcode: https://www.baeldung.com/java-equals-hashcode-contracts
import java.util.Map;
import java.util.HashMap;
public class HelloWorld{
public static void main(String []args) {
Entity entity1 = new Entity();
entity1.id = 1L;
Entity entity2 = new Entity();
entity2.id = 1L;
Map map1 = new HashMap<Entity, String>();
map1.put(entity1, "one");
Map map2 = new HashMap<Entity, String>();
map2.put(entity2, "two");
System.out.println(map1.get(entity1));
System.out.println(map2.get(entity1));
}
private static class Entity {
public Long id;
@Override
public boolean equals(Object o) {
return ((Entity) o).id.equals(this.id);
}
}
}
As you can see even two different references but with same id are now considered equals.
CodePudding user response:
for Any java version 8
List<String> valuesFromMap2WhenKeysAreCommon =
map1.keySet().stream().filter(map1::containsKey).map(map2::get).collect(Collectors.toList());