Questions tagged [keyset]

76 questions
53
votes
2 answers

lua: retrieve list of keys in a table

I need to know how to retrieve the key set of a table in lua. for example, if I have the following table: tab = {} tab[1]='a' tab[2]='b' tab[5]='e' I want to be retrieve a table that looks like the following: keyset = {1,2,5}
ewok
  • 20,148
  • 51
  • 149
  • 254
38
votes
5 answers

java collections - keyset() vs entrySet() in map

I put a string array elements is a map where elements of string array is key and frequency of word is value, e.g.: String[] args = {"if","it","is","to","be","it","is","up","me","to","delegate"}; then the map will have entries like [ if:1, it:2…
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
17
votes
7 answers

Java Iteration over a keySet

I have the following Java code: public void myMethod (final Map pFeatureGroupsFromPackage) { final Set keys = pFeatureGroupsFromPackage.keySet(); for (final String key : keys) { tmpList = (List)…
Luixv
  • 8,590
  • 21
  • 84
  • 121
16
votes
3 answers

Avoiding map.get(key) method

I have the following code but i saw that retrieving values from a Map while iterating over the Map keys with keySet() is a mistake even with findBugs i get the warning WMI_WRONG_MAP_ITERATOR for(String elementId : mapElements.keySet()){ …
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
9
votes
5 answers

Are keySet entries of a WeakHashMap never null?

If I iterate over the key set of a WeakHashMap, do I need to check for null values? WeakHashMap> hm = new WeakHashMap>(); for ( MyObject item : hm.keySet() ) { if ( item !=…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
9
votes
3 answers

Unsupported add/addAll operations for Map.keySet()

Regarding the Map interface: Why does keySet() return a Set that supports the remove operation but doesn't support add() and addAll() operations?
Hicham Moustaid
  • 773
  • 6
  • 13
8
votes
1 answer

why map.keyset() returns set view but map.values() returns collections in Java?

This question is more about design implementation by Java developers. I want to know (if there any vital reason that I cannot think of) why Keyset() returns a set-view but values() returns Collection-view. why not return Values() as a ValueSet with…
brain storm
  • 30,124
  • 69
  • 225
  • 393
6
votes
2 answers

keySet() method in HashMap could be terser

JDK 8 on mac OS, looking at following code from HashMap.java: public Set keySet() { Set ks = keySet; if (ks == null) { ks = new KeySet(); keySet = ks; } return ks; } Any changes…
Freddy
  • 63
  • 4
5
votes
2 answers

How to use keySet() to retrieve a set of keys within a HashMap, loop over it and find its count for each key?

I am nearing the end of my assignment and one of the last thing that I have been instructed t do is to: Use keySet() to retrieve the set of keys (the String part of the mapping). Loop over this set and print out the word and its count. I have used…
Alan
  • 117
  • 1
  • 8
5
votes
3 answers

Java TreeMap contains a key but a containsKey call returns false (even the key is exactly the same unchanged object)

Why is it possible to loop the keySet of a TreeMap and getting a .containsKey == false? for (Object thisObject : map.keySet()) { if (!map.containsKey(thisObject)) { System.out.println("This line should be never reached."); …
4
votes
1 answer

How can HashMap.keySet() return a view of keys?

Here is the keySet() function inside java.util.HasMap class : public Set keySet() { Set ks = keySet; if (ks == null) { ks = new KeySet(); keySet = ks; } return ks; } In the comment, it says this…
Bonsaisteak
  • 151
  • 1
  • 8
4
votes
1 answer

Performance - Inefficient use of keySet iterator instead of entrySet iterator

This piece of code throws the error This method accesses the value of a Map entry, using a key that was retrieved from a keySet iterator. It is more efficient to use an iterator on the entrySet of the map, to avoid the Map.get(key) lookup. Kindly…
Arun
  • 43
  • 3
4
votes
1 answer

JSONObject keySet() function not there?

Very confused. New project, add this code: import org.json.JSONObject; JSONObject jsonObject = new JSONObject(); jsonObject.keys(); jsonObject.keySet(); .keys() method resolves as it should. .keySet() method highlights red with AndroidStudio…
Alexandre G
  • 1,655
  • 20
  • 30
4
votes
1 answer

HashMap's KeySet, EntrySet, and values are null while table is not empty

I'm reading a data.ser file to fill some HashMaps with saved data. Although the Map has the values I'm supposed to have, the KeySet, EntrySet, and values are all null. for example, Hashmap "Employees" looks like this when I inspect it in debug…
Roi811
  • 67
  • 1
  • 6
3
votes
0 answers

Why keySet() in ConcurrentHashMap returns KeySetView and not just Set like other Map implementations?

Why keySet() in ConcurrentHashMap returns KeySetView and not just Set (since Java SE8)? ConcurrentHashMap.KeySetView keySet() KeySetView - A view of a ConcurrentHashMap as a Set of keys, in which additions may optionally be enabled…
Code Complete
  • 3,146
  • 1
  • 15
  • 38
1
2 3 4 5 6