英文:
How to find Frequency of word in arrayList of type extended class
问题
这是一个餐厅类,其中存储了不同颜色的收据。我正在尝试使用 Collections.Frequency
来获取每种收据颜色的频率。
注意:必须使用 Collections.Frequency
。
public static void main(String[] args) {
ArrayList<Resturant> list1 = new ArrayList<>();
list1.add(new Drive_Through(4, 20, 2));
list1.add(new Walk_in(5, 15, 10, 10));
list1.add(new Drive_Through(3, 5, 5));
list1.add(new Walk_in(1, 3, 10, 20));
System.out.println("\n\n\n----绿色收据数量----\n" + Collections.frequency(list1, "绿色"));
List contents:
`收据编号:1,日期/时间:27/08/20,收据颜色:绿色,总项目数:3,总税费:0.1,总付款:2.64,总折扣:0.6600000000000001`
`收据编号:3,日期/时间:27/08/20,收据颜色:白色,总项目数:5,总税费:0.05,总付款:5.25`
`收据编号:4,日期/时间:27/08/20,收据颜色:白色,总项目数:20,总税费:0.02,总付款:20.4`
`收据编号:5,日期/时间:27/08/20,收据颜色:绿色,总项目数:15,总税费:0.1,总付款:14.85,总折扣:1.6500000000000001`
英文:
This a Restaurant Class which stores different colours of Receipts. I'm trying to get the Frequency of each Receipt colour using Collections.Frequency
note the use of Collections.Frequency
is a must.
public static void main(String[] args) {
ArrayList<Resturant> list1 = new ArrayList<>();
list1.add(new Drive_Through(4, 20, 2));
list1.add(new Walk_in(5, 15, 10, 10));
list1.add(new Drive_Through(3, 5, 5));
list1.add(new Walk_in(1, 3, 10,20));
System.out.println("\n\n\n----Number of Green Recipet----\n"+Collections.frequency(list1, "Green"));
List contents:
Receipt No.: 1, Date/Time: 27/08/20, Recipt Colour: Green, Total items: 3, Total Tax: 0.1, Total Payment: 2.64, Total Discount: 0.6600000000000001
Receipt No.: 3, Date/Time: 27/08/20, Recipt Colour: White, Total items: 5, Total Tax: 0.05, Total Payment: 5.25
Receipt No.: 4, Date/Time: 27/08/20, Recipt Colour: White, Total items: 20, Total Tax: 0.02, Total Payment: 20.4
Receipt No.: 5, Date/Time: 27/08/20, Recipt Colour: Green, Total items: 15, Total Tax: 0.1, Total Payment: 14.85, Total Discount: 1.6500000000000001]
答案1
得分: 0
如 `Collections.frequency` 检查给定对象与集合中所有元素的相等性,因此可以通过覆盖 `equals` 方法来实现,仅比较一个属性(在给定情况下为 `color`):
```java
class Receipt {
// 属性、构造函数、getter、setter
@Override
public boolean equals(Object o) {
if (null == o) {
return false;
}
Color otherColor = null;
if (o instanceof Color) {
otherColor = (Color) o;
} else if (o instanceof Receipt) {
otherColor = ((Receipt) o).getColor();
}
return null != this.color && this.color.equals(otherColor);
}
@Override
public int hashCode() {
return this.color.hashCode();
}
}
// 使用 Collections.frequency
List<Receipt> receipts = prepareReceipts();
int greenCount = Collections.frequency(receipts, Color.GREEN);
这只能用作所需情况的“硬编码”解决方案。
如果需要根据另一个属性或多个属性计算频率,则最好使用建议的方法实现装饰器/一组装饰器来计算频率。
<details>
<summary>英文:</summary>
As `Collections.frequency` checks equality of the given object to all the elements of collection, this could be implemented by overriding `equals` method to compare only one property (`color` in the given case):
```java
class Receipt {
// properties, constructor(s), getters, setters
@Override
public boolean equals(Object o) {
if (null == o) {
return false;
}
Color otherColor = null;
if (o instanceof Color) {
otherColor = (Color) o;
} else if (o instanceof Receipt) {
otherColor = ((Receipt) o).getColor();
}
return null != this.color && this.color.equals(otherColor);
}
@Override
public int hashCode() {
return this.color.hashCode;
}
}
// using Collections.frequency
List<Receipt> receipts = prepareReceipts();
int greenCount = Collections.frequency(receipts, Color.GREEN);
This can be used only as a "hardcoded" solution for the required case.
If the frequency needs to be calculated by another property or properties, it's better to implement a decorators/set of decorators using the suggested approach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论