我有一组对象,其中包括已售出的产品以及在该订单中售出的数量。可能有一种产品多次出现。我正在尝试创建一个代码,该代码将返回最受欢迎的产品。例如对象1 10,对象2 15,对象1 5,对象3 4,它应该返回对象1和数字15(10 + 5)。订单具有产品名称和数量以及吸气剂和装模器的参数。
我的想法是使用一个集合,该集合将消除所有重复项(下面的代码示例),但是事实证明是破产,因为在这种情况下集合不起作用,我什至无法用一套完成它。我不知道还能尝试什么。谢谢!
public class Orders {
private Product[] orders;
public Orders() {
orders = new order[0];
}
public void add(Order order) {
Order[] newOrder = Arrays.copyOf(orders,
orders.length + 1);
newOrder[newOrder.length - 1] = order;
orders = newOrders;
}
// the method described starts here
public Product findTopProduct() {
int[] array;
Set<String> set = new HashSet<>();
for (int i=0; i<orders.length; i++) {
set.add(orders[i].getProductName());
}
array = new int[set.size()];
for (int i=0; i<orders.length; i++) {
for (int i1=0; i1<set.size();i1++) {
}
}
}
}
答案 0 :(得分:1)
考虑更广泛的探索(找到最大,最小,平均...)。有一个强大的类叫做IntSummaryStatistics
您可以尝试理解它并“提取”所需的数据。
我将为您提供一个示例,说明如何使用Map
集合以及一些示例数据的输出:
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.Map;
import java.util.stream.Collectors;
public class Answer {
static class Product{
private String name;
private int noOfSales;
public Product(String name, int noOfSales){
this.name = name;
this.noOfSales = noOfSales;
}
public String getName() {
return name;
}
public int getNoOfSales(){
return noOfSales;
}
}
public static void main(String[] args) {
Product[] products = {
new Product("tea", 4),
new Product("apple", 12),
new Product("tea", 15)
};
Map<String, IntSummaryStatistics> mapOfProducts =
Arrays.stream(products)
.collect(Collectors.groupingBy(
Product::getName,
Collectors.summarizingInt(Product::getNoOfSales)));
System.out.println(mapOfProducts);
}
}
输出为:
{apple = IntSummaryStatistics {count = 1,sum = 12,min = 12,平均值= 12.000000,max = 12},tea = IntSummaryStatistics {count = 2,sum = 19,min = 4,平均值= 9.500000,max = 15}}
请注意,仅找到最大reducing method。 (其中包含示例代码)
答案 1 :(得分:0)
最好使用Map数据结构来解决。您需要映射每个产品名称到您所有订单中的数量。我在Java中使用HashMap,但您还需要了解Map
数据结构以及如何/何时使用它,这就是我将其实现的方式(我假设您是{{1 }}类还有另一个名为Product
)的属性:
quantity
答案 2 :(得分:0)
尝试一下:
public static Product findTopProduct(Product[] orders) {
Map<String,List<Product>> var0 = Stream.of(orders)
.collect(Collectors.groupingBy(Product::getName));
int maxCount = Integer.MIN_VALUE;
Product top = null;
for(Map.Entry<String,List<Product>>entry : var0.entrySet()) {
int size = entry.getValue().size();
if (size > maxCount) {
top = entry.getValue().get(0);
maxCount = size;
}
}
return top;
}
答案 3 :(得分:0)
此getTopProduct
方法返回具有产品名称和总数量的新Product
。
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TestClass {
public static void main(String[] args) {
//sample list of objects containing productName and quantity fields
List<Product> productList = new ArrayList<>();
Product p1 = new Product("toy", 10);
Product p2 = new Product("car", 14);
Product p3 = new Product("food", 2);
Product p4 = new Product("toy", 6);
productList.add(p1);
productList.add(p2);
productList.add(p3);
productList.add(p4);
Product topProduct = getTopProduct(productList);
System.out.println("Most sold product: " + topProduct.getProductName() + ", " + topProduct.getQuantity());
}
private static Product getTopProduct(List<Product> productList) {
//map to hold products and total quantity
Map<String, Integer> map = new HashMap<>();
//populate the map
for(Product product : productList) {
if(map.containsKey(product.getProductName())) {
//if product already exists in map, add on the quantity
map.put(product.getProductName(), map.get(product.getProductName()) + product.getQuantity());
}else {
//if product doesnt exist in map yet, add it
map.put(product.getProductName(), product.getQuantity());
}
}
//find the highest value in the map which is the product with highestQuantity
int highestQuantity = Collections.max(map.values());
String productWithHighestQuantity = "";
//go through map to find which product had the highest quantity
for (Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue()==highestQuantity) {
productWithHighestQuantity = entry.getKey();
break;
}
}
return new Product(productWithHighestQuantity, highestQuantity);
}
}