Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get amount of specific element in List

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
like image 863
Frithjof Avatar asked Jan 22 '14 19:01

Frithjof


People also ask

How do you count certain elements in a list Python?

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.

How many items are in a list Python?

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() .

How do you count the number of repeated items in a list Python?

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.


2 Answers

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();
like image 164
Alexis C. Avatar answered Sep 28 '22 23:09

Alexis C.


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

like image 43
Donald Raab Avatar answered Sep 28 '22 23:09

Donald Raab