@Test
public void test(){
List list = new ArrayList<>();
list.add(40d);
list.add(30.00);
list.add(10d);
list.add(7d);
list.add(2d);
list.add(1.00);
list.add(0.05);
list.add(0.3999);
list.add(0.0001);
List objects = new ArrayList<>();
int count = 1000000;
double sum = list.stream().mapToDouble(e -> e).sum();
System.out.println(sum);
list = list.stream().sorted().collect(Collectors.toList());
for (int i = 0; i< count; i++){
double random = Math.random();
double s = 0d;
for (double d:list){
s = s + d;
if((random*sum)<=s){
objects.add(d); break;
}
}
}
System.out.println(objects.size());
Map collect = objects.stream()
.collect(Collectors.groupingBy(e ->e,Collectors.counting()));
Map objectObjectHashMap = new HashMap<>();
System.out.println(collect);
System.out.println(collect.values().stream().mapToDouble(e->e).sum());
for (Double s : collect.keySet()){
objectObjectHashMap.put(s, collect.get(s)*100d/count);
}
System.out.println(objectObjectHashMap);
System.out.println(objectObjectHashMap.values()
.stream().mapToDouble(e->e).sum());
}