Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Basic treemap loop

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Basic treemap loop

    So I want to make a for loop for a tree map to get the highest number right now I have:


    public void findRichestInList()
        {
            bank = new TreeMap<String, Integer>();
            bank.put("Dave",139);
            bank.put("Mary",99);
            for(.......
    }

    I am not sure how to write the loop to check each record.
    How would I do this?
    Also would it be easier to use a hashMap instead of a treemap?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Basic treemap loop

    A HashMap won't preserve order.

    A TreeMap preserves the order of its keys, in this case the String names. So using TreeMap doesn't really get you anything either, if you're trying to get the order of the numbers.

    You can get a Set containing all of the keys of the Map by calling keySet(). Then you can use that to loop over all of the values, searching for the largest one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Basic treemap loop

    Well with the example I am doing I have to do it with either a treemap or a hashmap. I just need to know what I would put inside the brackets for a for loop so it will check through each record.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: Basic treemap loop

    Iterate over the key Set. See The Map Interface (The Java™ Tutorials > Collections > Interfaces) for some examples.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Trunk Monkeey (May 2nd, 2011)

Similar Threads

  1. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 09:12 AM
  2. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 04:10 PM
  3. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 10:52 AM
  4. Student TreeMap
    By raphytaffy in forum Algorithms & Recursion
    Replies: 6
    Last Post: March 1st, 2010, 11:11 PM
  5. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM

Tags for this Thread