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);
}
}
int totalData = 5;
int user1Ratio = 10;
int user2Ratio = 30;
int user3Ratio = 60;
int user1DataCount = (int) ((double) user1Ratio / (user1Ratio + user2Ratio + user3Ratio) * totalData);
int user2DataCount = (int) ((double) user2Ratio / (user1Ratio + user2Ratio + user3Ratio) * totalData);
int user3DataCount = totalData - user1DataCount - user2DataCount;
List<Integer> dataList = IntStream.rangeClosed(1, totalData).boxed().collect(Collectors.toList());
Collections.shuffle(dataList, new Random());
Map<String, List<Integer>> userDataMap = new HashMap<>();
userDataMap.put("User1", dataList.stream().limit(user1DataCount).collect(Collectors.toList()));
userDataMap.put("User2", dataList.stream().skip(user1DataCount).limit(user2DataCount).collect(Collectors.toList()));
userDataMap.put("User3", dataList.stream().skip(user1DataCount + user2DataCount).collect(Collectors.toList()));
System.out.println("User1: " + userDataMap.get("User1"));
System.out.println("User2: " + userDataMap.get("User2"));
System.out.println("User3: " + userDataMap.get("User3"));