英文:
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<Item>
. 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<Item> gear;
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(gear.getType(), "weapon"));
System.out.println(Collections.frequency(gear.getType(), "helmet"));
System.out.println(Collections.frequency(gear.getType(), "ring"));
}
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 = "Weapon";
long fequency = gear.stream().filter(gearInput -> 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<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论