[英]How to search for an object in an arraylist with multiple strings and return opbject?
Good afternoon,下午好,
I'm new to coding and i need some help I have created an Arraylist of objects named alHup.我是编码新手,我需要一些帮助我创建了一个名为 alHup 的对象的 Arraylist。 alHup contains objects named "Company".
alHup 包含名为“Company”的对象。
private ArrayList<Company> alHup = new ArrayList();
Company exists of String ID, String Companytype, String year, String Region, String Class, int Size, int Profit and int Area.公司存在 String ID、String Companytype、String year、String Region、String Class、int Size、int Profit 和 int Area。
I have Created a separate class to define Company.我创建了一个单独的 class 来定义公司。
package aaadblok1.landbouwapp.View;
public class Company {
private String Companytype, Year, Region, Class;
private Integer Size, Profit, Area;
public Company(){
}
public String getYear() {
return Year;
}
public void setYear(String Year) {
this.Year = Year;
}
public Integer getSize() {
return Size;
}
public void setSize(Integer Size) {
this.Size = Size;
}
public Integer getArea() {
return Area;
}
public void setArea(Integer Area) {
this.Area = Area;
}
public Integer getProfit() {
return totaalopbrengst;
}
public void setProfit(Integer Profit) {
this.Profit = Profit;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getClass() {
return Class;
}
public void setClass(String Class) {
this.Class = Class;
}
public String getCompanytype() {
return Companytype;
}
public void setCompanytype(String Companytype) {
this.Companytype = Companytype;
}
public String getRegion() {
return Region;
}
public void setRegion(String Region) {
this.Region = Region;
}
}
I have created 4 textfields for String Companytype, String Year, String Region and String Class.我为 String Companytype、String Year、String Region 和 String Class 创建了 4 个文本字段。 I want to be able to search the Arraylist for a single object that contains the values that are in the 4 textfields and return the int Size, int Profit and int Area for this object.
我希望能够在 Arraylist 中搜索包含 4 个文本字段中的值的单个 object,并返回此 ZA8CFDE6331BD59EB26666F8911ZB4 的 int Size、int Profit 和 int Area。 I want to display int Size, int Profit and Int Area in 3 TextFields.
我想在 3 个 TextField 中显示 int Size、int Profit 和 Int Area。
txtCompanytype= new TextField();
txtClass= new TextField();
txtRegion= new TextField();
txtYear= new TextField();
txtProfit = new TextField();
txtSize = new TextField();
txtArea= new TextField();
I have created a button named Search for this.我为此创建了一个名为“搜索”的按钮。 but I do not know how the lambda expression for the button should look like.
但我不知道按钮的 lambda 表达式应该是什么样子。
Search= new Button("Search");
Can anybuddy help me with this. anybuddy可以帮我解决这个问题。 Thank you in advance.
先感谢您。
Right now my class looks like this现在我的 class 看起来像这样
package aaadblok1.landbouwapp;
import aaadblok1.landbouwapp.View.Bedrijfsgroep;
import java.util.ArrayList;
import java.util.Optional;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
/**
*
* @author potat
*/
public class OverzichtScherm {
private Label Bedrijftype33, SoKlasse, Regio, Jaar, Totaalopbrengst, Omvang, Oppervlaktecultuurgrond;
private TextField txtBedrijfstype, txtSoKlasse, txtRegio, txtTotaalopbrengst, txtOmvang, txtOppervlaktecultuurgrond;
private Button Zoeken, Uitloggen, nh, zh, ut, nb, ze, li, ge , ov, fl, fr, gr, dr, klasse1, klasse2, klasse3, klasse4, klasse5, klasse6, klasse7, klasse8, Groep1, Groep2, Groep3, Groep4, Groep5;
private GridPane Scherm;
private ArrayList<Bedrijfsgroep> alHup = new ArrayList();
private DataCollector dc = new DataCollector();
public OverzichtScherm(Pane root){
// maken van labels en tekstvelden
Bedrijftype33= new Label("Bedrijfstype:");
SoKlasse= new Label("So-Klasse:");
Regio=new Label("Regio:");
Jaar= new Label("Jaar:");
Totaalopbrengst= new Label("Totaal Opbengst:");
Omvang= new Label("Omvang:");
Oppervlaktecultuurgrond= new Label("Oppervlakte Cultuurgrond:");
txtBedrijfstype= new TextField();
txtSoKlasse= new TextField();
txtRegio= new TextField();
txtTotaalopbrengst = new TextField();
txtOmvang = new TextField();
txtOppervlaktecultuurgrond= new TextField();
Scherm= new GridPane();
Scherm.setAlignment(Pos.CENTER);
Scherm.setHgap(10);
Scherm.setVgap(10);
Scherm.setMinSize(30, 30);
Scherm.setPadding(new Insets(10, 10, 10, 10));
//Het maken van de buttons
Zoeken= new Button("Zoeken");
Uitloggen= new Button("Uitloggen");
nh= new Button();
zh= new Button();
ut=new Button();
nb= new Button();
ze= new Button();
li= new Button();
ge= new Button();
ov = new Button();
fl= new Button();
fr= new Button();
gr= new Button();
dr= new Button();
klasse1 = new Button("3.000 tot 25.000 euro");
klasse2= new Button("25.000 tot 100.000 euro");
klasse3= new Button("100.000 tot 250.000 euro");
klasse4= new Button("250.000 tot 500.000 euro");
klasse5= new Button("500.000 tot 1.000.000 euro");
klasse6= new Button("1.000.000 tot 1.500.000 euro");
klasse7=new Button("1.500.000 tot 3.000.000 euro");
klasse8= new Button("meer als 3.000.000 euro");
Groep1=new Button("Akkerbouw");
Groep2=new Button("Tuinbouw");
Groep3=new Button("Blijvende Teelt");
Groep4= new Button("Graasdieren");
Groep5= new Button("Hokdieren");
// bepaalde tekstvlakken niet bewerkbaar zetten (voor gebruiksvriendelijkheid en veiligheid).
txtBedrijfstype.setEditable(false);
txtSoKlasse.setEditable(false);
txtRegio.setEditable(false);
txtTotaalopbrengst.setEditable(false);
txtOppervlaktecultuurgrond.setEditable(false);
txtOmvang.setEditable(false);
//Combobox voor de jaartallen
ComboBox jaartal = new ComboBox();
jaartal.getItems().add("2000JJ00");
jaartal.getItems().add("2001JJ00");
jaartal.getItems().add("2002JJ00");
jaartal.getItems().add("2003JJ00");
jaartal.getItems().add("2004JJ00");
jaartal.getItems().add("2005JJ00");
jaartal.getItems().add("2006JJ00");
jaartal.getItems().add("2007JJ00");
jaartal.getItems().add("2008JJ00");
jaartal.getItems().add("2009JJ00");
jaartal.getItems().add("20010JJ00");
jaartal.getItems().add("2011JJ00");
jaartal.getItems().add("2012JJ00");
jaartal.getItems().add("2013JJ00");
jaartal.getItems().add("2014JJ00");
jaartal.getItems().add("2015JJ00");
jaartal.getItems().add("2016JJ00");
jaartal.getItems().add("2017JJ00");
jaartal.getItems().add("2018JJ00");
jaartal.getItems().add("2019JJ00");
jaartal.getItems().add("2020JJ00");
//knoppen programmeren provincies zodat ze de het textvak invullen
nh.setOnAction(event -> {txtRegio.setText("PV27");});
zh.setOnAction(event -> {txtRegio.setText("PV28");});
ut.setOnAction(event -> {txtRegio.setText("pv26");});
nb.setOnAction(event -> {txtRegio.setText("PV30");});
ze.setOnAction(event -> {txtRegio.setText("PV29");});
li.setOnAction(event -> {txtRegio.setText("PV31");});
ge.setOnAction(event -> {txtRegio.setText("PV25");});
ov.setOnAction(event -> {txtRegio.setText("PV23");});
fl.setOnAction(event -> {txtRegio.setText("PV24");});
fr.setOnAction(event -> {txtRegio.setText("PV20");});
gr.setOnAction(event -> {txtRegio.setText("PV21");});
dr.setOnAction(event -> {txtRegio.setText("PV22");});
// knoppen programmeren soklasse zodat het textvak juist wordt ingevuld
klasse1.setOnAction(event -> {txtSoKlasse.setText("A029006");});
klasse2.setOnAction(event -> {txtSoKlasse.setText("A029007");});
klasse3.setOnAction(event -> {txtSoKlasse.setText("A029008");});
klasse4.setOnAction(event -> {txtSoKlasse.setText("A029009");});
klasse5.setOnAction(event -> {txtSoKlasse.setText("A029010");});
klasse6.setOnAction(event -> {txtSoKlasse.setText("A029011");});
klasse7.setOnAction(event -> {txtSoKlasse.setText("A029012");});
klasse8.setOnAction(event -> {txtSoKlasse.setText("A029013");});
// knoppen programmeren bedrijfstypen
Groep1.setOnAction(event -> {txtBedrijfstype.setText("A009481");});
Groep2.setOnAction(event -> {txtBedrijfstype.setText("A009487");});
Groep3.setOnAction(event -> {txtBedrijfstype.setText("A009497");});
Groep4.setOnAction(event -> {txtBedrijfstype.setText("A009501");});
Groep5.setOnAction(event -> {txtBedrijfstype.setText("A009510");});
// uitlogknop programmeren, met loguitboodschap
Uitloggen.setOnAction(event ->
{ Alert Loguit = new Alert (Alert.AlertType.INFORMATION);
Loguit.setTitle("Uitloggen");
Loguit.setHeaderText("Bedankt en tot ziens!");
Loguit.showAndWait();
root.getChildren().clear();
new LogIn (root); });
//zoeken knop programmeren zodat de juiste zoektermen worden ingevuld
ArrayList<Bedrijfsgroep> alHup = dc.getBedrijfsgroep();
Zoeken.setOnAction(event ->{
});
// Alles op Gridpane zetten en op root
// zoekvakken
Scherm.add(Bedrijftype33, 0,3);
Scherm.add(txtBedrijfstype,1,3);
Scherm.add(SoKlasse, 0,4);
Scherm.add(txtSoKlasse, 1, 4);
Scherm.add(Regio, 0, 5);
Scherm.add(txtRegio, 1, 5);
Scherm.add(Jaar, 0, 6);
Scherm.add(jaartal,1,6);
Scherm.add(Zoeken,1,7);
//UItkomstvakken
Scherm.add(Omvang, 0,10);
Scherm.add(txtOmvang,1,10);
Scherm.add(Oppervlaktecultuurgrond, 0,11);
Scherm.add(txtOppervlaktecultuurgrond, 1, 11);
Scherm.add(Totaalopbrengst, 0,12);
Scherm.add(txtTotaalopbrengst, 1, 12);
Scherm.add(Uitloggen, 1, 13);
//provincies
Scherm.add(nh, 6,3);
Scherm.add(zh, 6,4);
Scherm.add(ut, 6,5);
Scherm.add(nb, 6,6);
Scherm.add(ze, 6,7);
Scherm.add(li, 6,8);
Scherm.add(ge, 6,9);
Scherm.add(ov, 6,10);
Scherm.add(fl, 6,11);
Scherm.add(fr, 6,12);
Scherm.add(gr, 6,13);
Scherm.add(dr, 6,14);
//soklassen
Scherm.add(klasse1, 9,3);
Scherm.add(klasse2, 9,4);
Scherm.add(klasse3, 9,5);
Scherm.add(klasse4, 9,6);
Scherm.add(klasse5, 9,7);
Scherm.add(klasse6, 9,8);
Scherm.add(klasse7, 9,9);
Scherm.add(klasse8, 9,10);
//Bedrijfstypen
Scherm.add(Groep1, 12,3);
Scherm.add(Groep2, 12,4);
Scherm.add(Groep3, 12,5);
Scherm.add(Groep4, 12,6);
Scherm.add(Groep5, 12,7);
root.getChildren().add(Scherm);
}
}
It would be easier to give you the right answer, if you would provide your whole code, but anyway I would use filters and my lambda would look like this:如果您提供整个代码,给您正确的答案会更容易,但无论如何我会使用过滤器,我的 lambda 看起来像这样:
Optional<Company> result = alHup.stream().filter(company -> company.getCompanyType() == txtCompanytype.getText())
.filter(company -> company.getYear() == txtYear.getText())
.filter(company -> company.getRegion() == txtRegion.getText())
.filter(company -> company.getCompanyClass() == txtClass.getText())
.findFirst();
if (result.isPresent()) {
Company company = result.get();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.