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?
786 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
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in HASHMAP
- Borrow mutable and immutable reference in the same block
- How entrySet() works internally in HashMap?
- Java HashMap, hashCode() equals() - how to be consistent with multiple keys?
- How to sort the string on the basis of the frequency which is a part of a string?
- How to get the index of the hash map array list in android?
- java - how to create custom hashtable iterator?
- How to convert a string to a key for hash table
- How to use hash tables when amount of slots is unknown?
- How to display the value of a HashMap key when user clicks autocom
- Iterating through a array of hashes in a hash that has multiple indexes
- combine two LinkedHashMap<>() to one Map
- How to traverse and update a Collection like HashMap in java dynamically?
- Load Dictionary Text File Into Java
- Why not use hash table with overflow area?
- Storing objects of different classes inside an ArrayList
Related Questions in SET
- Removing duplicates from arraylist using set
- Order-independent Hash Algorithm
- How to count 2 different duplicate values in array - Swift
- CMD specifying columns to save?
- comparison of two sets when repeated in r
- How to use set(data structure) in mongodb console?
- Reversing logic of a product-country mapping
- Storing data in sorted manner in a HashSet
- Generate all combinations of strings and their substrings in a set -- python
- How to access class data in a set of pointers to that class
- Scala set element uniqueness: What to implement for comparison of user defined classes?
- java.util.Set add and remove method signature difference
- retrieve all the smallest strings from a set in python
- Setting a String Variable in SSIS
- Setting a robocopy log file to a variable
Related Questions in KEY
- Android Signature key differences between Old and New PC
- Java HashMap, hashCode() equals() - how to be consistent with multiple keys?
- Kaltura account settings error
- fetch json value with no key using jquery
- C++ Custom std::map<> key class causing memory violation
- How do I use php file() to access an array and echo specific keys?
- constructing key by bit shifting 3 integers in C
- How to get keystrokes with java outside of frames
- Keys in Perl hash disappeared
- Master Array compare csv
- Covering index and getting rid of Key Lookup
- How to add key pairs to object literals using loop--WITHOUT overriding existing entries?
- PHP Convert Array Including Key Rename
- Python: How to append to existing key of a dictionary?
- Python key error - for key in dictionary: dictionary[key]
Related Questions in KEYSET
- How to test collections in Junit (Java)
- Method doesn't work for large data set
- How do I list print out all the keys currently stored in my HashMap mapping people to their addresses
- Why Java HashMap get(key) works faster when keys are read using same HashMap's Iterator than when keys are read using a Set's Iterator?
- java collections - keyset() vs entrySet() in map
- why map.keyset() returns set view but map.values() returns collections in Java?
- mapping keys and values from a cell array of strings
- Impossible to configure SharePoint 2019
- Map.keySet() and Set.addAll throwing NullPoniterException
- Java TreeMap contains a key but a containsKey call returns false (even the key is exactly the same unchanged object)
- Websphere keyset not get the latest key
- How to sort the keySet() of a TreeMap<String, Boolean> with keys containing number?
- Convert keys to Values
- How is the underlying keyset of a Hashmap implemented so that add method fails?
- Compare builtin `setOf` with Android's `keySet`?
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