4

我正在研究 javaFx 应用程序并创建了一个 tableview 并且有大约 100,000+ 行和 10 列。

我还使用 java swing Jtable 创建了同一张表。

现在我需要在 javaFx 中的排序性能更好,如果不比 java swing 的 jtable 附近的性能更好的话。

现在我正在使用 sortorder() 通过单击列标题对数据进行排序,排序速度比 Jtable 慢 20 倍。

有人可以帮忙吗?

谢谢你

编辑:

我在此链接中使用示例 13.8 http://docs.oracle.com/javafx/2/ui_controls/table-view.htm 只是添加了几行代码,通过生成随机数据来添加 100,000 行。

4

2 回答 2

7

花了一段时间,但我想我已经弄清楚了,至少对于这个例子来说。

在这个例子中,Person 类没有任何属性访问器(即有一个 getFirstName() 方法,但没有 firstNameProperty() 方法)。按列排序必须通过单元格值工厂访问列中每个单元格中的值。当没有属性访问器时,单元格值工厂将调用 getFirstName(),然后在每次调用时将结果包装在新的 ReadOnlyObjectWrapper 中。

如果您确保表示行数据的类具有适当的属性访问器,则检索该值的效率要高得多,因为它仅返回对现有 StringProperty 的引用。

此示例在我的系统(MacBookPro 8GB RAM,四核)上大约在一秒钟内对 100,000 行进行排序。您可以通过提供显式的单元格值工厂来进一步提高性能,它可以解决计算单元格值时反射的需要。换句话说,替换

    firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person, String>("firstName"));

    firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,String>, ObservableValue<String>>() {
      @Override
      public ObservableValue<String> call(CellDataFeatures<Person, String> cdf) {
        return cdf.getValue().firstNameProperty();
      }
    });

这里的性能节省没有那么显着。

这是完整的示例:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Random;

public class TableSortPerformanceTest extends Application {


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(550);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        final TableView<Person> table = new TableView<Person>();
        table.setEditable(true);

        TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));
        firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,String>, ObservableValue<String>>() {
          @Override
          public ObservableValue<String> call(CellDataFeatures<Person, String> cdf) {
            return cdf.getValue().firstNameProperty();
          }
        });

        TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));



        TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));


        Random random = new Random();
        for (int i = 0; i < 100000; i++) {
          table.getItems().add(new Person(randomString(random), randomString(random), randomString(random)));
        }
        table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));

        long start = new Date().getTime();
        Collections.sort(table.getItems());
        long end   = new Date().getTime();
        System.out.println("Took: " + (end - start));


        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("First Name");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("Last Name");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("Email");

        final Button addButton = new Button("Add");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                table.getItems().add(new Person(
                        addFirstName.getText(),
                        addLastName.getText(),
                        addEmail.getText()));
                addFirstName.clear();
                addLastName.clear();
                addEmail.clear();
            }
        });

        final HBox hb = new HBox(3);
        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    private String randomString(Random random) {
      char[] chars = new char[20];
      for (int i = 0; i < 20; i++) {
        int nextInt = random.nextInt(26);
        nextInt += random.nextBoolean() ? 65 : 97;
        chars[i] = (char) nextInt;
      }
      return new String(chars);
    }

    public static class Person implements Comparable<Person> {

        private final StringProperty firstName;
        private final StringProperty lastName;
        private final StringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public StringProperty firstNameProperty() {
          return firstName ;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public StringProperty lastNameProperty() {
          return lastName ;
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

        public StringProperty emailProperty() {
          return email ;
        }

    @Override
    public int compareTo(Person o) {
      return firstName.get().compareTo(o.getFirstName());
    }
  }
} 

更新:请注意,这在 JavaFX 8 中已修复。

于 2013-05-30T17:37:10.000 回答
0

当您单击表格标题时,我不知道 TableView 上的排序速度是什么 - 正如您在问题中提到的那样,它会变慢 100,000 行。

如果您只提供一个对底层集合进行排序的按钮,那么它的排序速度会快很多倍,并且表格更新得很好(至少在 Java 8 下是这样)。对一列进行排序的时间不到一秒。

Button sortByEmail = new Button("Sort by Email");
sortByEmail.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent event) {
    Collections.sort(table.getItems(), new Comparator<Person>() 
      @Override public int compare(Person o1, Person o2) {
        return o1.getEmail().compareTo(o2.getEmail());
      }
    });
  }
});

或者使用 Java 8 lambda:

Button sortByEmail = new Button("Sort by Email");
sortByEmail.setOnAction(event -> 
    Collections.sort(
        table.getItems(), 
        (o1, o2) -> o1.getEmail().compareTo(o2.getEmail())
    )
);

因此,如果您的表中有很多项目,请在列上调用setSortable(false)并在需要时为用户提供对表列进行排序的按钮。

将我的笔记本电脑插入墙上并增加 JVM 的最大内存也提高了大型数据集的排序性能(通过单击下面示例的列标题将排序时间从 20 秒缩短到大约 10 秒)。

使用以下示例代码向JavaFX 问题跟踪器提交增强请求。该代码生成100,000行随机数据,您可以通过单击表格列标题对表格列进行排序来检查标准列排序的性能,并将其与Collections.sort按按钮调用的排序性能进行比较。

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Random;

public class Test extends Application {

    private TableView<Person> table = new TableView<Person>();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com"));
    final HBox hb = new HBox();

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(550);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));

        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));

        Random random = new Random();
        table.setItems(data);
        for (int i = 0; i < 100000; i++) {
          table.getItems().add(new Person(randomString(random), randomString(random), randomString(random)));
        }
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        long start = new Date().getTime();
        Collections.sort(table.getItems());
        long end   = new Date().getTime();
        System.out.println("Took: " + (end - start));


        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("First Name");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("Last Name");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("Email");

        final Button addButton = new Button("Add");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                data.add(new Person(
                        addFirstName.getText(),
                        addLastName.getText(),
                        addEmail.getText()));
                addFirstName.clear();
                addLastName.clear();
                addEmail.clear();
            }
        });

        Button sortByFirstName = new Button("Sort by First Name");
        sortByFirstName.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent event) {
            Collections.sort(table.getItems());
          }
        });

        Button sortByEmail = new Button("Sort by Email");
        sortByEmail.setOnAction(new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent event) {
            Collections.sort(table.getItems(), new Comparator<Person>() {
              @Override
              public int compare(Person o1, Person o2) {
                return o1.getEmail().compareTo(o2.getEmail());
              }
            });
          }
        });

        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, sortByFirstName, sortByEmail);
        hb.setSpacing(3);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    private String randomString(Random random) {
      char[] chars = new char[20];
      for (int i = 0; i < 20; i++) {
        int nextInt = random.nextInt(26);
        nextInt += random.nextBoolean() ? 65 : 97;
        chars[i] = (char) nextInt;
      }
      return new String(chars);
    }

    public static class Person implements Comparable<Person> {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

    @Override
    public int compareTo(Person o) {
      return firstName.get().compareTo(o.getFirstName());
    }
  }
} 
于 2013-05-29T11:22:58.010 回答