统计对象ArrayList中某个属性的出现次数

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

Count occurrences in a Object ArrayList of a Attribute

问题

package main;

import java.util.ArrayList;
import java.util.Collections;

class Main {

    public static void main(String[] args) {
        ArrayList<Item> gear = new ArrayList<>(); // Corrected the ArrayList declaration

        gear.add(new Item("weapon", 1, 1));
        gear.add(new Item("weapon", 1, 1));
        gear.add(new Item("helmet", 1, 1));
        gear.add(new Item("ring", 1, 1));
        gear.add(new Item("weapon", 1, 1));
        gear.add(new Item("ring", 1, 1));

        System.out.println(Collections.frequency(getTypeList(gear), "weapon"));
        System.out.println(Collections.frequency(getTypeList(gear), "helmet"));
        System.out.println(Collections.frequency(getTypeList(gear), "ring"));
    }

    static ArrayList<String> getTypeList(ArrayList<Item> gear) {
        ArrayList<String> types = new ArrayList<>();
        for (Item item : gear) {
            types.add(item.getType());
        }
        return types;
    }

    class Item {
        String type;
        int weight;
        int strength;

        public Item(String type, int weight, int strength) {
            super();
            this.type = type;
            this.weight = weight;
            this.strength = strength;
        }

        public String getType() {
            return type;
        }
    }
}
英文:

How can I count the occurrences of the String type field/attribute that equals weapon in the stated ArrayList&lt;Item&gt;. I added three faulty formatted System.out.println();

package main;
import java.util.ArrayList;
import java.util.Collections;
class Main{
public static void main(String[] args) {
ArrayList&lt;Item&gt; gear;
gear.add(new Item(&quot;weapon&quot;, 1, 1));
gear.add(new Item(&quot;weapon&quot;, 1, 1));
gear.add(new Item(&quot;helmet&quot;, 1, 1));
gear.add(new Item(&quot;ring&quot;, 1, 1));
gear.add(new Item(&quot;weapon&quot;, 1, 1));
gear.add(new Item(&quot;ring&quot;, 1, 1));
System.out.println(Collections.frequency(gear.getType(), &quot;weapon&quot;));
System.out.println(Collections.frequency(gear.getType(), &quot;helmet&quot;));
System.out.println(Collections.frequency(gear.getType(), &quot;ring&quot;));
}
class Item{
String type;
int weight;
int strength;
public Item(String type, int weight, int strength) {
super();
this.type = type;
this.weight = weight;
this.strength = strength;
}
public String getType() {
return type;
}
}
}

Wanted output:

3
1
2

答案1

得分: 3

使用lambda表达式非常简单,

String gearType = "Weapon";
long fequency = gear.stream().filter(gearInput -> gearInput.getType().equalsIgnoreCase(gearType)).count();

你可以将这部分代码封装在一个函数中,并将gearType作为参数传递进去。

英文:

using lambdas its quite simple,

String gearType = &quot;Weapon&quot;;
long fequency = gear.stream().filter(gearInput -&gt; gearInput.getType().equalsIgnoreCase(gearType)).count();

You can wrap this within a function, passing gearType as parameter.

答案2

得分: 1

public class Item {
    String type;
    int weight;
    int strength;
    
    Item(String ty, int we, int st)
    {
        type = ty;
        weight = we;
        strength = st;
    }
    String getType()
    {
        return type;
    }
}

private int getFrequency(ArrayList<Item> list, String type)
{
    int counter = 0;
    for (Item object: list) {
        if (object.getType().equals(type))
            counter++;
    }
    return counter;
}

public static void main(String[] args)
{
   ArrayList<Item> gear = new ArrayList<Item>();
   gear.add( new Item("weapon",1,10) );
   gear.add( new Item("dog",2,23) );
   gear.add( new Item("none",4,44) );
   gear.add( new Item("car",1,88) );
   gear.add( new Item("weapon",65,20) );

   System.out.println("weapon elements: "+ this.getFrequency(gear,"weapon"));
   System.out.println("weapon elements: "+ this.getFrequency(gear,"helmet"));
   System.out.println("weapon elements: "+ this.getFrequency(gear,"ring"));
}
// Output: weapon elements: 2
英文:
public class Item {
String type;
int weight;
int strength;
Item(String ty, int we, int st)
{
type = ty;
weight = we;
strength = st;
}
String getType()
{
return type;
}
}
private int getFrequency(ArrayList&lt;Item&gt; list, String type)
{
int counter = 0;
for (Item object: list) {
if (object.getType().equals(type))
counter++;
}
return counter;
}
public static void main(String[] args)
{
ArrayList&lt;Item&gt; gear = new ArrayList&lt;Item&gt;();
gear.add( new Item(&quot;weapon&quot;,1,10) );
gear.add( new Item(&quot;dog&quot;,2,23) );
gear.add( new Item(&quot;none&quot;,4,44) );
gear.add( new Item(&quot;car&quot;,1,88) );
gear.add( new Item(&quot;weapon&quot;,65,20) );
System.out.println(&quot;weapon elements: &quot;+ this.getFrequency(gear,&quot;weapon&quot;));
System.out.println(&quot;weapon elements: &quot;+ this.getFrequency(gear,&quot;helmet&quot;));
System.out.println(&quot;weapon elements: &quot;+ this.getFrequency(gear,&quot;ring&quot;));
}
// Output: weapon elements: 2

huangapple
  • 本文由 发表于 2020年10月10日 19:04:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64292689.html
匿名

发表评论

匿名网友

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

确定