entryset

Stream versus Iterator in entrySet of a Map

大城市里の小女人 提交于 2019-12-05 12:52:19
问题 To my understanding, the following code should print true , since both Stream and Iterator are pointing to the first element. However, when I run the following code it is printing false : final HashMap<String, String> map = new HashMap<>(); map.put("A", "B"); final Set<Map.Entry<String, String>> set = Collections.unmodifiableMap(map).entrySet(); Map.Entry<String, String> entry1 = set.iterator().next(); Map.Entry<String, String> entry2 = set.stream().findFirst().get(); System.out.println

Stream versus Iterator in entrySet of a Map

我的梦境 提交于 2019-12-03 22:36:20
To my understanding, the following code should print true , since both Stream and Iterator are pointing to the first element. However, when I run the following code it is printing false : final HashMap<String, String> map = new HashMap<>(); map.put("A", "B"); final Set<Map.Entry<String, String>> set = Collections.unmodifiableMap(map).entrySet(); Map.Entry<String, String> entry1 = set.iterator().next(); Map.Entry<String, String> entry2 = set.stream().findFirst().get(); System.out.println(entry1 == entry2); What is the reason of the different behavior? Both entries are referring to the same

Why Java 6 overrides keySet(), entrySet() and values() interface in SortedMap

一个人想着一个人 提交于 2019-12-02 03:53:13
问题 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 purpose of making such a change? 回答1: The methods had to be overridden in order to have their own Javadoc. Other reasons why you would declare a method in a subinterface are the ability to restrict the return type or to add annotations, but they

Iterating over a map entryset

戏子无情 提交于 2019-11-27 23:19:43
问题 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()) { ... } } but this compile: public void myMethod(Map anyMap) { Set<Entry> entries = anyMap.entrySet(); for(Entry entry : entries) { ... } } and this also compiles (I cannot use this one since I do not know the types of the map): public void myMethod(Map<String,