previous value in TreeMap

Posted on 2008-11-15
Last Modified: 2012-05-05
Hi everybody.
Trivial question on TreeMap: how can I get the value corresponding to the key immediately preceding a given key?
Question by:perissf
    LVL 86

    Expert Comment

    You can't ;-) That's not how a Map works - even a TreeMap. If you want to do that, you need to do make the keys into a List

    Author Comment

    but...I was told that the TreeMap had sorted I thought that with an iterator I could get the previous key and then ask for the corresponding value...
    Also, you can assume that the keys are already sorted, since I put them starting from the older and then proceeding with the newer...(keys are Dates).
    If this does not work, then I'd need some help in order to:
    -get the keys
    - sort them
    -get the previous key
    Thanks mate
    LVL 86

    Accepted Solution

    You could do something like the following:
        public static void prev() {
            Map<Integer, String> sorted = new TreeMap<Integer, String>();
            for(int i = 0;i < 10;i++) {
                sorted.put(i, Integer.toString((int)(Math.random() * Integer.MAX_VALUE), 36));
            List<Integer> keys = new ArrayList<Integer>(sorted.keySet());
            String val4 = sorted.get(4);
            System.out.printf("Value with key 4 is %s\n", val4);
            String val3 = sorted.get(keys.get(keys.indexOf(4) - 1));
            System.out.printf("Value with key 3 is %s\n", val3);

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    The viewer will learn how to implement Singleton Design Pattern in Java.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now