Guest User

Untitled

a guest
Feb 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. input: 12,34
  2. output:
  3. 0 (no values from 0-9)
  4. 1 (12 is between 10-19)
  5. 0
  6. 1 (34 between 30-39)
  7. ... (my values are guaranteed to be between 0-100)
  8.  
  9. package javaapplication2;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.Map;
  14. import java.util.concurrent.ThreadLocalRandom;
  15. import java.util.stream.Collectors;
  16.  
  17. public class JavaApplication2 {
  18.  
  19. public static void main(String[] args){
  20. //generating sample data------------------------------
  21. class Temp{
  22. Long vote=ThreadLocalRandom.current().nextLong(100);
  23. public long getVote(){
  24. System.out.println(vote);
  25. return vote;
  26. }
  27. }
  28.  
  29. ArrayList<Temp> t = new ArrayList<>();
  30. t.add(new Temp());
  31. t.add(new Temp());
  32. t.add(new Temp());
  33. //end generating data------------------------------------
  34. Map<Integer, Long> counters =
  35. t.stream()
  36. .collect(Collectors.groupingBy(p -> ((int)p.getVote())/10,
  37. Collectors.counting()));
  38.  
  39. Collection<Long> values = counters.values();
  40. Long[] res = values.toArray(new Long[values.size()]);
  41. for(Long l:res)
  42. System.out.println(l.toString());
  43. }
  44.  
  45. }
Add Comment
Please, Sign In to add comment