1

I have a Map<String, Employee> and Map<String, Double>. I want Map<Employee, Double> for the keys which have the same key. E.g.

Employee emp1 = new Employee("bla1");
Employee emp2 = new Employee("bla2");
Map<String,Employee> nameEmployee = new HashMap<>();
nameEmployee.put("emp1", emp1 ); 
nameEmployee.put("emp2", emp2 ); 

Map<String,Double> nameSalary = new HashMap<>();
nameSalary.put("emp1", 1D);
nameSalary.put("emp2", 2D);

Expected Output:

Map<Employee,Double> --> [emp1:1D, emp2:2D]
Mikheil Zhghenti
  • 734
  • 8
  • 28
Garima Natany
  • 53
  • 1
  • 4
  • 2
    And what did you try yourself? What is your problem? That operation doesn't seem too complicated to me. – Amongalen Sep 22 '20 at 09:06
  • Iterate on first map, if the key exist (get) put it in the 3rd map. Try it! After try with lambda. The key is TRY first – AsfK Sep 22 '20 at 09:09
  • I am able to do it by iterating over element one by one on first map and then search if second contains that key and put it in third map but it comes with O(n2) complexity . I want to achieve it in O(n) – Garima Natany Sep 22 '20 at 09:11
  • You do not have to *search* the entire map. You have the key and can just do a lookup which is O(1) – Thiyagu Sep 22 '20 at 09:13
  • @GarimaNatany it's O(n) Because you iterate just only once, on the first map. You find the keys in the second map with HashMap.get that it's O(1). BTW, please add what you did to the question and what you want, the question will be more clearly.. – AsfK Sep 22 '20 at 09:13

0 Answers0