What is a fast way to count how many times a given value is present in an ArrayList?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ArrayList<Integer> myArrayList = new ArrayList<>;
for (int i = 0; i < 5; i++) {
myArrayList.add(i);
}
myArrayList.add(2);
myArrayList.add(2);
myArrayList.add(3);
//arryaylist contains: (0,1,2,3,4,2,2,3)
</code>
<code>ArrayList<Integer> myArrayList = new ArrayList<>; for (int i = 0; i < 5; i++) { myArrayList.add(i); } myArrayList.add(2); myArrayList.add(2); myArrayList.add(3); //arryaylist contains: (0,1,2,3,4,2,2,3) </code>
ArrayList<Integer> myArrayList = new ArrayList<>;
for (int i = 0; i < 5; i++) {
    myArrayList.add(i);
}
myArrayList.add(2);
myArrayList.add(2);
myArrayList.add(3);

//arryaylist contains: (0,1,2,3,4,2,2,3)

I want to give input to a method, and for it to return amount of times the input is seen in the array. For example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public int someMethod(int searchVal) {
//code
//returns int
}
System.out.println(someMethod(2));
//prints 3
</code>
<code>public int someMethod(int searchVal) { //code //returns int } System.out.println(someMethod(2)); //prints 3 </code>
public int someMethod(int searchVal) {
    //code
    //returns int
}
    
System.out.println(someMethod(2));
//prints 3

I can think of a way like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public int someMethod(int searchVal) {
int counter = 0;
for (int i : myArraylist) {
if (i == searchVal) {
counter +=1;
}
}
return counter;
}
</code>
<code>public int someMethod(int searchVal) { int counter = 0; for (int i : myArraylist) { if (i == searchVal) { counter +=1; } } return counter; } </code>
public int someMethod(int searchVal) {
    int counter = 0;
    for (int i : myArraylist) {
        if (i == searchVal) {
            counter +=1;
        } 
    }
    return counter; 
}

But the arraylist I need this method for is very long and I’m going to use it multiple times for each of the possible values in the ArrayList. Meaning i.e. someMethod(0), someMethod(1), someMethod(2), someMethod(3), someMethod(4).

In the example I have int, but I will use ArrayList. In form of: ArrayList<ArrayList< Integer >> myArrayList = ((1,2) , (2,1), (1,2), (3,0)…) So .sort() will not work.

  1. Is there a faster way to generally calculate how many times an individual value is present in an ArrayList?
  2. Since I’m going to use this on all values, is there a faster way of doing this than having method that compares each value?

7

To count the occurrences of all list values, you can use a stream with Collectors.groupingBy and Collectors.counting

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>myArrayList.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
</code>
<code>myArrayList.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); </code>
myArrayList.stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

This groups the values of the list by their identity, and then for each group, counts how many elements are in the group, returning a map with the list values as the keys, and the occurrence counts as the values.

In your example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var myArrayList = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
myArrayList.add(i);
}
myArrayList.add(2);
myArrayList.add(2);
myArrayList.add(3);
Map<Integer, Long> counts =
myArrayList.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(counts);
</code>
<code>var myArrayList = new ArrayList<Integer>(); for (int i = 0; i < 5; i++) { myArrayList.add(i); } myArrayList.add(2); myArrayList.add(2); myArrayList.add(3); Map<Integer, Long> counts = myArrayList.stream() .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(counts); </code>
var myArrayList = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
    myArrayList.add(i);
}
myArrayList.add(2);
myArrayList.add(2);
myArrayList.add(3);

Map<Integer, Long> counts =
        myArrayList.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(counts);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{0=1, 1=1, 2=3, 3=2, 4=1}
</code>
<code>{0=1, 1=1, 2=3, 3=2, 4=1} </code>
{0=1, 1=1, 2=3, 3=2, 4=1}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật