英文:
List stream does not filter correct
问题
我在流式处理时遇到了关于 List 的问题。如果我减少流的操作,那么我可以得到正确的结果,但是如果我对流进行筛选并调用 findFirst() 方法,我会得到一个空的结果。为什么会这样?我做错了什么?
Index - 包含映射数据的类:
public class Index {
private final Map<String, String> data = new HashMap<>();
public String getData(String name) {
return data.getOrDefault(name, "");
}
public Set<String> getDataNames() {
return data.keySet();
}
@Override
public String toString() {
return data.toString();
}
}
IndexList - 包含 Index 对象的列表,通过在 Main 类中调用 load() 方法来填充:
public enum IndexList {
INDICES_0("First list", new LinkedList<>(Arrays.asList("name", "surname", "an")));
private List<Index> loaded;
private final String name;
private final Queue<String> nameQueue;
IndexList(String name, Queue<String> nameQueue) {
this.name = name;
this.nameQueue = nameQueue;
}
public Index get(int id) {
return loaded.get(id-1);
}
public List<Index> getLoaded() {
return loaded;
}
public void load() {
this.loaded = new LinkedList<>();
// ... > 从 xml 文件加载数据
List<Index> loaded = this.loaded;
this.loaded = new ArrayList<>(loaded);
loaded.clear();
}
public Queue<String> queueNames() {
return new LinkedList<>(nameQueue);
}
public String getName() {
return name;
}
}
在正常运行时,我应该在 GUI 中看到一些 Index 对象,但实际上没有显示出来。因此,我在调试器中运行了程序并添加了监视变量。
在调试器中:
IndexList.INDICES_0.loaded.size() = 558
IndexList.INDICES_0.loaded.stream().get(0) = {Index@1700} "{surname=Smith, name=John, an=1/2000}"
IndexList.INDICES_0.loaded.stream().reduce((i0, i1) -> i1.getData("surname").equals("Smith") && i1.getData("name").equals("John") ? i1 : i0) = {Optional@1623} "Optional[{surname=Smith, name=John, an=1/2000}]"
IndexList.INDICES_0.loaded.stream().filter(i -> i.getData("surname").equals("Smith") && i.getData("name").equals("John")).findFirst() = {Optional@1640} "Optional.empty"
英文:
I have problem with List when I'm streaming. If I reduce stream then I get correct result, but if I filter stream and invoke findFirst() I get empty result. Why? What do I do wrong?
Index - contains data in map:
public class Index {
private final Map<String, String> data = new HashMap<>();
public String getData(String name) {
return data.getOrDefault(name, "");
}
public Set<String> getDataNames() {
return data.keySet();
}
@Override
public String toString() {
return data.toString();
}
}
IndexList - contains Index objects in list, filling is invoke load() method in Main class:
public enum IndexList {
INDICES_0("First list", new LinkedList<>(Arrays.asList("name", "surname", "an")));
private List<Index> loaded;
private final String name;
private final Queue<String> nameQueue;
IndexList(String name, Queue<String> nameQueue) {
this.name = name;
this.nameQueue = nameQueue;
}
public Index get(int id) {
return loaded.get(id-1);
}
public List<Index> getLoaded() {
return loaded;
}
public void load() {
this.loaded = new LinkedList<>();
// ... > loading from xml file
List<Index> loaded = this.loaded;
this.loaded = new ArrayList<>(loaded);
loaded.clear();
}
public Queue<String> queueNames() {
return new LinkedList<>(nameQueue);
}
public String getName() {
return name;
}
}
I normal run I saw some Index object should be show in GUI, but it did not. So I ran program in debugger and added watches.
In debugger:
IndexList.INDICES_0.loaded.size() = 558
IndexList.INDICES_0.loaded.stream().get(0) = {Index@1700} "{surname=Smith, name=John, an=1/2000}"
IndexList.INDICES_0.loaded.stream().reduce((i0, i1) -> i1.getData("surname").equals("Smith") && i1.getData("name").equals("John")? i1 : i0) = {Optional@1623} "Optional[{surname=Smith, name=John, an=1/2000}]"
IndexList.INDICES_0.loaded.stream().filter(i -> i.getData("surname").equals("Smith") && i.getData("name").equals("John")).findFirst() = {Optional@1640} "Optional.empty"
答案1
得分: 0
我解决了问题:在字符串集合中有UTF-8 BOM字符,并且相同的姓氏出现在不同的位置。因此,reduce方法在流中返回了第一个对象。
英文:
I resolved problem: in set of strings were UTF-8 BOM characters and the same surnames was in different places.
So, reduce method returned the first object in stream.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论