gpt4 book ai didi

java - 在 map 中为每个键获取重复值

转载 作者:行者123 更新时间:2023-11-30 06:52:39 26 4
gpt4 key购买 nike

我正在尝试将所有值放入 map 中,并且我有超过 20k 个值,现在我正在尝试使用想法将值放入 map 中,因为键 1 包含从 1(考虑 i)到 1000(即 i*1000)的值) 但我得到的输出包含重复值(键 1 和 2 具有相同的值),不确定我在做什么错

这是代码

 public class GetNumbers {

public static List<String> createList() throws IOException {
List<String> numbers = new LinkedList<>();
Path path = null;
File file = null;
BufferedReader reader = null;
String read = "";
try {
path = Paths.get("file.txt");
file = path.toFile();
reader = new BufferedReader(new FileReader(file));
while ((read = reader.readLine()) != null) {
numbers.add(read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return numbers;
}

public static Map<Integer, List<String>> createNewFiles() throws IOException {
Map<Integer, List<String>> myMap = new HashMap<>();
List<String> getList = GetNumbers.createList();
List<String> list = null;
int count = getList.size() / 1000;
---------------------------doubt full code-----------------------------------
for (int i = 1; i <= count; i++) {
if (getList.size() > 1000) {
list = getList.subList(i, i * 1000);
} else if (getList.size() < 999) {
list = getList.subList(i, getList.size());
}
-----------------------------------------------------------------------------
myMap.put(i, list);
}
return myMap;

}

public static void getMap() throws IOException {
Map<Integer, List<String>> map = GetNumbers.createNewFiles();
List<String> listAtIndexOne = map.get(2);
List<String> listAtIndexTwo = map.get(1);
for (String elementFromFirstList : listAtIndexOne) {
for (String elementFromSecondList : listAtIndexTwo) {
if (elementFromFirstList.equals(elementFromSecondList)) {
System.out.println("duplicate copy");
}
}
}

}

public static void main(String[] args) {
try {
GetNumbers.getMap();
} catch (IOException e) {
e.printStackTrace();
}
}
}

编辑

如果我将代码更改为

for (int i = 0; i <= count; i++) {
if (getList.size() > (i * 1000)) {
list = getList.subList(i, (i + 1) * 1000);
} else if (getList.size() < 999) {
list = getList.subList(i, getList.size());
}
myMap.put(i, list);
}

我得到了

Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 25000 at java.util.SubList.(Unknown Source) at java.util.AbstractList.subList(Unknown Source) at com.dnd.GetNumbers.createNewFiles(GetNumbers.java:43) at com.dnd.GetNumbers.getMap(GetNumbers.java:54) at com.dnd.GetNumbers.main(GetNumbers.java:69)

感谢任何帮助

谢谢

最佳答案

要将列表拆分为 1000 个元素的子列表,您可以这样写:

        for (int i = 1; i <= count; i++) {
if (getList.size() >= i*1000) {
list = getList.subList((i-1) * 1000, i * 1000);
} else {
list = getList.subList((i-1) * 1000, getList.size());
}
myMap.put(i, list);
}

或更简单:

        for (int i = 1; i <= count; i++) {
list = getList.subList((i-1) * 1000, Math.min(getList.size(),i * 1000));
myMap.put(i, list);
}

请注意,索引是从 0 开始的,因此第一个子列表将从 0 到 999,第二个从 1000 到 1999,依此类推。

关于java - 在 map 中为每个键获取重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com