如何在类型为扩展类的数组列表中找到单词的频率

huangapple go评论87阅读模式
英文:

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&lt;Resturant&gt; list1 = new ArrayList&lt;&gt;();
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(&quot;\n\n\n----Number of Green Recipet----\n&quot;+Collections.frequency(list1, &quot;Green&quot;));

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 &amp;&amp; this.color.equals(otherColor);
}
@Override
public int hashCode() {
return this.color.hashCode;
}
}
// using Collections.frequency
List&lt;Receipt&gt; 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.

huangapple
  • 本文由 发表于 2020年8月27日 21:01:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63616569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定