Introduction to Java Collections and Strings

Introduction

Welcome to this course!

Before we delve deeper into Java essentials for interview prep, let's start with some foundational Java features — specifically, ArrayLists and Strings. These features allow Java to group multiple elements, such as numbers or characters, under a single entity.

Understanding ArrayLists and Strings

As our starting point, it's crucial to understand how ArrayLists and Strings function in Java. ArrayList is part of Java’s Collections Framework and is mutable (we can change it after creation), while Strings are immutable (unalterable post-creation). Let's see examples:

import java.util.Arrays;
import java.util.ArrayList;

class Solution {
    public static void main(String[] args) {
        // Defining an ArrayList and a String
        // <Integer> specifies the type of Objects the ArrayList will hold
        ArrayList<Integer> myList = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
        String myString = "hello";

        // Now let's try to change the first element of both these features
        // set method changes the element at the specified index (0 in this case)
        myList.set(0, 100);
        // There is no method set for Strings
        // The following method does not change the string in place,
        // but returns a new string where 'h' is replaced with 'H'
        myString.replace('h', 'H');
        // So it is possible to obtain a new string like this:
        String newString = myString.replace('h', 'H');
        
        System.out.println(myList); // prints [100, 2, 3, 4]
        System.out.println(myString); // prints hello
        System.out.println(newString); // prints Hello
    }
}

Diving Into Lists

Imagine having to take an inventory of all flora in a forest without a list at your disposal — seems near impossible, right? That's precisely the purpose ArrayList serves in Java. They let us organize data so that each item holds a definite position or an index. The index allows us to access or modify each item individually.

Working with Lists in Java is as simple as this:

import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<String> fruits = new ArrayList<>(Arrays.asList("apple", "banana", "cherry"));

        // Add a new element at the end
        fruits.add("date"); // ['apple', 'banana', 'cherry', 'date']

        // Inserting an element at a specific position
        fruits.add(1, "bilberry"); // ['apple', 'bilberry', 'banana', 'cherry', 'date']

        // Removing a particular element
        fruits.remove("banana"); // ['apple', 'bilberry', 'cherry', 'date']

        // Accessing elements using indexing
        String firstFruit = fruits.get(0); // apple
        String lastFruit = fruits.get(fruits.size() - 1); // date

        // Converting static array to ArrayList and vice versa
        String[] fruitArray = {"kiwi", "lemon", "mango"};
        ArrayList<String> fruitList = new ArrayList<>(Arrays.asList(fruitArray));
        String[] newFruitArray = fruitList.toArray(new String[0]);
    }
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal