英文:
How to retrieve ID elements from ArrayList
问题
以下是翻译好的内容:
能帮我检索一个ArrayList元素吗?前提条件如下。
Product类有三个属性(**productID**,产品名称,价格),因此在构造函数中传入以下值并创建对象。
Product chocolate = new Product("5", "chocolate", 12);
Department类有两个属性(部门ID,部门名称),因此在构造函数中传入以下值并创建对象。
Department类还有一个"addProduct"方法,因此将上述chocolate对象添加到杂货部门。
Department groceries = new Department("2", "groceries");
groceries.addProduct(chocolate);
Directory类有一个ArrayList,因此使用"addDepartemnt"方法将groceries部门添加到ArrayList中。
Directory directory = new Directory();
directory.addDepartment(groceries);
假设有多个产品。有没有办法从目录ArrayList中检索"**productID**"?我将循环遍历目录ArrayList并比较ID和用户输入的ID,如果匹配,我想检索所有产品信息(productID,产品名称,价格)。
public class Department implements Catalog {
private ArrayList<Catalog> subCatalogList = new ArrayList<Catalog>();
private String departmentId;
private String departmentName;
public Department(String departmentId, String departmentName) {
this.departmentName = departmentName;
this.departmentId = departmentId;
}
public void addProduct(Catalog catalog) {
subCatalogList.add(catalog);
}
}
public class Directory implements Catalog {
private ArrayList<Catalog> catalogDirectory = new ArrayList<Catalog>();
public void addDepartment(Catalog department) {
catalogDirectory.add(department);
}
}
public class Product implements Catalog {
private String prodId;
private String prodName;
private double price;
public Product(String prodId, String prodName, double price) {
this.prodId = prodId;
this.prodName = prodName;
this.price = price;
}
}
英文:
Could you help me to retrieve one of the ArrayList elements? Precondition is below.
Product class has three properties(productID, product name, price), so throw the below values in the constructor and create an object.
Product chocolate = new Product("5", "chocolate", 12);
Department class has two properties(department ID, department name), so throw the below values in the constructor and create an object.
Department class also has "addProduct" method, so add the above chocolate object in the grocery department.
Department groceries= new Department("2", "groceries");
groceries.addProduct(chocolate);
Directory class has an ArrayList, so added groceries department in the ArrayList using "addDepartemnt" method.
Directory directory = new Directory();
directory.addDepartment(groceries);
Suppose there are multiple products. Is there any way to retrieve "productID" from the directory ArrayList? I am going to loop the directory ArrayList and compare the IDs and user input ID, and it it's mactched, i want to retrieve all the product information (productID, product name, price).
public class Department implements Catalog {
private ArrayList<Catalog> subCatalogList = new ArrayList<Catalog>();
private String departmentId;
private String departmentName;
public Department(String departmentId, String departmentName){
this.departmentName = departmentName;
this.departmentId = departmentId;
}
public void addProduct(Catalog catalog){
subCatalogList.add(catalog);
}
public class Directory implements Catalog {
private ArrayList<Catalog> catalogDirectory = new ArrayList<Catalog>();
public void addDepartment(Catalog department){
catalogDirectory.add(department);
}
public class Product implements Catalog {
private String prodId;
private String prodName;
private double price;
public Product(String prodId, String prodName, double price){
this.prodId = prodId;
this.prodName = prodName;
this.price = price;
}
答案1
得分: 0
你可以使用Java流(Java streams)来实现这个,例如给定一个 productID
和一个 directory
:
Product productToFind = (Product) directory.getCatalogDirectory().stream()
.flatMap(department -> ((Department) department).getSubCatalogList().stream())
.filter(product -> ((Product) product).getProdId().equals(productID))
.findFirst().get();
首先,你使用 flatMap
将一个目录中的所有产品收集到一个列表中,然后在这个列表上使用给定的 productID
进行筛选。
编辑:
别忘了为你的类添加像下面这样的 getters
:
public class Department implements Catalog {
// ... 其他属性和方法
public ArrayList<Catalog> getSubCatalogList(){
return this.subCatalogList;
}
}
public class Directory implements Catalog {
// ... 其他属性和方法
public ArrayList<Catalog> getCatalogDirectory(){
return this.catalogDirectory;
}
}
public class Product implements Catalog {
// ... 其他属性和方法
public String getProdId(){
return this.prodId;
}
}
测试用例:
@Test
public void test(){
Directory directory = new Directory();
Department dep = new Department("department1", "departmentName");
dep.subCatalogList = new ArrayList<>(Arrays.asList(new Product("idtofind", "hello name", 32)));
directory.catalogDirectory = new ArrayList<>(Arrays.asList(dep));
String productID = "idtofind";
Product productToFind = (Product) directory.getCatalogDirectory().stream()
.flatMap(department -> ((Department) department).getSubCatalogList().stream())
.filter(product -> ((Product) product).getProdId().equals(productID))
.findFirst().get();
System.out.println(productToFind.prodName);
}
英文:
You can achieve his using Java streams, for example for a given productID
and a given directory
:
Product productToFind = (Product) directory.getCatalogDirectory ().stream()
.flatMap( department -> ((Department) department).getSubCatalogList().stream())
.filter( product -> ((Product)product).getProdId().equals(productID)).findFirst().get();
First, you gather all the products from a directory in one list using flatMap
, and then you filter on it using the given productID
.
EDIT:
Don't forget to add getters
to your classes like bellow:
public class Department implements Catalog {
private ArrayList<Catalog> subCatalogList = new ArrayList<Catalog>();
private String departmentId;
private String departmentName;
public Department(String departmentId, String departmentName){
this.departmentName = departmentName;
this.departmentId = departmentId;
}
public void addProduct(Catalog catalog){
subCatalogList.add(catalog);
}
public ArrayList<Catalog> getSubCatalogList(){
return this.subCatalogList;
}
}
public class Directory implements Catalog {
private ArrayList<Catalog> catalogDirectory = new ArrayList<Catalog>();
public void addDepartment(Catalog department){
catalogDirectory.add(department);
}
public ArrayList<Catalog> getCatalogDirectory (){
return this.catalogDirectory ;
}
}
public class Product implements Catalog {
private String prodId;
private String prodName;
private double price;
public Product(String prodId, String prodName, double price){
this.prodId = prodId;
this.prodName = prodName;
this.price = price;
}
public String getProdId (){
return this.prodId;
}
}
Test case:
@Test
public void test(){
Directory directory=new Directory();
Department dep =new Department("department1", "departmentName");
dep.subCatalogList = new ArrayList<>(Arrays.asList(new Product("idtofind", "hello name", 32)));
directory.catalogDirectory = new ArrayList<>(Arrays.asList(dep));
String productID="idtofind";
Product productToFind = (Product) directory.getCatalogDirectory ().stream()
.flatMap(department -> ((Department) department).getSubCatalogList().stream())
.filter(product -> ((Product)product).getProdId().equals(productID)).findFirst().get();
System.out.println(productToFind.prodName);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论