如何从ArrayList中检索ID元素

huangapple go评论79阅读模式
英文:

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(&quot;5&quot;, &quot;chocolate&quot;, 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(&quot;2&quot;, &quot;groceries&quot;);
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&lt;Catalog&gt; subCatalogList = new ArrayList&lt;Catalog&gt;();
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&lt;Catalog&gt; catalogDirectory = new ArrayList&lt;Catalog&gt;();
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 -&gt; ((Department) department).getSubCatalogList().stream())
.filter( product -&gt; ((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&lt;Catalog&gt; subCatalogList = new ArrayList&lt;Catalog&gt;();
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&lt;Catalog&gt; getSubCatalogList(){
return this.subCatalogList;
}
}
public class Directory implements Catalog {
private ArrayList&lt;Catalog&gt; catalogDirectory = new ArrayList&lt;Catalog&gt;();
public void addDepartment(Catalog department){
catalogDirectory.add(department);
}
public ArrayList&lt;Catalog&gt; 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(&quot;department1&quot;, &quot;departmentName&quot;);
dep.subCatalogList = new ArrayList&lt;&gt;(Arrays.asList(new Product(&quot;idtofind&quot;, &quot;hello name&quot;, 32)));
directory.catalogDirectory = new ArrayList&lt;&gt;(Arrays.asList(dep));
String productID=&quot;idtofind&quot;;
Product productToFind = (Product) directory.getCatalogDirectory ().stream()
.flatMap(department -&gt; ((Department) department).getSubCatalogList().stream())
.filter(product -&gt; ((Product)product).getProdId().equals(productID)).findFirst().get();
System.out.println(productToFind.prodName);
}

huangapple
  • 本文由 发表于 2020年9月20日 05:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定