Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6693879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:02:13+00:00 2026-05-26T06:02:13+00:00

I want to go through a map with integer keys that are in the

  • 0

I want to go through a map with integer keys that are in the range of 14000-18000. I want to go through them and print the relative difference between them. So if there were three keys 14152 and 14153, 14159, the print output would be 0, 1, 7.

I have put my keys and values into a TreeMap, since it stores things in order.

However, with my implementation:

int dayCounter = 0;

for (Entry<Integer, String> entry : map.entrySet())
{
    builder.append(dayCounter);
    dayCounter = entry.getKey();
}

I am going through the map but know no way of getting the “previous” entry. If I was using an array I could get the (i-1)th value and subtract from i to get the relative value. Is there any way to get thie functionality with java maps?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:02:14+00:00Added an answer on May 26, 2026 at 6:02 am

    When you iterate, you know you have the smallest value first. So I would do something like this:

    Integer first = null;
    
    for(Integer i : map.keySet()) {
        if(first == null) first = i; // save the first value
        builder.append(i - first);
    }
    

    Using your example:

    import java.util.TreeMap;
    class Eggonlegs {
        public static void main(String[] args) {
            TreeMap<Integer,String> map = new TreeMap<Integer,String>();
            map.put(14152,"First");
            map.put(14153,"Second");
            map.put(14159,"Third");
    
            Integer first = null;
    
            for(Integer i : map.keySet()) {
                if(first == null) first = i; // save the first value
                System.out.println(i - first);
            }
        }
    }
    

    Results in

    c:\files>javac Eggonlegs.java
    
    c:\files>java Eggonlegs
    0
    1
    7
    
    c:\files>
    

    Now, it’s possible that’s not what you intended. Perhaps you want the difference between each node, which not what your example shows. In that case, I would leverage the Collections library like this:

    List<Integer> list = new ArrayList<Integer>(map.keySet());
    
    for(int i = 0; i < list.size(); i++) {
        if(i == 0) builder.append(0);
        else builder.append(list.get(i) - list.get(i-1));
    }
    

    Here’s an example of that, incase it’s what oyu actually intended:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.TreeMap;
    class Eggonlegs {
        public static void main(String[] args) {
            TreeMap<Integer,String> map = new TreeMap<Integer,String>();
            map.put(14152,"First");
            map.put(14153,"Second");
            map.put(14159,"Third");
    
            List<Integer> list = new ArrayList<Integer>(map.keySet());
    
            for(int i = 0; i < list.size(); i++) {
                if(i == 0) System.out.println(0);
                else System.out.println(list.get(i) - list.get(i-1));
            }
        }
    }
    

    Results in

    c:\files>javac Eggonlegs.java
    
    c:\files>java Eggonlegs
    0
    1
    6
    
    c:\files>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an STL map that I want to iterate through, and can't seem
Basically I have a list of methods that I want to iterate through, call
I want my position through GPS & from that I want to go another
Lets say I want to use the threading operator to thread a map through
I've got a map containing some keys (Strings) and values (POJOs) I want to
Is it possible to obtain values that match a range of keys in Java
I want to loop through the controls on a UserControl and set a property
I want to iterate through a table/view and then kick off some process (e.g.
I want to go through each character in a String and pass each character
I want to go through the children of an element and filter only the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.