Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the occurrences of items in ArrayList

I have a java.util.ArrayList<Item> and an Item object.

Now, I want to obtain the number of times the Item is stored in the arraylist.

I know that I can do arrayList.contains() check but it returns true, irrespective of whether it contains one or more Items.

Q1. How can I find the number of time the Item is stored in the list?

Q2. Also, If the list contains more than one Item, then how can I determine the index of other Items because arrayList.indexOf(item) returns the index of only first Item every time?

like image 538
Yatendra Avatar asked Apr 15 '10 16:04

Yatendra


People also ask

How do you count the number of occurrences in an ArrayList in Java?

Using a Set The idea is to get distinct elements in the list by inserting all elements in the set & then call static method frequency(Collection<?> c, Object o) provided by the Collections class for each distinct element. frequency() returns the total number of occurrences of the specified element in the list.

How do you count elements in an ArrayList?

The size of an ArrayList can be obtained by using the java. util. ArrayList. size() method as it returns the number of elements in the ArrayList i.e. the size.

How do you count occurrences of a value in a list?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.


2 Answers

You can use Collections class:

public static int frequency(Collection<?> c, Object o)

Returns the number of elements in the specified collection equal to the specified object. More formally, returns the number of elements e in the collection such that (o == null ? e == null : o.equals(e)).

If you need to count occurencies of a long list many times I suggest you to use an HashMap to store the counters and update them while you insert new items to the list. This would avoid calculating any kind of counters.. but of course you won't have indices.

HashMap<Item, Integer> counters = new HashMap<Item, Integer>(5000);
ArrayList<Item> items = new ArrayList<Item>(5000);

void insert(Item newEl)
{
   if (counters.contains(newEl))
     counters.put(newEl, counters.get(newEl)+1);
   else
     counters.put(newEl, 1);

   items.add(newEl);
 }

A final hint: you can use other collections framework (like Apache Collections) and use a Bag datastructure that is described as

Defines a collection that counts the number of times an object appears in the collection.

So exactly what you need..

like image 120
Jack Avatar answered Sep 23 '22 23:09

Jack


This is easy to do by hand.

public int countNumberEqual(ArrayList<Item> itemList, Item itemToCheck) {
    int count = 0;
    for (Item i : itemList) {
        if (i.equals(itemToCheck)) {
          count++;
        }
    }
    return count;
}

Keep in mind that if you don't override equals in your Item class, this method will use object identity (as this is the implementation of Object.equals()).

Edit: Regarding your second question (please try to limit posts to one question apiece), you can do this by hand as well.

public List<Integer> indices(ArrayList<Item> items, Item itemToCheck) {
    ArrayList<Integer> ret = new ArrayList<Integer>();
    for (int i = 0; i < items.size(); i++) {
        if (items.get(i).equals(itemToCheck)) {
            ret.add(i);
        }
    }
    return ret;
}
like image 39
danben Avatar answered Sep 23 '22 23:09

danben