gpt4 book ai didi

java - FXCollections.unmodifiableObservableList 的用途是什么?

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

为了向我的 API 的客户端返回一个不可变的可观察列表,我使用了 FXCollections.unmodifiableObservableList(list) 包装器,如下所示:

private final ObservableList<Person> persons = FXCollections.observableArrayList();

public ObservableList<Person> getPersons() {
return FXCollections.unmodifiableObservableList(persons);
}

但是,当客户端将 ListChangeListener 添加到返回的列表时,它不会在发生更改时收到通知。这显然是因为 FXCollections 类创建的包装器在包装列表上设置了一个弱监听器,并且这个弱监听器被垃圾收集。

我是不是漏掉了这个包装器的某些内容?

返回不可变可观察列表的正确方法是什么?

最佳答案

您走在正确的轨道上:包装器列表向包装器列表添加一个弱监听器以避免内存泄漏,如果您不持有对包装器列表的引用,它将被垃圾收集。

查看此测试(取自 here ):

private ObservableList<String> s = FXCollections.observableArrayList();

@Override
public void init() throws Exception {
FXCollections.unmodifiableObservableList(s).addListener((ListChangeListener.Change<? extends String> c) ->
System.out.println(c));
s.setAll("A1");
s.setAll("A2");
System.gc();
s.setAll("A3"); // <- Doesn't trigger the listener
}

它打印:

{ [A1] added at 0 }
{ [A1] replaced by [A2] at 0 }

但是如果您添加对列表的引用:

private ObservableList<String> s = FXCollections.observableArrayList();

@Override
public void init() throws Exception {
// create a reference
final ObservableList<String> wrapperList = FXCollections.unmodifiableObservableList(s);

wrapperList.addListener((ListChangeListener.Change<? extends String> c) ->
System.out.println(c));
s.setAll("A1");
s.setAll("A2");
System.gc();
s.setAll("A3"); // <- Triggers the listener
}

现在打印:

{ [A1] added at 0 }
{ [A1] replaced by [A2] at 0 }
{ [A2] replaced by [A3] at 0 }

关于java - FXCollections.unmodifiableObservableList 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341400/

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