Divide numbers in a different range

I am trying to measure how long each thread takes when pasting into a database. I wrote down all these performance numbers on a map called ConcurrentHashMap , for example, how long each thread takes to insert. In this parallel hash map, it will be something like this.

 Key- 10 Value- 2 

Thus, this means that 2 calls returned after 10 ms. Another example below

 Key - 20 Value -1 

which means that 1 call returned after 20 ms.

And this map will contain a lot more data, which means more than a pair of key values.

So now I'm trying to do something like below using the same map above, which means I need to iterate the map above to get the numbers below in this specific range. Can this be done?

 How many calls(X number) came back in between 1 and 20 ms How many calls(X number) came back in between 20 and 40 ms How many calls(X number) came back in between 40 and 60 ms How many calls(X number) came back in between 60 and 80 ms How many calls(X number) came back in between 80 and 100 ms How many calls(X number) came back after 100 ms 

Some kind of code that I was thinking about initially.

 SortedSet<Long> keys = new TreeSet<Long>(map.keySet()); for (Long key : keys) { System.out.print(key + " : "); for (int i = 0; i < map.get(key); i++) { // Not sure what I am supposed to do here? } System.out.println(); } 

Can someone help me here?

Update: -

The value of my card is

 {31=3, 48=1, 33=1, 30=12, 43=1, 38=1, 32=1} 

This means that the general call was 3+1+1+12+1+1+1 = 20 , adding value from map

And from this I need to find out that the above script means something like this

 How many calls(X number) came back in between 1 and 20 ms How many calls(X number) came back in between 20 and 40 ms How many calls(X number) came back in between 40 and 60 ms How many calls(X number) came back in between 60 and 80 ms How many calls(X number) came back in between 80 and 100 ms How many calls(X number) came back after 100 ms 

Below is my code that I tried with the sentence below -

private static void drawHistogram (Map Map) {

 int counter[] = new int[6]; for (Integer key : map.keySet()) { System.out.println("" + key); // add sample int idx = key / 20; idx = Math.min(idx, counter.length - 1); counter[idx]++; } for (int i = 0; i < counter.length; i++) { System.out.println(counter[i] + " came back in between " + i * 20 + " and " + (i + 1) * 20 + " ms"); } 

}

As you can see, I have 20 calls made, but this only shows 7 calls. Is something wrong with me? This is the result I got -

 0 came back in between 0 and 20 ms 5 came back in between 20 and 40 ms 2 came back in between 40 and 60 ms 0 came back in between 60 and 80 ms 0 came back in between 80 and 100 ms 0 came back in between 100 and 120 ms 

which shows only 7 calls. But there are 20 challenges.

+4
source share
5 answers

Anticipating the need to easily redefine the size of the bucket (and indeed the number of units you are inserting into), I suggest:

  Map<Integer, Integer> values = new HashMap<Integer, Integer>(); int[] definition = {0, 20, 40, 60, 80, 100}; int[] buckets = new int[definition.length]; for (int time : values.keySet()) { for (int i=definition.length-1; i>=0; i--) { if (time >= definition[i]) { buckets[i] += values.get(time); break; } } } for (int i=0; i<definition.length; i++) { String period = ""; if (i == definition.length-1) { period = "greater than " + definition[i] + "ms"; } else { period = "between " + (definition[i]+1) + " and " + definition[i+1] + "ms"; } System.out.println(buckets[i] + " came back " + period); } 

Configurability is driven by a change in definition . For this, I used the following code:

  Random rnd = new Random(); for (int i=0; i<1000; i++) { int time = rnd.nextInt(121); Integer calls = values.get(time); if (calls == null) { calls = Integer.valueOf(0); } calls += 1; values.put(time, calls); } 
0
source
 SortedSet<Long> keys = new TreeSet<Long>(map.keySet()); Map<Long, Long> values = new HashMap<Long, Long>(); Integer total = null; Integer current = null; Long point = null; for (Long key : keys) { System.out.print(key + " : "); current = map.get(key); if(key >= 1 && key <= 20) { point = 1; } // Do Other Comparisons also and change point 2, 3, 4, 5, 6 total = values.get(point); if(total == null) { total = 0; } total += current; values.put(point, total); System.out.println(); } 

Now if you loop keySet values

Point 1 will be How many calls(X number) came back in between 1 and 20 ms

0
source

You can use NavigableMap , which allows you to query a range of numbers (head, tail). Secure implementation of the ConcurrentSkipListMap stream.

In particular, look at the methods NavigableMap<K,V> headMap(K toKey, boolean inclusive) , NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) and SortedMap<K,V> subMap(K fromKey, K toKey)

Example

 //your existing concurrent map changed to concurrent navigable map NavigableMap<Long, Long> throughputCounter = new ConcurrentSkipListMap<Long, Long>(); // this prints for inclusive values - 1 and 20 are both included System.out.println("How many calls(X number) came back in between 1 and 20 ms:" + calcThroughput(throughputCounter.subMap(1L, true, 20L, true))); System.out.println("How many calls(X number) came back in between 20 and 40 ms:" + calcThroughput(throughputCounter.subMap(20L, true, 40L, true))); System.out.println("How many calls(X number) came back in between 40 and 60 ms:" + calcThroughput(throughputCounter.subMap(40L, true, 60L, true))); System.out.println("How many calls(X number) came back in between 60 and 80 ms:" + calcThroughput(throughputCounter.subMap(60L, true, 80L, true))); System.out.println("How many calls(X number) came back in between 80 and 100 ms:" + calcThroughput(throughputCounter.subMap(80L, true, 100L, true))); System.out.println("How many calls(X number) came back in after 100 ms:" + calcThroughput(throughputCounter.tailMap(100L))); private Long calcThroughput(NavigableMap<Long, Long> subMap) { Long sumOfARange = new Long(0); for (Long value : subMap.values()) { sumOfARange += value; } return sumOfARange; } 
0
source

You don't need a map at all. You can simply divide the time by 20 (ms) and increase the counter in the array.

 public static void main( String[] args) { int counter[] = new int[6]; for ( int i = 0 ; i < 100 ; i++ ) { int time = (int) ( Math.random() * 200 ); System.out.println( "" + time ); // add sample int idx = time / 20; idx = Math.min( idx, counter.length-1); counter[idx]++; } for ( int i = 0 ; i < counter.length ; i++ ) { System.out.println( counter[i] + " came back in between " + i*20 + " and " + (i+1)*20 + " ms" ); } } 

Note that the last element of the array contains the number of all samples> = 100 ms, so the output should be fixed. It was not possible to make the code as short and clear as possible.

Output example

 13 came back in between 0 and 20 ms 10 came back in between 20 and 40 ms 13 came back in between 40 and 60 ms 10 came back in between 60 and 80 ms 11 came back in between 80 and 100 ms 43 came back in between 100 and 120 ms 

UPDATE: Conclusion as it should be

 for ( int i = 0 ; i < counter.length-1 ; i++ ) { System.out.println( counter[i] + " came back in between " + i*20 + " and " + (i+1)*20 + " ms" ); } System.out.println( counter[counter.length-1] + " came back after 100" ms" ); 
0
source

You can try:

 SortedSet<Long> keys = new TreeSet<Long>(map.keySet()); int group1To20=0; int group20To40=0; int group40To60=0; int group60To80=0; int group80To100=0; int groupAbove100=0; for (Long key : keys) { if(key>=0 && key<=20){ group1To20=group1To20+map.get(key); }elseif(key>20 && key<=40){ group20To40=group20To40+map.get(key); } //Similarly do as above for other range of group }//end of loop System.out.print("group 1-20 contains " + group1To20); //Now print the group range and values here } 

I tried for your solution. I may misunderstand your question. If yes, then clarify the question for me.

0
source

All Articles