如何在javaHashMap中获得基于产品类别的产品详细信息



有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;
}
}

DatabaseClass.java

public class DatabaseClass {
private static Map<Long, Product> products=new HashMap<>();
public static Map<Long, Product> getProduct()
{
return products;
}
}

产品Dao.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(字符串类别(编写代码。

您可以迭代映射中的值集,并过滤以返回匹配产品的列表:

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(的更好做法

一种方法是如下迭代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;
}

您至少有两个选项,如.

  1. 为要搜索的每个字段提供一个特定的映射。这是如果你在map占用O(1(,而遍历整个集合占用O(N(。Big O备忘单

声明地图

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. 使用相同的地图正如其他答案所解释的那样,对所有值进行了多次迭代

最后,我不知道这是否只是一个练习代码,你的值很少——在这种情况下,性能没有那么重要——或者这是你的生产代码的开始——在那种情况下,你希望得到很多值。如果最新的结果是真的,那么您可能希望将其建模为一个DB(sql或非sql(,并根据您想要/需要的特定字段进行查询。

相关内容

  • 没有找到相关文章

最新更新