Questions tagged [entryset]
32 questions
20
votes
3 answers
Stream Vs. Iterator in entrySet of a Map - Java 8
To my understanding, the following code should have print true, since both Stream and Iterator are pointing to the first element.
However, when I ran the following code it is printing false:
final HashMap map = new…

T-Bag
- 10,916
- 3
- 54
- 118
19
votes
6 answers
Iterating over a map entryset
I need to iterate over the entry set of a map from which I do not know its parameterized types.
When iterating over such entryset, why this does not compile ?
public void myMethod(Map anyMap) {
for(Entry entry : anyMap.entrySet()) {
...
…

Sergio
- 8,532
- 11
- 52
- 94
7
votes
2 answers
Printing HashMap of HashMaps : Map.Entry or java8
I have a method which returns out hashmap of hashmaps
HashMap> mapofmaps = abcd(, );
I am trying to print the the outer hashmap using the following code
for (Entry>…

Betafish
- 1,212
- 3
- 20
- 45
7
votes
4 answers
Under what scenario Map.Entry returned by map.entrySet will be NULL
I came across a code snippet which iterates over a map using its entry set and performs some action only if entry != null
As far as I know even if we don't enter anything in map map.entrySet returns an empty set and not null.
Even if I put…

Abhinav
- 1,720
- 4
- 21
- 33
6
votes
3 answers
How to get the 3 highest values in a HashMap?
I have a hashmap which is the following:
HashMap hm = new HashMap;
hm.put("a", 1);
hm.put("b", 12);
hm.put("c", 53);
hm.put("d", 2);
hm.put("e", 17);
hm.put("f", 8);
hm.put("g",…

shadwphenixx
- 71
- 1
- 6
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
3
votes
1 answer
The Set returned from HashMap.entryset(), how is it sorted?
I need to replicated the sorting I get on a set returned from the function entrySet() from the HashMap class. I don't understand how it is sorted.
The following code:
HashMap testList = new HashMap

fruktprins
- 33
- 1
- 5
2
votes
4 answers
add() method on entrySet() in Map<>
While iterating Map<> using for loop
for(Map.Entry mapEntry : myMap.entrySet()){
// something
}
I found entrySet() method returns a set of Entry
so it has add(Entry e) method
then I created a class which implements Map.Entry…

Seethend Reddy
- 23
- 1
- 4
2
votes
3 answers
From "Map>" to "Map>" using Java 8
Is there a better way to transform "Map>" to "Map>"?
Map> collectionsMap = ...
Map> listsaps =
collectionsMap.entrySet().stream()
…

Romain DEQUIDT
- 792
- 8
- 15
2
votes
1 answer
Why Java 6 overrides keySet(), entrySet() and values() interface in SortedMap
Java 5 http://docs.oracle.com/javase/1.5.0/docs/api/java/util/SortedMap.html
Java 6 https://docs.oracle.com/javase/6/docs/api/java/util/SortedMap.html
As you can see that since Java 6, these three apis are overridden. Can anyone tell me what's the…

Jiaguo Fang
- 333
- 1
- 2
- 11
2
votes
2 answers
How to make entrySet() show key-value pairs on new line?(java)
I have a long Map (int, int[] ) ;
I use map.entrySet() and it returns me the Set view of my map
Map >res = function() ;
System.out.println( res.entrySet() ) ;
i want to show every entry on new line:
[1=[1, 47, 432,…

ERJAN
- 23,696
- 23
- 72
- 146
1
vote
1 answer
Retrieving Values from having a particular property from a Map using Java 8 Stream
I have a class UserCourseEntity with a property userId
@AllArgsConstructor
@Getter
@ToString
public static class UserCourseEntity {
private String userId;
}
And I have a map with UserCourseEntity objects as values.
public final Map

Yugandhar
- 11
- 1
1
vote
1 answer
Java - Serializing Iterable> with Gson
I trying to serialize an "Iterable" from type Map.Entry with google GSON library -
and i got an empty output.
here an example of the code:
static private Iterable> getIterable(){
HashMap map = new…

Eyal leshem
- 995
- 2
- 10
- 21
0
votes
1 answer
Sorting a entrySet by keys
In my entrySet I have the next values:
name -> "someName"
nameSpace -> "someNameSpace"
version -> "someVersion"
I'm iterating over this entrySet and adding its values to a final object, my problem is that I need to add the values in the…

rasilvap
- 1,771
- 3
- 31
- 70
0
votes
0 answers
Sorting HashMap which does not maintain insertion order?
In an interview I was asked to sort HashMap by values. Even after I write such a program, it does not print the HashMap in sorted order. Can someone please explain?
public static void main(String[] args) {
HashMap x= new…

Prabhat Gaur
- 146
- 1
- 10