I'm looking for a quick method to find the amount of elements of a List
that are one specific element:
List<String> list = new ArrayList<String>();
list.add("apple");
list.add("banana");
list.add("apple");
list.add("kiwi");
// I'm looking for a method as List.amountOf(Object obj):
list.amountOf("apple"); // should return 2
list.amountOf("kiwi"); // should return 1
list.amountOf("pear"); // should return 0
The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.
If a two-dimensional list (a list of lists) is passed directly to len() , the number of lists stored as elements is returned. The number of items in each list (the number of items in each row) can be obtained using the list comprehensions. The total number of items can be calculated with sum() .
If you want to count duplicates for a given element then use the count() function. Use a counter() function or basics logic combination to find all duplicated elements in a list and count them in Python.
You can use Collections.frequency
:
int amountOfApple = Collections.frequency(list,"apple");
With Java 8 you will also be able to do this using streams :
long amountOfApple = list.stream().filter(s -> "apple".equals(s)).count();
If you use Eclipse Collections you can use either a MutableBag
or MutableList
, depending on whether order matters for the collection.
// If order doesn't matter
MutableBag<String> bag = Bags.mutable.with("apple", "banana", "apple", "kiwi");
// O(1) for bag.occurrencesOf()
Assert.assertEquals(2, bag.occurrencesOf("apple"));
Assert.assertEquals(1, bag.occurrencesOf("kiwi"));
Assert.assertEquals(0, bag.occurrencesOf("pear"));
// If order does matter
MutableList<String> list = Lists.mutable.with("apple", "banana", "apple", "kiwi");
// O(n) for collection.count()
// Java 5 - 7
Assert.assertEquals(2, list.count(Predicates.equal("apple")));
Assert.assertEquals(1, list.count(Predicates.equal("kiwi")));
Assert.assertEquals(0, list.count(Predicates.equal("pear")));
// using Java 8 Lambdas
Assert.assertEquals(2, list.count(fruit -> fruit.equals("apple")));
Assert.assertEquals(1, list.count(fruit -> fruit.equals("kiwi")));
Assert.assertEquals(0, list.count(fruit -> fruit.equals("pear")));
// using Java 8 Method References
Assert.assertEquals(2, list.count("apple"::equals));
Assert.assertEquals(1, list.count("kiwi"::equals));
Assert.assertEquals(0, list.count("pear"::equals));
// O(n) for collection.countWith()
// using Java 8 Method References
Assert.assertEquals(2, list.countWith(Object::equals, "apple"));
Assert.assertEquals(1, list.countWith(Object::equals, "kiwi"));
Assert.assertEquals(0, list.countWith(Object::equals, "pear"));
Note: I am a committer for Eclipse Collections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With