Reputation: 1864
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?
Upvotes: 0
Views: 157
Reputation: 1864
I didn't explain my question well. Sorry guys. I wanted a cumulative running total. This is what I did by combining your answers:
int hold_previous_key = 0;
int difference = 0;
int running_total = 0;
for (int key : map.keySet())
{
if (hold_previous_key != 0)
{
difference = key - hold_previous_key;
running_total += difference;
builder.append(key + " - " + hold_previous_key + " = " + difference + " RUNNING TOTAL = " + running_total + "; ");
}
else
{
// print 0 for the first date in the data set
builder.append("first val = 0" + "; ");
}
hold_previous_key = key;
}
Upvotes: 0
Reputation: 82599
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>
Upvotes: 2
Reputation: 33575
Why don't you hold it in a separate variable?
//Warning : Notepad coding.
int hold_prev_key = -1;
for(int key : map.keySet())
{
if(hold_prev_key != -1)
builder.append(key - hold_prev_key);
hold_prev_key = key;
}
Upvotes: 2
Reputation: 178521
simplest and fastest way I think: you can store the last entry in a temp variable, and use it only on next iteration. Don't forget to modify it at the end of each iteration.
Upvotes: 4