如何在Java HashMap中基于产品类别获取产品详细信息?

时间:2018-07-28 16:19:04

标签: java model hashmap dao

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

public List<Product> getProductsBasedOnCategory(String category)
{

}

请找到以下代码。

Product.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;
    }

}

DatabaseClass.java

public class DatabaseClass {

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

    public static Map<Long, Product> getProduct()
    {
        return products;
    }

}

ProductDao.java

private Map<Long, Product> products=DatabaseClass.getProduct();

public ProductDaoImpl()
{
    products.put(1L, new Product(1L,"TV","Entertinement",10000F,250L,"This is best TV!"));
    products.put(2L, new Product(2L,"Computer","Technology",20000F,350L,"My Computer name Hp and SONY ViVo!"));
    products.put(3L, new Product(3L,"DeskTopComputer","Technology",15000F,150L,"My Desktop Computer name Accer and SONY ViVo!"));
}

//Get All products
public List<Product> getAllProducts() {

    return new ArrayList<Product>(products.values());
}

//Get product by product id
public Product getProduct(long pid) {

    return products.get(pid);
}

//To Add the products 
public Product addProduct(Product product) {
    product.setPid(products.size()+1);
    products.put(product.getPid(), product);
    return product;
}

//Update the product
public Product updateProduct(Product product) {
    if(product.getPid()<=0)
    {
        return null;
    }
    products.put(product.getPid(), product);
    return product;
}

// Delete the product
public Product deleteProduct(long pid) {

    return products.remove(pid);

}



//Get the product by category
public List<Product> getProductByCategory(String category) {

    if(products.size()<=0)
    {
        return null;
    }

    else if(category.equals(products.get(Product))
    {



    }

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

3 个答案:

答案 0 :(得分:1)

您可以遍历地图中的一组值,并进行过滤以返回匹配产品的列表:

public List<Product> getProductByCategory(String category) {

    if(products.size() == 0){
        return new ArrayList<>();
    }

    return this.products.values().stream()
       .filter(product -> product.getCategory().equals(category))
       .collect(Collectors.toList());
}

您也可以为此使用for循环:

public List<Product> getProductByCategory(String category) {

    List<Product> ret = new ArrayList<>();

    if(products.size() == 0){
        return ret;
    }

    for(Product p: this.products.values()) {
        if(p.getCategory().equals(category))
            ret.add(p);
    }

    return ret;
}

请注意,如果产品映射为空,我将返回一个空的ArrayList。对于集合返回类型(而不是返回null),这是一种更好的做法

答案 1 :(得分:0)

一种方法是如下遍历Hashmap:

public List<Product> getProductsBasedOnCategory(String category)
{
    List<Product> list = new ArrayList<Product>();

    if (products.size()<=0) {
        return list;
    }

    products.entrySet().stream().forEach((entry) -> {
        if (((Product) entry.getValue()).getCategory().equals(category)) {
            list.add(entry.getValue())
        }
    });

    return list;

}

答案 2 :(得分:0)

您至少有2个选项,例如。

  1. 为每个要搜索的字段都有一个特定的地图。这是 如果您在搜索对象后有很多对象,将会更快 map遍历整个collection需要O(1) 上)。 Big O Cheat Sheet

声明地图

private Map<Long, Product> productsByID = new HashMap();
private Map<String, Product> productsByCategory = new HashMap();

初始化地图

public ProductDaoImpl()
{
    // Create the objects
    Product p1 = new Product(1L,"TV","Entertinement",10000F,250L,"This is best TV!");
    Product p2 = new Product(2L,"Computer","Technology",20000F,350L,"My Computer name Hp and SONY ViVo!");
    Product p3 = new Product(3L,"DeskTopComputer","Technology",15000F,150L,"My Desktop Computer name Accer and SONY ViVo!");

    //Assign the objects into the map by ids
    productsByID.put(1L, p1);
    productsByID.put(2L, p2);
    productsByID.put(3L, p3);

    //Assign the objects into the map by category
    productsByCategory.put(p1.getCategory(), p1);
    productsByCategory.put(p2.getCategory(), p2);
    productsByCategory.put(p3.getCategory(), p3);
}
  1. 使用您拥有的相同且漂亮的地图     正如其他答案所解释的那样,对所有值进行了大量迭代。

最后,我不知道这是否只是一个练习代码,您没有多少值,在这种情况下,性能无关紧要,或者这是生产代码的开始,因此您希望很多价值观。 如果最新的为真,则可能要将其建模到数据库(sql或非sql)中,并按所需/需要的特定字段进行查询。