复制ObservableList java [英] Copy ObservableList java

查看:165
本文介绍了复制ObservableList java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MasterData,它是一个ObservableList和filteredData,它也是一个ObservableList。

I have a MasterData which is an ObservableList and filteredData which is also an ObservableList.

然后,我想用它来显示设置过滤器时过滤后的数据,同时也能恢复必要时,这是MCVE:

Then, I want to use it to show the filtered data when the filter is set, but also to be able to recover whenever necessary, here is the MCVE:

package br;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Main{
    private static ObservableList<Foo> masterData = FXCollections.observableArrayList();
    private static ObservableList<Foo> filteredData = FXCollections.observableArrayList();
    static Filter filter = new Filter();
    public static void addDummy(){
        masterData.add(new Foo("a",1));
        masterData.add(new Foo("aa",3));
        masterData.add(new Foo("b",2));
        masterData.add(new Foo("bb",4));
        masterData.add(new Foo("c",3));
    }
    public static void printData(ObservableList<Foo> list){
        for(Foo f: list) System.out.println(f.getName());
    }
    public static void main(String[] args) {
        addDummy();
        applyFilter(3);
        printData(filteredData);
        applyFilter(0);
        printData(filteredData);
    }
    public static void applyFilter(int num){
        filter.setData(masterData);
        filter.setFilter(num);
        filteredData = filter.getData();
    }
}

类过滤器,我用它来过滤数据:

package br;

import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Filter {
    private ObservableList<Foo> data = FXCollections.observableArrayList();
    public void setFilter(int filter){
        List<Foo> copy = new ArrayList<Foo>(data);
        for(Foo f: copy)
            if(f.getBar()<filter) data.remove(f);
    }
    public ObservableList<Foo> getData() {
        return data;
    }

    public void setData(ObservableList<Foo> data) {
        this.data = data;
    }

}

和Foo类,这是只是一个字符串名称和一个int栏(加上getter / setters)

And the Foo class, which is just a String Name and a int bar (plus getters /setters)

package br;

public class Foo {
    private String name;
    private int bar;
    public Foo(String name, int bar){
        this.setBar(bar);
        this.setName(name);
    }
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getBar() {return bar;}
    public void setBar(int bar) {this.bar = bar;}
}

当你运行这段代码时,你会得到:

When you run this code, you will get:


aa

bb

c

aa
bb
c

当过滤器设置为3时(按预期过滤数据)和过滤器重置为零后

Both when the filter was set to 3 (which filtered the data as expected) and after the filter was "reseted" to zero.

如何确保始终使用中的数据处理过滤器masterData 然后存储在 filteredData 上?

How can I make sure the filter is always processed with the data in the masterData and then stored on the filteredData ?

注意:这是针对JavaFX项目的,所以我真的需要使用ObservableList。

推荐答案

问题

public void setData(ObservableList<Foo> data) {
    this.data = data;
}

说明

如果您想知道Java服从 Pass By Value 并且 masterData 的值应该已经复制到列表数据。你是对的 !但是这个故事只有一点点扭曲!

In case you are wondering that Java obeys Pass By Value and the value of masterData should have been copied to the list data. You are correct ! But there is just a little twist to the story !

包含原语,值传递但是包含对象,对象引用按值传递

Incase of primitives, the values are passed but incase of objects, Object references are passed by value.

因此,您正在引用 masterData 的Object,而不是将值复制到一个新的清单!

So, you are referencing to the Object of the masterData, instead of copying the values to a new List !

解决方案

this.data = FXCollections.observableArrayList(data);

这将为数据创建一个新的对象来引用和所有操作的将在这个新对象的数据上完成。

This will create a new Object for data to refer and all the manipulation's will be done on this new object's data.

这篇关于复制ObservableList java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆