需求是这个样子的:
代码如下
任务1代码:
package com.hidata.hiops.paas.demo; import java.util.ArrayList; import java.util.List; /** * 任务一、产品基本信息 */ public class Product { private static List<Product> productList = new ArrayList<>(); private String name; private String price; private String profile; private String score; static { productList.add(new Product("xiaomi8","2599","well-known","100")); productList.add(new Product("xiaomi9","3599","well-known","90")); productList.add(new Product("xiaomi10","5599","well-known","99")); productList.add(new Product("xiaomi12","7599","well-known","100")); } public Product() {} public Product(String name, String price, String profile, String score) { this.name = name; this.price = price; this.profile = profile; this.score = score; } public String getName() { return name; } public static void getProductInfo(String typeName){ List<Product> list = Product.productList; list.stream().filter(obj -> obj.getName().equals(typeName)).forEach(e ->System.out.println(e)); } @Override public String toString() { return "Product{" + "name='" + name + '\'' + ", price='" + price + '\'' + ", profile='" + profile + '\'' + ", score='" + score + '\'' + '}'; } public static void main(String[] args) { Product.getProductInfo("xiaomi8"); Product.getProductInfo("xiaomi9"); } }
运行结果
Product{name='xiaomi8', price='2599', profile='well-known', score='100'} Product{name='xiaomi9', price='3599', profile='well-known', score='90'} Process finished with exit code 0
任务2 代码:
package com.hidata.hiops.paas.demo; import com.ruoyi.framework.web.domain.server.Sys; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 任务二、分类下的产品信息 */ public class ProductCategory { private String categoryName;//产品分类名称 private List<ProductItme> productList;//产品列表 private static List<ProductCategory> categoryList = new ArrayList<>(); static { List<ProductItme> productList = new ArrayList<>(); productList.add(new ProductItme("XIAOMI")); productList.add(new ProductItme("HUAWEI")); productList.add(new ProductItme("OPPO")); categoryList.add(new ProductCategory("tel",productList)); List<ProductItme> productList2 = new ArrayList<>(); productList2.add(new ProductItme("HP")); productList2.add(new ProductItme("Lenovo")); productList2.add(new ProductItme("DELL")); categoryList.add(new ProductCategory("notebook",productList2)); } public ProductCategory(String categoryName, List<ProductItme> productList) { this.categoryName = categoryName; this.productList = productList; } public String getCategoryName() { return categoryName; } @Override public String toString() { return "ProductCategory{" + "分类名称='" + categoryName + '\'' + ", productList=" + productList + '}'; } public static void getProductItmeList(String categoryName) { List<ProductCategory> categories = ProductCategory.categoryList; categories.stream().filter(obj -> obj.getCategoryName().equals(categoryName)).forEach(e -> System.out.println(e)); } public static void main(String[] args) { ProductCategory.getProductItmeList("notebook"); System.out.println("================================================"); ProductCategory.getProductItmeList("tel"); } } class ProductItme{ private String pName; public ProductItme(String pName) { this.pName = pName; } @Override public String toString() { return "产品{" + "pName='" + pName + '\'' + '}'; } }
运行结果
ProductCategory{分类名称='notebook', productList=[产品{pName='HP'}, 产品{pName='Lenovo'}, 产品{pName='DELL'}]} ================================================ ProductCategory{分类名称='tel', productList=[产品{pName='XIAOMI'}, 产品{pName='HUAWEI'}, 产品{pName='OPPO'}]} Process finished with exit code 0
任务3 代码:
package com.hidata.hiops.paas.demo; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * 任务三:分类搜索 */ public class ProductCategorySearch { private String categoryName;//产品分类 private static List<ProductCategorySearch> categoryList = new ArrayList<>(); static { categoryList.add(new ProductCategorySearch("A")); categoryList.add(new ProductCategorySearch("B")); categoryList.add(new ProductCategorySearch("X")); categoryList.add(new ProductCategorySearch("Y")); categoryList.add(new ProductCategorySearch("Z")); } public ProductCategorySearch(String categoryName) { this.categoryName = categoryName; } public String getCategoryName() { return categoryName; } public static void getProductItmeList(String categoryName) { List<ProductCategorySearch> newProductCategorys = new ArrayList<>(); List<ProductCategorySearch> categories = ProductCategorySearch.categoryList; String [] arr = categoryName.split("\\s+"); for (String name : arr) { for (ProductCategorySearch category : categories) { if(name.equals(category.getCategoryName())){ newProductCategorys.add(category); } } } reverse(newProductCategorys); } private static void reverse(List<ProductCategorySearch> newProductCategorys) { Collections.reverse(newProductCategorys); System.out.println(newProductCategorys); } @Override public String toString() { return "ProductCategorySearch{" + "categoryName='" + categoryName + '\'' + '}'; } public static void main(String[] args) { ProductCategorySearch.getProductItmeList("B A"); System.out.println("================================================"); ProductCategorySearch.getProductItmeList("X Y Z"); System.out.println("================================================"); ProductCategorySearch.getProductItmeList("A B XY"); } }
运行结果
[ProductCategorySearch{categoryName='A'}, ProductCategorySearch{categoryName='B'}] ================================================ [ProductCategorySearch{categoryName='Z'}, ProductCategorySearch{categoryName='Y'}, ProductCategorySearch{categoryName='X'}] ================================================ [ProductCategorySearch{categoryName='B'}, ProductCategorySearch{categoryName='A'}] Process finished with exit code 0