O. Ziggy wrote:According to the API, Treemap.headMap() returns a SortedSet which suggests that a cast is not required? I cant work out what is the correct way to use the headMap() method.
Paul Clapham wrote:
Also I notice that at lines 27 and 31 of your posted code, you create a new object and assign it to a variable, only to immediately discard that object and assign a different object to that variable in the next line of code. Why are you doing that?
O. Ziggy wrote:
Paul Clapham wrote:
Also I notice that at lines 27 and 31 of your posted code, you create a new object and assign it to a variable, only to immediately discard that object and assign a different object to that variable in the next line of code. Why are you doing that?
At lines 27 and 31, i am extracting the headSet from the TreeSet declared in line 05. The next line of code extracts the headMap from the TreeMap declared in line 16.
Paul Clapham wrote:
O. Ziggy wrote:
Paul Clapham wrote:
Also I notice that at lines 27 and 31 of your posted code, you create a new object and assign it to a variable, only to immediately discard that object and assign a different object to that variable in the next line of code. Why are you doing that?
At lines 27 and 31, i am extracting the headSet from the TreeSet declared in line 05. The next line of code extracts the headMap from the TreeMap declared in line 16.
No. At lines 27 and 31 you are creating an empty TreeSet and an empty TreeMap. It's lines 28 and 32 which actually extract data. They assign that data to the variables which used to point to the empty TreeSet and the empty TreeMap. In other words it was pointless to create those objects in the first place. So don't do that. It's wasteful, but more to the point it's confusing.
O. Ziggy wrote:The way i understood how it works is that headSet returns a new TreeSet. This is why i create a new TreeSet in line 27 and called it subset. This new Treeset will hold the result of calling the headSet method.
Do you mean i could have used a shorthand version? i.e. something like this
The return type of TreeMap.headMap is SortedMap. It is not the case that SortedMap IS-A TreeMap, so you can not cast it to TreeMap.
John Stark wrote:
The return type of TreeMap.headMap is SortedMap. It is not the case that SortedMap IS-A TreeMap, so you can not cast it to TreeMap.
Why does
work? The problem should be the same?
John
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |