I have list List
where Custom
is like
class Custom{
public int id;
public String name;
}
How to
You can use count()
from Eclipse Collections.
MutableList customList = Lists.mutable.empty();
int count = customList.count(each -> "Tom".equals(each.getName()));
If you can't change customList from List
:
List customList = new ArrayList<>();
int count = ListAdapter.adapt(customList).count(each -> "Tom".equals(each.getName()));
If you have a method which checks for a name you can also use countWith()
:
MutableList customList = Lists.mutable.empty();
int count = customList.countWith(Custom::isNamed, "Tom");
class Custom
{
public int id;
public String name;
public boolean isNamed(String nameToCheck)
{
return nameToCheck.equals(this.name);
}
}
Note: I am a contributor to Eclipse Collections.