What is ArrayList?

Type-1:

Below is a Java program that takes an ArrayList of strings and a specific word as input, then counts the number of occurrences of that word in the list:


import java.util.ArrayList; 
import java.util.Scanner;
public class CountOccurrences {
public static void main(String[] args) {
// Create an ArrayList of strings 
 ArrayList<String> wordList = new ArrayList<>(); 
Scanner scanner = new Scanner(System.in);
// Add elements to the ArrayList 
 wordList.add("apple");
 wordList.add("banana"); 
 wordList.add("apple"); 
 wordList.add("orange"); 
 wordList.add("grape"); 
 wordList.add("apple"); 
// Input the word to count its occurrences
System.out.print("Enter a word to count its occurrences: ");
String wordToCount = scanner.next();
// Count occurrences of the word 
int occurrences = countOccurrences(wordList, wordToCount); 
// Output the result 
 System.out.println("Occurrences of '" + wordToCount + "': " + occurrences); 
 } 
// Method to count occurrences of a word in the ArrayList 
public static int countOccurrences(ArrayList<String> list, String word) {
int count = 0;
for (String str : list) { 
if (str.equals(word)) { 
 count++; 
 } 
 } 
return count; 
 }
 }


Example usage:

Suppose the ArrayList contains the words "apple", "banana", "apple", "orange", "grape", and "apple". If the user enters "apple" as the word to count, the program will output:

Enter a word to count its occurrences: apple

Occurrences of 'apple': 3

Explanation:

  1. We start by creating an ArrayList of strings called wordList and add some elements to it.
  2. The user is prompted to input the word they want to count.
  3. We use a method called countOccurrences that takes the ArrayList and the word to count as parameters and returns the number of occurrences of that word in the list.
  4. The method uses a loop to iterate through the ArrayList and checks if each element is equal to the word being searched. If it matches, the count is incremented.
  5. The result is then printed to the console. In this example, "apple" appears 3 times in the ArrayList.
Type-2:

Another way to count the occurrences of a specific word in an ArrayList of strings is by using Java Streams. Java Streams provide a more concise and functional way to perform operations on collections. Here's the updated Java program using Streams:

import java.util.ArrayList; 
import java.util.Scanner;
public class CountOccurrences
public static void main(String[] args) {
// Create an ArrayList of strings 
 ArrayList<String> wordList = new ArrayList<>(); 
Scanner scanner = new Scanner(System.in);
// Add elements to the ArrayList
wordList.add("apple"); 
wordList.add("banana");
wordList.add("apple");
 wordList.add("orange"); 
 wordList.add("grape");
 wordList.add("apple");
// Input the word to count its occurrences 
 System.out.print("Enter a word to count its occurrences: ");
String wordToCount = scanner.next(); 
// Count occurrences of the word using Streams
long occurrences = wordList.stream() .filter(word -> word.equals(wordToCount)) .count();
// Output the result 
 System.out.println("Occurrences of '" + wordToCount + "': " + occurrences); 
 } 
}
Example usage and output remain the same as in the previous implementation:


Enter a word to count its occurrences: apple
Occurrences of 'apple': 3

Explanation:
  1. In this version, we use Java Streams to work with the ArrayList.
  2. We use the stream() method on the ArrayList to convert it into a Stream of elements.
  3. Then, we use the filter() method on the Stream to filter out only the elements that match the specified word (word.equals(wordToCount)).
  4. Finally, we call the count() method on the filtered Stream to count the occurrences of the specified word.

Using Streams, we can perform operations on collections in a more functional style, making the code concise and readable. In this case, we use the power of Streams to easily count the occurrences of a word in the ArrayList of strings.