import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class DataDistributionExample {
public static void main(String[] args) {
List<String> dataList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
dataList.add("User" + i);
}
Map<String, Double> distributionMap = new HashMap<>();
distributionMap.put("小明", 0.3);
distributionMap.put("小红", 0.4);
distributionMap.put("小张", 0.3);
Collections.shuffle(dataList);
int totalDataCount = dataList.size();
int countForXiaoming = (int) Math.round(totalDataCount * distributionMap.get("小明"));
int countForXiaohong = (int) Math.round(totalDataCount * distributionMap.get("小红"));
List<String> resultForXiaoming = new ArrayList<>();
List<String> resultForXiaohong = new ArrayList<>();
List<String> resultForXiaozhang = new ArrayList<>();
for (int i = 0; i < totalDataCount; i++) {
if (i < countForXiaoming) {
resultForXiaoming.add(dataList.remove(0));
} else if (i < countForXiaoming + countForXiaohong) {
resultForXiaohong.add(dataList.remove(0));
} else {
resultForXiaozhang.add(dataList.remove(0));
}
}
System.out.println("小明的数据: " + resultForXiaoming);
System.out.println("小红的数据: " + resultForXiaohong);
System.out.println("小张的数据: " + resultForXiaozhang);
System.out.println("================================");
t001();
}
static void t001() {
List<Integer> originalList = IntStream.rangeClosed(1, 100).boxed().collect(Collectors.toList());
int xiaomingEnd = (int) (originalList.size() * 0.3);
int zhanggongEnd = xiaomingEnd + (int) (originalList.size() * 0.3);
Set<Integer> sampledSet = new HashSet<>();
List<Integer> xiaomingData = originalList.subList(0, xiaomingEnd)
.stream()
.distinct()
.filter(sampledSet::add)
.collect(Collectors.toList());
List<Integer> zhanggongData = originalList.subList(xiaomingEnd, zhanggongEnd)
.stream()
.distinct()
.filter(sampledSet::add)
.collect(Collectors.toList());
List<Integer> othersData = originalList.subList(zhanggongEnd, originalList.size())
.stream()
.distinct()
.filter(sampledSet::add)
.collect(Collectors.toList());
System.out.println(xiaomingData+"\n"+zhanggongData+"\n"+othersData);
}
}