Java中如何创建仅指定特定字段的Transaction类对象并构建List集合
Hey there! Let's work through this problem together. The core issue here is that your current Transaction class only uses the default no-argument constructor, so you can't directly pass values for mnemonic, transactionCode, and timeStramp when creating new objects. Here are two easy-to-implement solutions:
方案1:添加带指定参数的构造方法
This is the most direct approach—we'll add a constructor to the Transaction class that accepts exactly the three fields you need to customize, while leaving the other fields with their default values.
修改后的Transaction类
import com.fasterxml.jackson.annotation.JsonProperty; public class Transaction{ @JsonProperty("Amount") public String amount = "1390.00"; @JsonProperty("Mnemonic") public String mnemonic = "NYU"; @JsonProperty("Transaction_Code") public String transactionCode = "1435a"; @JsonProperty("Time_Stramp") public String timeStramp = "2021-02-16-22.50.41.782000"; @JsonProperty("AccountNo") public String accountNo = "15433276004350"; // 添加带指定参数的构造方法 public Transaction(String mnemonic, String transactionCode, String timeStramp) { this.mnemonic = mnemonic; this.transactionCode = transactionCode; this.timeStramp = timeStramp; } // 保留默认构造方法(Jackson序列化/反序列化会用到) public Transaction() {} }
在Main类中创建5个对象并添加到List
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Transaction> transactions = new ArrayList<>(); // 对应示例数据逐个创建对象并添加 transactions.add(new Transaction("PRY", "15s57", "2021-03-14-23.40.45.781300")); transactions.add(new Transaction("PCA", "16a48", "2021-04-21-17.30.15.331200")); transactions.add(new Transaction("PRY", "16t21", "2021-04-26-23.45.45.764100")); transactions.add(new Transaction("ASL", "17q02", "2021-05-12-18.20.25.232300")); transactions.add(new Transaction("RRL", "19m40", "2021-08-04-04.10.34.536400")); } }
方案2:添加Setter方法(更灵活)
If you prefer more flexibility (like being able to modify fields after creating the object), you can add setter methods for the three fields instead of a custom constructor.
修改后的Transaction类
import com.fasterxml.jackson.annotation.JsonProperty; public class Transaction{ @JsonProperty("Amount") public String amount = "1390.00"; @JsonProperty("Mnemonic") public String mnemonic = "NYU"; @JsonProperty("Transaction_Code") public String transactionCode = "1435a"; @JsonProperty("Time_Stramp") public String timeStramp = "2021-02-16-22.50.41.782000"; @JsonProperty("AccountNo") public String accountNo = "15433276004350"; // 添加Setter方法 public void setMnemonic(String mnemonic) { this.mnemonic = mnemonic; } public void setTransactionCode(String transactionCode) { this.transactionCode = transactionCode; } public void setTimeStramp(String timeStramp) { this.timeStramp = timeStramp; } }
在Main类中创建对象
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Transaction> transactions = new ArrayList<>(); // 创建第一个对象并设置字段 Transaction tx1 = new Transaction(); tx1.setMnemonic("PRY"); tx1.setTransactionCode("15s57"); tx1.setTimeStramp("2021-03-14-23.40.45.781300"); transactions.add(tx1); // 重复模式创建剩下的4个对象 Transaction tx2 = new Transaction(); tx2.setMnemonic("PCA"); tx2.setTransactionCode("16a48"); tx2.setTimeStramp("2021-04-21-17.30.15.331200"); transactions.add(tx2); Transaction tx3 = new Transaction(); tx3.setMnemonic("PRY"); tx3.setTransactionCode("16t21"); tx3.setTimeStramp("2021-04-26-23.45.45.764100"); transactions.add(tx3); Transaction tx4 = new Transaction(); tx4.setMnemonic("ASL"); tx4.setTransactionCode("17q02"); tx4.setTimeStramp("2021-05-12-18.20.25.232300"); transactions.add(tx4); Transaction tx5 = new Transaction(); tx5.setMnemonic("RRL"); tx5.setTransactionCode("19m40"); tx5.setTimeStramp("2021-08-04-04.10.34.536400"); transactions.add(tx5); } }
小提示
If you're using Java 16+, you could also make Transaction a record for even cleaner code, but since your original class uses default values and Jackson annotations, the above two methods are more compatible with your existing setup.
内容的提问来源于stack exchange,提问作者Deepika

