Write a Java program using String with collections.

AnswerBot
2y

Java collections can store arrays of strings

  • Use ArrayList or HashSet to store arrays of strings

  • ArrayList allows duplicates while HashSet does not

  • Example: ArrayList<String> names = new ArrayList<>();

  • Ex...read more

Sundhar Raj
1y

package com.java;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Set;

public class String_Collection {

public static void main(String[] args) {

// List example

List<String> stringList = new ArrayList<>();

stringList.add("apple");

stringList.add("banana");

stringList.add("orange");

System.out.println("List Example:");

for (String fruit : stringList) {

System.out.println(fruit);

}

// Set example

Set<String> stringSet = new HashSet<>();

stringSet.add("apple");

stringSet.add("banana");

stringSet.add("orange");

System.out.println("\nSet Example:");

for (String fruit : stringSet) {

System.out.println(fruit);

}

// Map example

Map<Integer, String> stringMap = new HashMap<>();

stringMap.put(1, "apple");

stringMap.put(2, "banana");

stringMap.put(3, "orange");

System.out.println("\nMap Example:");

for (Map.Entry<Integer, String> entry : stringMap.entrySet()) {

System.out.println(entry.getKey() + ": " + entry.getValue());

}

}

}

In this example, we've used three different Java collections: ArrayList (for a list of strings), HashSet (for a set of strings), and HashMap (for a map with integer keys and string values). The program adds strings to each collection and then demonstrates iterating through and printing the elements

Help your peers!
Select
Add answer anonymously...

IBM Specialist Testing interview questions & answers

A Specialist Testing was asked 3mo agoQ. What are the OOP concepts in Java?
A Specialist Testing was asked 3mo agoQ. How do you identify bugs in your application?
A Specialist Testing was asked Q. What is Angular?

Popular interview questions of Specialist Testing

A Specialist Testing was asked 3mo agoQ1. What are the OOP concepts in Java?
A Specialist Testing was asked 3mo agoQ2. How do you identify bugs in your application?
A Specialist Testing was asked Q3. What is Angular?
IBM Specialist Testing Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits