Solved

# Range Value

Posted on 2006-04-06
275 Views
Hi,
Hi,

I have array d[i][j] that stores values. By using map, i am able to get the number of times the values occur.
For example:
Values    How many times occur
3.6667 =2
3.52   =3
3.51 =1

Is there a way if I want to find the occurances in range. For example,

how many times i get the value between and including 3.5 and 3.6 . Is there any other function or method to use...

For example, for the example above,
3.5 &&3.6 is 4 times

0
Question by:Jasbir21

LVL 86

Expert Comment

You need to coalesce your keys using another map. Start with them in a SortedMap (<Double,Integer>) and then coalese them into SortedMap<BigDecimal, Integer> or  SortedMap<String, Integer>
0

LVL 86

Expert Comment

How you implement it depends somewhat on the type of the key though. What is it?
0

LVL 92

Assisted Solution

use a loop to go thru your values checking whether each is within the range
if it is the incremnent a counter for the number in the range

you can improve on this if you already have a map with the number of times each appears
in this case you can go thru the keys in your map (the values) and if in the range you inclement your counter by the number of times that value appeared.

let me know if u have any questions :)
0

LVL 14

Expert Comment

This can be done by sorting all the values, preprocessing and then doing binary search
on the both end values of the interval and then you have the number of values that fall in between
0

LVL 14

Expert Comment

another way is to use binary-search-tree
do a simple recursive traversal of the tree to find the starting value (call it S)
from the right child of S do an inroder traversal to obtain the data in ascending order
count the nodes you visit, until you reach the end of interval
this will eliminate the sort overhead (in a 1 dimensional array) but would also cause another overhead (traversal).

my first comment is good where you insert values only once but get range occurances many times
it costs only two binarysearch that is O(Log n)
then you only need to subtract the start index from the end index.
0

LVL 86

Expert Comment

>>This seems like a homework and it most likely is.

I didn't think so, as jasbir has been around for some time AFAICR
0

LVL 20

Expert Comment

I saw this but he also never made even an attempt to do anything here. :) Give him an idea so he can start implementing and then help on this :)
0

Author Comment

This program takes the number of years and iterations to be runned. It then calculates the scores and stores them in the 2 dimensional score array based on the number of iterations and years.

For example,if the program is runned five times(iterations=3 and years=2), this could be the output.

score[year][iteration]

Iterations 1   Iteratons 2   Iterations 3
Years   1  2.666667       2.66667         2.66667
Years  2   3.2224           3.2221          3.4

so what i need to do is to find the percentage for the range based on years
For example, for year 1 , how many times got value between 2.6 to 2.7

For example, for year 2,how many times, got value between 3.2 to 3.3, 3.3 to 3.4

I just realized that the map below takes all the value in and does not consider it based on years..Is there a way to make the map think in years.

I am posting a new question for map first coz without getting the map correct..i cant sorrt..PLEASE look AT THIS..THANKS A LOT.

Map <Double,Integer> map=new HashMap<Double,Integer>();

for(int i=0;years+1;i++)
{
for(int j=0;j<iterations;j++){
Integer val=map.get(score[i][j]);
if(val==null)
map.put(score[i][j],1);

else
{
int newVal=val+1;
map.put(score[i][j],newVal);
}
}
}
0

Author Comment

0

Author Comment

Thanks for helping on the maps question...

Now my map is like this:

ArrayList<Map<Double,Integer>> maps=new ArrayList<Map<Double,Integer>>(years+1);

for(int i=0;i<years+1;i++)
{
Map<Double,Integer> map = new HashMap<Double,Integer>();

for(int j=0;j<iterations;j++)
{
Integer val = map.get(score[i][j]);
if(val==null)
map.put(score[i][j],1);

else
{
int newVal=val+1;
map.put(score[i][j],newVal);
}
}
}

How to use sorted map  here ..Pls help..

0

LVL 92

Expert Comment

what did u want  to do with the sorted map?
0

LVL 86

Expert Comment

>>How to use sorted map  here ..Pls help..

You would need:

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

LVL 86

Accepted Solution

But to normalise your keys to get the ranges, you should use

Map<BigDecimal, Integer> map = new TreeMap<BigDecimal, Integer>();
map.put(BigDecimal.valueOf(score[i][j]).setScale(1, RoundingMode.FLOOR), newVal);
0

Author Comment

just saw this...
0

Author Comment

>>>what did u want  to do with the sorted map?

actually, now i have my map's scores based on years..
meaning map 1 would refer to scores for year 1,

map 2 would refer to scores for year 2..

however, for the scores in each map, i need find range

for example: between 3.2 and 3.3 ,how many times...
0

Author Comment

ArrayList<Map<BigDecimal,Integer>> maps =new ArrayList<BigDecimal,Integer>>(years+1);

for (int i=0;i<years+1;i++)
{

Map<BigDecimal,Integer>map =new TreeMap<BigDecimal,Integer>();

for(int j=0;j<iterations;j++)
{
Integer val = map.get(score[i][j]);

int newVal=val+1;
map.put(BigDecimal.valueOf(score[i][j]).setScale(1, RoundingMode.FLOOR), newVal);

}
}

I get error: cannot find symbol Floor

0

Author Comment

sorry..its i got errror : cannot find symbol BigDecimal, i hope it is ok if the arraylist of map is also BigDecimal
0

LVL 86

Expert Comment

You need to import

java.math.BigDecimal
0

Author Comment

when i compile , i dont get any errors but when i run, i get exceptions

exception in thread "AWT-eventqueque-1" java lang.ClassCastException :java.math.BigDecimal..

pls help...thanks
0

Author Comment

sorry..i increase the points...

i hope , my explaination is ok...

actually, i am finding range values which is needs to be based on years which is kept in the arraylist

maps 1 keeps the scores in map for year 1
maps 2 keeps the scores in map for year 2

my problem is that for each maps, i need to find the range..For example,
maps 1 for year 1
the scores stored in map would be { 2.66667=2 , 2.6661=1, 3.5=1}

so, now i find range of sorted map like this

2.6=3 (something like if value between 2.6 and 2.7, you add)
3.5=1

Data

score[year][iteration]

Iterations 1   Iteratons 2   Iterations 3 Iteration 4
Years   1  2.666667       2.6661         2.66667       3.5
Years  2   3.2224           3.2221          3.4

0

LVL 86

Expert Comment

>>exception in thread "AWT-eventqueque-1" java lang.ClassCastException :java.math.BigDecimal..

Are you putting in instances of BigDecimal? If you aren't and treating the Map as if it does have them, then you're going to get that kind of exception, although since you're using >= 1.5, you should ensure that you genericize the Map so this error is obvious at *compile* time

>>actually, i am finding range values which is needs to be based on years which is kept in the arraylist

Yes. What i'm suggesting needs to be done in a loop
0

LVL 86

Expert Comment

btw jasbir - *is* this homework?
0

Author Comment

Thanks a lot...I learnt a lot.. I understand what is being done now.. you get the values and then your floor them.. :-)
0

LVL 86

Expert Comment

:-)
0

Author Comment

hi,

nope this is not homework..actually, this was done earlier in array and was being used ..then, found out that it cant be done in array..it is the same graph project i am working on..
0

Author Comment

i am new to use maps, treemaps, jfreegraphs  ..

i am very sorry for troubling a lot..but thanks for helping a lot..
0

LVL 86

Expert Comment

No - i didn't think it was. Hope it wasn't made too much harder by the removal of my code
0

Author Comment

thanks again for everything..i have more question on maps ..i am going to post now..could you pls look at it as well..
0

Author Comment

0

## Featured Post

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.