About 628,000 results
Open links in new tab
  1. java - How do you search through a map? - Stack Overflow

    Nov 11, 2015 · List<String> matches = new ArrayList<String>(); for (Map.Entry<String,String> entry : map.keys()) { if (targetValue.equals(entry.getValue())) { matches.add(entry.getKey()); } } Better perhaps would be to use a second map for the reverse mapping, or a bidirectional map from a third-party library.

  2. java - Searching for a Value in a HashMap - Stack Overflow

    In java 8 you could use something like this: .entrySet() .stream() .filter(entry -> entry.getValue().contains("Barry")) .sorted(Map.Entry.comparingByKey()) .findFirst(); This get all the entries from the map, filters them based on the …

  3. Check if Particular Value Exists in Java HashMap

    Jul 30, 2021 · The different approaches to check for the existence of a particular Value in a Java HashMap are: Using the built in containsValue() method of the HashMap class; Creating a map from the entries of the HashMap and then iterating through the Values; Creating an ArrayList from the Values of the HashMap and then iterating through this list; Approach 1 :

  4. java - Search a value for a given key in a HashMap - Stack Overflow

    Jul 20, 2016 · Map<Integer, String> hashMap = new HashMap<Integer, String>(); ... int x = scan.nextInt(); value = hashMap.get(x); EDIT or without generics, like said in the comments: int x = scan.nextInt(); value = (String) hashMap.get(new Integer(x));

  5. Map Interface in Java - GeeksforGeeks

    Jan 9, 2025 · There are two interfaces for implementing Map in Java. They are Map and SortedMap, and three classes: HashMap, TreeMap, and LinkedHashMap. This method is used in Java Map Interface to clear and remove all of the elements or mappings from a …

  6. How to Find an Element in a Map with Java - Java Guides

    Finding an element in a Map in Java can be done using various methods provided by the Java Collections Framework. You can use containsKey and get to find an element by its key, containsValue to find an element by its value, and iterate over the entry set to find an entry based on a specific condition.

  7. How to search a value in hashmap in java? - W3schools

    How to search a value in hashmap in java? We can use containsValue () method to search a value in hashmap in java. containsValue (Object v): Returns true if this map contains specified value otherwise returns false. Syntax:

  8. Java HashMap Search and Sort - DZone

    Mar 16, 2017 · Finding a key in a HashMap is quite simple. The HashMap API provides the containsKey () method, which tells you whether the key exists. Finding a value is also easy given the method containsValue...

  9. Iterate Over a Map in Java - Baeldung

    Dec 16, 2024 · In this tutorial, we’ll look at the different ways of iterating through the entries of a Map in Java and write a benchmark test to determine the most efficient method. Simply put, we can extract the contents of a Map using entrySet (), keySet (), or values (). Since these are all sets, similar iteration principles apply to all of them. 2.

  10. A Guide to Java HashMap - Baeldung

    Jan 16, 2025 · If we want to find a specific element in a list, the time complexity is O(n) and if the list is sorted, it will be O(log n) using, for example, a binary search. The advantage of a HashMap is that the time complexity to insert and retrieve a value is O(1) on average.

  11. Some results have been removed
Refresh