Home > Blockchain >  Find in a list if there are elements present in a HashMap
Find in a list if there are elements present in a HashMap

Time:06-21

I have this list of numbers:

"123"
"321"
"435"
"128"

And I have a hashMap with these values:

"User 1" -> 700
"User 2" -> 123
"User 3" -> 321

I want to get the elements from my list that has the same key value in the HashMap. I other words, with this example I want to get from my list these elements:

"123"
"321"

Because these are the elements that are present in my HashMap. So far this is my try, but I am getting false for all of this:

      getPhoneInformation().stream().forEach(phoneNumber -> {
            phoneNumber.stream().forEach(phone -> {
                assignedToMap.keySet().toString());
                if(phone.getPhoneNumber().contains(assignedToMap.keySet().stream().toString())) {
                    System.out.println("SAME_NUMBER");
                    
                }
            });
        });

CodePudding user response:

How about

public static void solution() {
    // just your sample data
    final List<String> numberList = List.of(
            "123",
            "321",
            "435",
            "128"
    );

    final Map<String, Integer> theMap = Map.ofEntries(
            Map.entry("User 1", 700),
            Map.entry("User 2", 123),
            Map.entry("User 3", 321)
    );

    // here comes the magic
    List<String> result = numberList.stream()
            .filter(num -> theMap.containsValue(Integer.parseInt(num)))
            .collect(Collectors.toList());

    // dump the results to STDOUT
    result.forEach(System.out::println);
}

I'm gonna skip any explanation since the "magic" is actually one line of code and I hope pretty self-explanatory, too.

CodePudding user response:

There are several ways to solve this, here's one.

First, let's start with your sample data:

List<Integer> numbers = Arrays.asList(123, 321, 435, 128);

Map<String, Integer> map = new HashMap<>();
map.put("User 1", 700);
map.put("User 2", 123);
map.put("User 3", 321);

With that setup, this code would do what you're looking for:

1. Collection<Integer> numbersInMap = map.values();
2. List<Integer> numbersInBoth = new ArrayList<>();
3. for (Integer number : numbers) {
4.     if (numbersInMap.contains(number)) {
5.         numbersInBoth.add(number);
6.     }
7. }
8. System.out.println(numbersInBoth);

A few points:

  • line 1: As a first step, get all of the numbers in the map – 700, 123, 321 – by asking the map for all the values it contains by calling values():
  • line 2: create an empty list that you can use to gather your results; each time you find a number that's in both the map and the list, add it to numbersInBoth
  • line 3: iterate over each number in the numbers list. As an alternative, you could also iterate over each number in numberInMap collection, and then adjust the loop body accordingly (look for matches in the list instead of the map).
  • line 4: check if the number from "list" is also in the "map" numbers
  • line 5: if in both, add it to numbersInBoth
  • line 8: print out numbersInBoth, this is the overlapping set of results

Running that code produces this output:

[123, 321]

If you want to use streams, replace lines 2-7 with this:

List<Integer> numbersInBoth = numbers.stream()
        .filter(numbersInMap::contains)
        .collect(Collectors.toList());
  • Related