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 8061643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:24:21+00:00 2026-06-05T10:24:21+00:00

I wanna sort a collection of dates value pair. My key is Date and

  • 0

I wanna sort a collection of dates value pair. My key is Date and the value is a String. So I selected a TreeMap.

Now,

  1. Is the following iterator is sorted according similar to the TreeMaps key. I tried few loops but still have a doubt

    Iterator<Date> iter = policyMap.keySet().iterator();
    
  2. Is there any way to get the next key without incrementing the iterator’s index.

  3. Is there any equalant way than this policyMap.higherKey(cpDate)) before java 6.

Finally I ashamed of my self that I tried for following too.

TreeMap<Date, String> policySubMap = 
new TreeMap<Date, String>policyMap.tailMap(cpDate));
policySubMap.remove(policySubMap.firstKey());
System.out.println(" | amount > " + policySubMap.firstKey());

This is my complete code :

public void controller(){

TreeMap<Date, String> policyMap = new TreeMap<Date, String>();
Calendar cal = Calendar.getInstance();

policyMap.put(addDate(cal, 2).getTime(), "Amount is 10");
policyMap.put(addDate(cal, 10).getTime(), "Amount is 10");
policyMap.put(addDate(cal, -10).getTime(), "Amount is -10");
policyMap.put(addDate(cal, 11).getTime(), "Amount is 11");
policyMap.put(addDate(cal, -11).getTime(), "Amount is -11");
policyMap.put(addDate(cal, -12).getTime(), "Amount is -12");

Iterator<Date> iter = policyMap.keySet().iterator();

while (iter.hasNext()) {
    Date cpDate = iter.next();
    System.out.print("From "+cpDate + " to " + policyMap.get(cpDate));
//      if(iter.hasNext())System.out.println(" | amount > " + policyMap.higherKey(cpDate)); // This is not supporting in before java 6
        if(iter.hasNext()){
            TreeMap<Date, String> policySubMap = new TreeMap<Date, String>(policyMap.tailMap(cpDate));
            policySubMap.remove(policySubMap.firstKey());
            System.out.println(" | amount > " + policySubMap.firstKey());
        }

    else System.out.println("Checking date");

    }
}

public Calendar addDate(Calendar cal, int amount) {
    cal.add(Calendar.DATE, amount);
    return cal;
}
  • 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-06-05T10:24:23+00:00Added an answer on June 5, 2026 at 10:24 am
    1. Yes

    2. No. You can use a second iterator, or mroe efficiently save the previous value.

    3. You can use

      Date nextKey = treeMap.tailMap(new Date(date.getTime()+1)).firstKey();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanna get the string representation of a variable. For example, (def my-var {})
wanna ask for your opinion. What would be the best object (Array, List<>, Collection,...)
I wanna validate date which can be either in short date format or long
Assume we have the following data in a SQL Server table (sorted by name)
I am an Android newbie and I wanna create a simple mp3 player. Now,
i wanna sort my table with jQuery Plugin TableSorter . So i get this
I wanna build a gallery, but the my problem is how to load the
i wanna do a single selection in my table view here is my code
i wanna creat a website for downloading some files (pdf,word,xls, such as these files),
I wanna upload/update file by System.IO.FileStream in Google Documents List API(C#)? I use two

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.