WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java Collections · Basic · question 2 of 100

What are the main interfaces in the Java Collections Framework?

📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

The Java Collections Framework provides a set of interfaces and classes that implement various types of collections. Here are the main interfaces in the Java Collections Framework:

Collection: This is the root interface of the Java Collections Framework. It defines the basic operations that all collections should support, such as adding and removing elements, checking if a collection contains an element, and getting the size of the collection.

List: This interface extends the Collection interface and defines an ordered collection of elements. It allows duplicates and provides methods for accessing elements by index, adding and removing elements at specific positions, and more.

Set: This interface extends the Collection interface and defines an unordered collection of elements with no duplicates. It provides methods for checking if an element is in the set, adding and removing elements, and more.

Map: This interface defines a mapping between keys and values. It provides methods for adding and removing key-value pairs, checking if a key or value is in the map, getting the size of the map, and more.

Queue: This interface defines a collection that orders its elements in a specific way for processing. It provides methods for adding and removing elements, checking the size of the queue, and more.

Here’s an example of how to use some of these interfaces in Java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

public class Example {
    public static void main(String[] args) {
        // create a list
        List<String> myList = new ArrayList<>();
        myList.add("apple");
        myList.add("banana");
        myList.add("orange");
        
        // create a set
        Set<Integer> mySet = new HashSet<>();
        mySet.add(5);
        mySet.add(10);
        mySet.add(15);
        
        // create a map
        Map<String, Integer> myMap = new HashMap<>();
        myMap.put("apple", 5);
        myMap.put("banana", 10);
        myMap.put("orange", 15);
        
        // create a queue
        Queue<Integer> myQueue = new LinkedList<>();
        myQueue.add(5);
        myQueue.add(10);
        myQueue.add(15);
        
        // print out the contents of the collections
        System.out.println("List: " + myList);
        System.out.println("Set: " + mySet);
        System.out.println("Map: " + myMap);
        System.out.println("Queue: " + myQueue);
    }
}

In this example, we create a list using the ArrayList class, a set using the HashSet class, a map using the HashMap class, and a queue using the LinkedList class. We then add some elements to each collection and print out their contents using the "println" method.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview — then scores it.
📞 Practice Java Collections — free 15 min
📕 Buy this interview preparation book: 100 Java Collections questions & answers — PDF + EPUB for $5

All 100 Java Collections questions · All topics