Java 如何从具有重复项的筛选列表中删除特定索引?

Java 如何从具有重复项的筛选列表中删除特定索引?,java,javafx,Java,Javafx,我有一个表视图,后面有一个分类列表包装一个过滤器列表包装一个可观察列表。筛选列表中的项目可以重复。也就是说,list.get(5)=list.get(10) 用户可以选择表视图上的行,然后按delete键。当他们这样做时,被选中的项目应该被删除,而不是其他项目 我尝试了两种解决方案,都存在问题: 在基础ObservableList上使用list.remove(对象)-由于列表中可能有重复的项,因此将删除对象的所有副本,而不仅仅是选定的副本 以下是我如何设置列表: private final Ob

我有一个
表视图
,后面有一个
分类列表
包装一个
过滤器列表
包装一个
可观察列表
。筛选列表中的项目可以重复。也就是说,
list.get(5)=list.get(10)

用户可以选择
表视图上的行
,然后按delete键。当他们这样做时,被选中的项目应该被删除,而不是其他项目

我尝试了两种解决方案,都存在问题:

在基础ObservableList上使用list.remove(对象)-由于列表中可能有重复的项,因此将删除对象的所有副本,而不仅仅是选定的副本

以下是我如何设置列表:

private final ObservableList <Item> items = FXCollections.observableArrayList(); 
private final FilteredList <Item> currentListFiltered = new FilteredList <Item>( items, p -> true );
private final SortedList <Item> currentListSorted = new SortedList <CurrentListTrack>( currentListFiltered );

TransformationList
(其中
SortedList
FilteredList
都是实现)有一种方法可以将转换列表中的索引“转换”为其源(基础)列表中的索引。因此,
currentListSorted(index)
给出了在已排序列表中具有所提供索引的项目的筛选列表中的索引,
currentListFiltered(index)
给出了在筛选列表中具有所提供索引的项目的原始
项目
列表中的索引

所以你可以

items.remove(currentListFiltered.getSourceIndex(
    currentListSorted.getSourceIndex(index)
));
删除可见表项(排序列表)的“索引坐标”中特定索引处的项

当然,您需要小心处理这里代码中的循环,因为当项目被删除时,索引将改变。(如果您只是简单地从简单列表中按索引删除项目,这也是正确的。)

因此,您可能需要以下几点:

List<Integer> indicesToBeRemoved = new ArrayList<>();
for (int index : indices) { // indices in the sorted list
    indicesToBeRemoved.add(currentListFiltered.getSourceIndex(
        currentListSorted.getSourceIndex(index)));
}
// sort with largest index first, as removing an item with
// a given index will not change the indices of items with small indices:
indicesToBeRemoved.sort(Comparator.reverseOrder()); 
for (Integer index : indicesToBeRemoved) {
    // be careful to explicitly unbox the Integer here, 
    // to avoid collision between remove(Object) and remove(int):
    items.remove(index.intValue());
}
List indicatestoremoved=new ArrayList();
对于(int-index:index){//排序列表中的索引
IndicateStoreMoved.add(currentListFiltered.getSourceIndex(
currentListSorted.getSourceIndex(index));
}
//首先使用最大索引排序,如删除具有
//给定索引不会改变索引较小的项目的索引:
indicatestoremoved.sort(Comparator.reverseOrder());
for(整数索引:indicesToBeRemoved){
//注意在这里显式取消对整数的装箱,
//要避免删除(对象)和删除(int)之间的冲突,请执行以下操作:
items.remove(index.intValue());
}
removeMenuItem.setOnAction( new EventHandler <ActionEvent>() {
    @Override
    public void handle ( ActionEvent event ) {
        ObservableList <Integer> selectedIndexes = currentListTable.getSelectionModel().getSelectedIndices();
        List <Integer> removeMe = new ArrayList<> ( selectedIndexes );
        removeItemsAtIndices ( removeMe );
    }
});
items.remove(currentListFiltered.getSourceIndex(
    currentListSorted.getSourceIndex(index)
));
List<Integer> indicesToBeRemoved = new ArrayList<>();
for (int index : indices) { // indices in the sorted list
    indicesToBeRemoved.add(currentListFiltered.getSourceIndex(
        currentListSorted.getSourceIndex(index)));
}
// sort with largest index first, as removing an item with
// a given index will not change the indices of items with small indices:
indicesToBeRemoved.sort(Comparator.reverseOrder()); 
for (Integer index : indicesToBeRemoved) {
    // be careful to explicitly unbox the Integer here, 
    // to avoid collision between remove(Object) and remove(int):
    items.remove(index.intValue());
}