猿问

如何在java HashMap中根据产品类别获取产品详细信息?

有HashMap。我在 hashmap 中添加了所有产品详细信息。我想根据类别显示产品详细信息。这意味着应该展示所有相同类别的产品。我正在尝试为此创建方法。方法名称是


public List<Product> getProductsBasedOnCategory(String category)

{


}

请找到下面的代码。


产品.java


public class Product {


    private long pid;

    private String pname;

    private String category;

    private float price;

    private long stock;

    private String remarks;


    public Product()

    {


    }


    public Product(long pid,String pname,String category,float price,long stock,String remarks){

        this.pid=pid;

        this.pname=pname;

        this.category=category;

        this.price=price;

        this.stock=stock;

        this.remarks=remarks;

    }

    public long getPid() {

        return pid;

    }

    public void setPid(long pid) {

        this.pid = pid;

    }

    public String getPname() {

        return pname;

    }

    public void setPname(String pname) {

        this.pname = pname;

    }

    public String getCategory() {

        return category;

    }

    public void setCategory(String category) {

        this.category = category;

    }

    public float getPrice() {

        return price;

    }

    public void setPrice(float price) {

        this.price = price;

    }

    public long getStock() {

        return stock;

    }

    public void setStock(long stock) {

        this.stock = stock;

    }

    public String getRemarks() {

        return remarks;

    }

    public void setRemarks(String remarks) {

        this.remarks = remarks;

    }


}

数据库类.java


public class DatabaseClass {


    private static Map<Long, Product> products=new HashMap<>();


    public static Map<Long, Product> getProduct()

    {

        return products;

    }


}


我正在尝试很多如何编写代码来获取 HashMap 中模型类的值。以及如何为上述 getProductByCategory(String category) 编写代码。




温温酱
浏览 178回答 3
3回答

慕哥6287543

您可以迭代地图中的一组值,并过滤以返回匹配产品的列表:public List<Product> getProductByCategory(String category) {&nbsp; &nbsp; if(products.size() == 0){&nbsp; &nbsp; &nbsp; &nbsp; return new ArrayList<>();&nbsp; &nbsp; }&nbsp; &nbsp; return this.products.values().stream()&nbsp; &nbsp; &nbsp; &nbsp;.filter(product -> product.getCategory().equals(category))&nbsp; &nbsp; &nbsp; &nbsp;.collect(Collectors.toList());}您还可以为此使用 for 循环:public List<Product> getProductByCategory(String category) {&nbsp; &nbsp; List<Product> ret = new ArrayList<>();&nbsp; &nbsp; if(products.size() == 0){&nbsp; &nbsp; &nbsp; &nbsp; return ret;&nbsp; &nbsp; }&nbsp; &nbsp; for(Product p: this.products.values()) {&nbsp; &nbsp; &nbsp; &nbsp; if(p.getCategory().equals(category))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret.add(p);&nbsp; &nbsp; }&nbsp; &nbsp; return ret;}请注意,ArrayList如果产品地图为空,我将返回一个空值。这是集合返回类型的更好做法(而不是返回null)

喵喵时光机

一种方法是遍历 Hashmap 如下:public List<Product> getProductsBasedOnCategory(String category){&nbsp; &nbsp; List<Product> list = new ArrayList<Product>();&nbsp; &nbsp; if (products.size()<=0) {&nbsp; &nbsp; &nbsp; &nbsp; return list;&nbsp; &nbsp; }&nbsp; &nbsp; products.entrySet().stream().forEach((entry) -> {&nbsp; &nbsp; &nbsp; &nbsp; if (((Product) entry.getValue()).getCategory().equals(category)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(entry.getValue())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return list;}

陪伴而非守候

您至少有 2 个选项,例如。为您要搜索的每个字段准备一张特定的地图。如果您有很多对象,这会更快,因为在地图中搜索需要 O(1) 而遍历整个集合需要 O(N)。大 O 备忘单声明地图private Map<Long, Product> productsByID = new HashMap();private Map<String, Product> productsByCategory = new HashMap();初始化地图public ProductDaoImpl(){&nbsp; &nbsp; // Create the objects&nbsp; &nbsp; Product p1 = new Product(1L,"TV","Entertinement",10000F,250L,"This is best TV!");&nbsp; &nbsp; Product p2 = new Product(2L,"Computer","Technology",20000F,350L,"My Computer name Hp and SONY ViVo!");&nbsp; &nbsp; Product p3 = new Product(3L,"DeskTopComputer","Technology",15000F,150L,"My Desktop Computer name Accer and SONY ViVo!");&nbsp; &nbsp; //Assign the objects into the map by ids&nbsp; &nbsp; productsByID.put(1L, p1);&nbsp; &nbsp; productsByID.put(2L, p2);&nbsp; &nbsp; productsByID.put(3L, p3);&nbsp; &nbsp; //Assign the objects into the map by category&nbsp; &nbsp; productsByCategory.put(p1.getCategory(), p1);&nbsp; &nbsp; productsByCategory.put(p2.getCategory(), p2);&nbsp; &nbsp; productsByCategory.put(p3.getCategory(), p3);}使用您拥有的相同地图,并按照其他答案的解释几乎遍历所有值。最后,我不知道这是否只是一个练习代码并且您的值很少,在这种情况下,性能无关紧要,或者这是您的生产代码的开始,在这种情况下,您希望得到很多值。如果最新的是真的,可能您想将其建模为数据库(sql 或非 sql)并按您想要/需要的特定字段进行查询。
随时随地看视频慕课网APP

相关分类

Java
我要回答