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

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

How to find Frequency of word in arrayList of type extended class

问题

这是一个餐厅类,其中存储了不同颜色的收据。我正在尝试使用 Collections.Frequency 来获取每种收据颜色的频率。

注意:必须使用 Collections.Frequency

  1. public static void main(String[] args) {
  2. ArrayList<Resturant> list1 = new ArrayList<>();
  3. list1.add(new Drive_Through(4, 20, 2));
  4. list1.add(new Walk_in(5, 15, 10, 10));
  5. list1.add(new Drive_Through(3, 5, 5));
  6. list1.add(new Walk_in(1, 3, 10, 20));
  7. System.out.println("\n\n\n----绿色收据数量----\n" + Collections.frequency(list1, "绿色"));
  8. List contents:
  9. `收据编号1日期/时间27/08/20收据颜色绿色总项目数3总税费0.1总付款2.64总折扣0.6600000000000001`
  10. `收据编号3日期/时间27/08/20收据颜色白色总项目数5总税费0.05总付款5.25`
  11. `收据编号4日期/时间27/08/20收据颜色白色总项目数20总税费0.02总付款20.4`
  12. `收据编号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.

  1. public static void main(String[] args) {
  2. ArrayList&lt;Resturant&gt; list1 = new ArrayList&lt;&gt;();
  3. list1.add(new Drive_Through(4, 20, 2));
  4. list1.add(new Walk_in(5, 15, 10, 10));
  5. list1.add(new Drive_Through(3, 5, 5));
  6. list1.add(new Walk_in(1, 3, 10,20));
  7. 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

  1. `Collections.frequency` 检查给定对象与集合中所有元素的相等性因此可以通过覆盖 `equals` 方法来实现仅比较一个属性在给定情况下为 `color`):
  2. ```java
  3. class Receipt {
  4. // 属性、构造函数、getter、setter
  5. @Override
  6. public boolean equals(Object o) {
  7. if (null == o) {
  8. return false;
  9. }
  10. Color otherColor = null;
  11. if (o instanceof Color) {
  12. otherColor = (Color) o;
  13. } else if (o instanceof Receipt) {
  14. otherColor = ((Receipt) o).getColor();
  15. }
  16. return null != this.color && this.color.equals(otherColor);
  17. }
  18. @Override
  19. public int hashCode() {
  20. return this.color.hashCode();
  21. }
  22. }
  23. // 使用 Collections.frequency
  24. List<Receipt> receipts = prepareReceipts();
  25. int greenCount = Collections.frequency(receipts, Color.GREEN);

这只能用作所需情况的“硬编码”解决方案。

如果需要根据另一个属性或多个属性计算频率,则最好使用建议的方法实现装饰器/一组装饰器来计算频率。

  1. <details>
  2. <summary>英文:</summary>
  3. 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):
  4. ```java
  5. class Receipt {
  6. // properties, constructor(s), getters, setters
  7. @Override
  8. public boolean equals(Object o) {
  9. if (null == o) {
  10. return false;
  11. }
  12. Color otherColor = null;
  13. if (o instanceof Color) {
  14. otherColor = (Color) o;
  15. } else if (o instanceof Receipt) {
  16. otherColor = ((Receipt) o).getColor();
  17. }
  18. return null != this.color &amp;&amp; this.color.equals(otherColor);
  19. }
  20. @Override
  21. public int hashCode() {
  22. return this.color.hashCode;
  23. }
  24. }
  25. // using Collections.frequency
  26. List&lt;Receipt&gt; receipts = prepareReceipts();
  27. 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:

确定