Duplikate in einer Object-List erkennen

Veröffentlicht von

Hier ein kleines Beispiel, wie man ein Duplikat erkennt, bevor man z.B. ein neues Item der Liste hinzufügt.

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DuplicateDemo extends Application {

    private ObservableList<String> listitems = FXCollections.observableArrayList("Visual Basic", "ASP.net", "JavaFX");
    private ObservableList<Persons> personData = FXCollections.observableArrayList();

    private TextField txtName = new TextField("Test");
    private TextField txtLastName = new TextField("Test");
    private Button btnSave = new Button("save");
    private ListView<String> listview = new ListView<>();
    private Label lblCategory = new Label();
    private Label lblIndex = new Label();

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

    @Override
    public void start(Stage stage) {
        listview.setItems(listitems);
        listview.getSelectionModel().selectedItemProperty().addListener(e -> set());
        btnSave.setOnAction(this::handleSave);
        Platform.runLater(() -> listview.getSelectionModel().select(0));
        
        
        VBox vb = new VBox(new HBox(5, new Label("Index:"), lblIndex),
            new HBox(5, new Label("Category:"), lblCategory),
            new HBox(5, new Label("Name:"), txtName),
            new HBox(5, new Label("Last name:"), txtLastName)
        );

        BorderPane bp = new BorderPane();
        bp.setLeft(listview);
        bp.setCenter(vb);
        bp.setRight(btnSave);

        Scene scene = new Scene(bp, 600, 400);
        stage.setScene(scene);
        stage.show();
    }


    private void set() {
      System.out.println("Selected");
      lblCategory.setText(listview.getSelectionModel().getSelectedItem());
      lblIndex.setText(String.valueOf(listview.getSelectionModel().getSelectedIndex()));
	}

	private void handleSave(ActionEvent event) {
        Persons newPerson = new Persons(Integer.valueOf(lblIndex.getText()), lblCategory.getText(), txtName.getText(), txtLastName.getText());
        if (!personData.contains(newPerson)) {
            personData.add(newPerson);
            
            System.out.println("-- START OF LIST --");
            for (Persons person : personData) {
                System.out.println(person);
            }

        } else {
            System.out.println("Duplicate!");
            System.out.println(newPerson);
        }
    }

    public class Persons {
        SimpleIntegerProperty id;
        SimpleStringProperty name;
        SimpleStringProperty lastname;
        SimpleStringProperty category;

        Persons(int id, String category, String name, String lastname) {
            this.id = new SimpleIntegerProperty(id);
            this.name = new SimpleStringProperty(name);
            this.lastname = new SimpleStringProperty(lastname);
            this.category = new SimpleStringProperty(category);
        }

        @Override
        public boolean equals(Object o) {
            if (o == this) return true;
            if (!(o instanceof Persons)) {
                return false;
            }
            Persons persons = (Persons) o;

            // persons.id.equals() leads to the default implementation in Object
            // --> instead use this one.
            // The Property classes have their own isEqualTo method
            // with get(), you will get your simple boolean from the returned BooleanBinding
            return persons.id.isEqualTo(id).get() &&
                persons.name.isEqualTo(name).get() &&
                persons.lastname.isEqualTo(lastname).get() &&
                persons.category.isEqualTo(category).get();

        }

        @Override
        public String toString() {
            return "Persons{" +
                "id=" + id +
                ", name=" + name +
                ", lastname=" + lastname +
                ", category=" + category +
                '}';
        }
    }
}
Code-Sprache: JavaScript (javascript)