Friday, January 17, 2025
HomeTechPrint ArrayList - java

Print ArrayList – java

In Java, there are several ways to print the elements of an ArrayList. Below are examples showcasing different approaches:

1. Using toString() Method

The toString() method of the ArrayList class returns a string representation of the list.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        System.out.println(list); // Prints: [Apple, Banana, Cherry]
    }
}

2. Using a for Loop

Iterate over the elements using a traditional for loop.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}

Output:

Apple
Banana
Cherry

3. Using an Enhanced for Loop

Use a for-each loop for a more concise way to iterate over elements.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        for (String item : list) {
            System.out.println(item);
        }
    }
}

4. Using Iterator

Use an Iterator object to traverse the ArrayList.

import java.util.ArrayList;
import java.util.Iterator;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

5. Using Streams (Java 8 and Above)

You can use the forEach method with a lambda expression.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        list.forEach(item -> System.out.println(item));
    }
}

6. Using join() Method (to Print as a Comma-Separated String)

You can join the elements into a single string.

import java.util.ArrayList;
import java.util.StringJoiner;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        System.out.println(String.join(", ", list)); // Prints: Apple, Banana, Cherry
    }
}

7. Using Streams with Collectors.joining() (Java 8 and Above)

You can use the Collectors.joining() method for advanced string joining.

import java.util.ArrayList;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        String result = list.stream().collect(Collectors.joining(", "));
        System.out.println(result); // Prints: Apple, Banana, Cherry
    }
}

Summary

  • Simple and Quick: Use toString() for basic printing.
  • Iterative: Use for loops or Iterator to process elements individually.
  • Functional: Use streams or lambdas for modern, concise code.
See also  How to style a clicked button in CSS

Choose the method that best suits your needs!

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x