for example if I have a HashMap with 10 keys, but only 4 keys have a value. How can I return a SetView of these keys. I only found the Map<K,V>.keySet()-method but this method is giving me EVERY Key in this Hashmap. I only need the ones with value !=null !!! Sorry for my bad English, im German :)
How to return a Set View of Keys with existing Values?
788 Views Asked by Yusuf At
2
There are 2 best solutions below
2
Thiyanesh
On
Streams
- Iterate over entrySet
- Ignore null values
- Collect the keys
Map<String, String> map = new HashMap<>();
map.entrySet().stream()
.filter(e -> e.getValue() != null)
.map(Entry::getKey)
.collect(Collectors.toSet());
Use for loop
Set<String> keys = new HashSet<>();
for (Map.Entry<String, String> e : map.entrySet()) {
if (e.getValue() != null) {
keys.add(e.getKey());
}
}
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in HASHMAP
- How can I optimize this transposition table for connect 4 AI?
- Why is getValue preferred over !! when retrieving from a Map (NoSuchElementException vs NullPointerException)?
- 389. Find the Difference LeetCode
- How to create a HashMap that receives different types?
- Hash collisions in Golang map resolving
- hashmap not recognizing identical keys
- Why can a Range be collected into a HashMap or Vec in Rust?
- Python Algorithm for finding hidden matches
- Count the number of occurrences of each variant of an enum
- how can i iterate through two HasMap at once, i want o compare the values of two hashmaps
- Implementing generic Borrow transitively for HashMap key lookup
- Getting error while printing Hashmap using scanner classes of Java
- Hash-flooding attacks for integer hashmaps in python
- Convert a Map<T, Value> to a List<T> based on parameter of the object and value
- Avoid double hashing in HashMap?
Related Questions in SET
- mondrian3 set by aggregate
- Produce a combination of all permutations for 4 groups of data with 4 unique values contained
- How to find the difference between two python files and write output with file source information
- Is there a problem with my code? Finding null pointer Exception
- The difference between set definitions in Python
- Leetcode BFS Set insertion giving TLE (200. Number of Islands)
- set.find() not working for ordered multiset
- TinyMCE custom toolbar button to set CSS property of selected text
- Find a bit with no duplicates among multiple bits in Java
- Algorithm for comparing two sets of sets
- Order of a set in Python
- Proof on inductive sets
- Remove all elements from a set greater than a number
- Trying to prove a set to be the union of its singleton sets in Dafny
- Declaring a set of a set in Mosel
Related Questions in KEY
- Ansible prompt "No existing session" in manual executing the playbook
- Visual Studio 2022 free certificate problem. "cannot import key file " how to fix
- Why MySQL doesn't use my primary key to join my table?
- Transform a series of JavaScript object keys into array(s) when they contain numbers
- PingID 2 MFA automation with 32 Digit pairing key
- Getting list of sub-keys for a given key
- How can i ensure that when I restart my computer or I use another computer I can access the same HashiCorp Vault that I will initially setup?
- Colab + Drive: import keys + sharing notebooks
- jq: how to extract a value without a property name
- CustomScrollView with center key and mulitple slivers: Expand widgets in different directions
- AWS CLI EMR keyname doesn't recognize my access key, same region confirmed
- Errors Installiing USB Coral
- What is the meaning of keycode '\x03'?
- TecDoc catalogue API
- New Key Event Listeners JS?
Related Questions in KEYSET
- How to set key_ops in generating JWK using java script?
- Impossible to configure SharePoint 2019
- Why the method 'replace' does not work properly, using keySet for a Map?
- Access the map type fields of the static class, and use the keyset() method to obtain its key set, but it is always empty
- Convert keys to Values
- Flutter Shortcuts physical key is pressed on a different logical key
- Creating an B2C Identity Experience Framework Policy Key with Key Secret in a single Graph API call
- SpotBugs warning: Inefficient use of keySet iterator instead of entrySet iterator
- How to sort the keySet() of a TreeMap<String, Boolean> with keys containing number?
- How to pull random string from a keySet?
- How to return a Set View of Keys with existing Values?
- Compare builtin `setOf` with Android's `keySet`?
- keySet() method in HashMap could be terser
- How is the underlying keyset of a Hashmap implemented so that add method fails?
- About HashMap collection's keySet() method
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Use the keySet() method, and loop through each Entry in the Set, checking the value of the Entry each time. If this "value" is null, then we remove it from the Set.
The resulting Set "entrySet" is what you're looking for