• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

Accessing keys in Map<Long, Double>

Dear all,

I'm using the store in map method to create a map with a long key with 30min timesteps and a double value.  I'm using a long and the Calendar object instead of the Date.  It works and stores the data and prints to console.  See first code block and bottom of post for output to console.

My issue the map isn't returning the correct information,   See unit test below.    The aim of the unit test is to retrieve date information and timestep for the given long key. I'm not able to see the issue.

Thanks

 An example of the output is
Timestep: Fri Mar 13 01:00:00 EST 2015 13 2 2015 1 0 0
Timestep: Fri Mar 13 04:30:00 EST 2015 13 2 2015 4 30 0
Timestep: Fri Mar 13 08:00:00 EST 2015 13 2 2015 8 0 0
Timestep: Fri Mar 13 11:30:00 EST 2015 13 2 2015 11 30 0
Timestep: Fri Mar 13 15:00:00 EST 2015 13 2 2015 15 0 0
Timestep: Fri Mar 13 18:30:00 EST 2015 13 2 2015 18 30 0
Timestep: Fri Mar 13 22:00:00 EST 2015 13 2 2015 22 0 0
Timestep: Fri Mar 13 05:30:00 EST 2015 13 2 2015 5 30 0
Timestep: Fri Mar 13 09:00:00 EST 2015 13 2 2015 9 0 0
Timestep: Fri Mar 13 12:30:00 EST 2015 13 2 2015 12 30 0
Timestep: Fri Mar 13 16:00:00 EST 2015 13 2 2015 16 0 0
Timestep: Fri Mar 13 19:30:00 EST 2015 13 2 2015 19 30 0
Timestep: Fri Mar 13 23:00:00 EST 2015 13 2 2015 23 0 0
Timestep: Fri Mar 13 13:00:00 EST 2015 13 2 2015 13 0 0
Timestep: Fri Mar 13 16:30:00 EST 2015 13 2 2015 16 30 0
Timestep: Fri Mar 13 20:00:00 EST 2015 13 2 2015 20 0 0
Timestep: Fri Mar 13 23:30:00 EST 2015 13 2 2015 23 30 0

start = new SimpleDateFormat("dd/MM/yyyy").parse("13/03/2015")
				.getTime();
endDate = new SimpleDateFormat("dd/MM/yyyy").parse("14/03/2015")
				.getTime();
public Map<Long, Double> storeInMap(long start, long end) {
		Map<Long, Double> map = new HashMap<>();
		long timeMS = start;
		while (timeMS < end) {
			// Store the data in the map
			map.put(timeMS, 0.0); // 0.0 default value

			// Move ahead 30 mins in milliseconds
			timeMS += TimeUnit.MILLISECONDS.convert(30L, TimeUnit.MINUTES);
			System.out.println(new Date(timeMS));
		}
		return map;
	}

Open in new window


Unit test.

@Test
	public void testFindTimestepFromMap() {
		map = util.storeInMap(startDate, endDate);
		for (Map.Entry<Long, Double> entry : map.entrySet()) {
			Long timestep = entry.getKey();
			Calendar calt = Calendar.getInstance();
			calt.setTimeInMillis(timestep);
			System.out.println("Timestep: " + calt.getTime() + " "
					+ calt.get(Calendar.DAY_OF_MONTH) + " "
					+ calt.get(Calendar.MONTH) + " " + calt.get(Calendar.YEAR)
					+ " " + calt.get(Calendar.HOUR_OF_DAY) + " "
					+ calt.get(Calendar.MINUTE) + " "
					+ calt.get(Calendar.SECOND));
		}

	}

Open in new window



Output of store in map method:
Fri Mar 13 00:30:00 EST 2015
Fri Mar 13 01:00:00 EST 2015
Fri Mar 13 01:30:00 EST 2015
Fri Mar 13 02:00:00 EST 2015
Fri Mar 13 02:30:00 EST 2015
Fri Mar 13 03:00:00 EST 2015
Fri Mar 13 03:30:00 EST 2015
Fri Mar 13 04:00:00 EST 2015
Fri Mar 13 04:30:00 EST 2015
Fri Mar 13 05:00:00 EST 2015
Fri Mar 13 05:30:00 EST 2015
Fri Mar 13 06:00:00 EST 2015
Fri Mar 13 06:30:00 EST 2015
Fri Mar 13 07:00:00 EST 2015
Fri Mar 13 07:30:00 EST 2015
Fri Mar 13 08:00:00 EST 2015
Fri Mar 13 08:30:00 EST 2015
Fri Mar 13 09:00:00 EST 2015
Fri Mar 13 09:30:00 EST 2015
Fri Mar 13 10:00:00 EST 2015
Fri Mar 13 10:30:00 EST 2015
Fri Mar 13 11:00:00 EST 2015
Fri Mar 13 11:30:00 EST 2015
Fri Mar 13 12:00:00 EST 2015
Fri Mar 13 12:30:00 EST 2015
Fri Mar 13 13:00:00 EST 2015
Fri Mar 13 13:30:00 EST 2015
Fri Mar 13 14:00:00 EST 2015
Fri Mar 13 14:30:00 EST 2015
Fri Mar 13 15:00:00 EST 2015
Fri Mar 13 15:30:00 EST 2015
Fri Mar 13 16:00:00 EST 2015
Fri Mar 13 16:30:00 EST 2015
Fri Mar 13 17:00:00 EST 2015
Fri Mar 13 17:30:00 EST 2015
Fri Mar 13 18:00:00 EST 2015
Fri Mar 13 18:30:00 EST 2015
Fri Mar 13 19:00:00 EST 2015
Fri Mar 13 19:30:00 EST 2015
Fri Mar 13 20:00:00 EST 2015
Fri Mar 13 20:30:00 EST 2015
Fri Mar 13 21:00:00 EST 2015
Fri Mar 13 21:30:00 EST 2015
Fri Mar 13 22:00:00 EST 2015
Fri Mar 13 22:30:00 EST 2015
Fri Mar 13 23:00:00 EST 2015
Fri Mar 13 23:30:00 EST 2015
Sat Mar 14 00:00:00 EST 2015
0
AndyC1000
Asked:
AndyC1000
  • 2
  • 2
2 Solutions
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm assuming that in your "example of the output" you haven't actually given the entire output. It just looks like the problem is that you are expecting the output to be in a certain order but it is in a random order.

That is because you are putting these entries into a HashMap which does NOT guarantee an iteration order. Simply change the first line in your storeInMap method (line 6 of the code block above) to the following...

		Map<Long, Double> map = new TreeMap<>();

Open in new window


The TreeMap class will return the Map entries in order based on the natural order of the keys in the map, which should be what you want. Another option is to use a LinkedHashMap which keeps it's order based on the order that you insert the entries. Both would work in this case because insertion order is the same as the natural order, but if you were to change your code that may not be the case and therefore you would need to decide exactly what functionality that you are after.
0
 
dpearsonCommented:
Is your concern that they're not coming back in order?

That's the nature of a HashMap - the entrySet comes back in hashCode order, which is essentially random.

If you want them to be sorted change the line where you create the HashMap to:

            Map<Long, Double> map = new TreeMap<>();

Doug
0
 
dpearsonCommented:
Oops - ships passing in the night :)
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
;) No worries
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now