• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Trying to iterate my TreeMap using navigableKeySet method. Could someone review and debug for me?  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


First of all, I am getting an InputMismatchException thrown right after Scanner takes in the first input.
Secondly, is my logic alright? I know the standard method of iterating using the entrySet() method but I am just trying to explore a bit and see if this works as well.
Thank you and I appreciate all the help provided to me.
 
Rancher
Posts: 1915
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 61690
192
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have obviously worked out that you can use an Integer as the “K” in a TreeMap without any special precautions because Integer implements Comparable<Integer>.

Karla Carr wrote:. . . First of all, I am getting an InputMismatchException thrown right after Scanner takes in the first input.

. . . and what was that input? Look here for an explanation why I think you are getting that Exception after the second lot of inputs.

Secondly, is my logic alright? I know the standard method of iterating using the entrySet() method but I am just trying to explore a bit and see if this works as well.

Yes, a for‑each loop is a good way to iterate such a Set, provided:-
  • 1: You want to iterate the whole Set.
  • 2: You want to iterate the Set in its normal order (not backwards).
  • 3: You are using the Set in a read‑only fashion.
  • You can use anything implementing the Iterable interface as a target for a for‑each loop. The order of iteration is determined by its Iterator, which is returned from its iterator() method. That is polymorphic, since it is an overridable instance method, so you might get a different order of iteration with a hash map

    the Iterable documentation wrote:Implementing this interface allows an object to be the target of the enhanced for statement (sometimes called the "for-each loop" statement).

    In fact, I think a for‑each loop is the “standard way” to iterate a set. The only way I could think to improve that is to use one of the Java8 default methods:-orOr even, mySet.forEach(System.out::println);

    Thank you and I appreciate all the help provided to me.

    That's a pleasure
     
    Campbell Ritchie
    Marshal
    Posts: 61690
    192
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you iterate the entry set, you may get a different order of iteration.
     
    Ranch Hand
    Posts: 3168
    18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:If you iterate the entry set, you may get a different order of iteration.



    I should hope not.  From the javadoc for java.util.SortedMap:

    The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods).

     
    Campbell Ritchie
    Marshal
    Posts: 61690
    192
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see I was mistaken. Sorry.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!