Logo

dev-resources.site

for different kinds of informations.

In Java how to create a custom ArrayList that doesn't allow duplicate? #Interview Question

Published at
6/17/2024
Categories
streams
java
Author
codegreen
Categories
2 categories in total
streams
open
java
open
Author
9 person written this
codegreen
open
In Java how to create a custom ArrayList that doesn't allow duplicate? #Interview Question

Creating a Custom ArrayList in Java that Doesn't Allow Duplicates

=================================================================

In Java, you can create a custom ArrayList that ensures no duplicate elements are added by extending the ArrayList class and overriding the add method.

import java.util.ArrayList;

public class CustomArrayList<E> extends ArrayList<E> {

    // Override add method to prevent duplicates
    @Override
    public boolean add(E e) {
        if (!contains(e)) {
            return super.add(e);
        }
        return false;
    }

    public static void main(String[] args) {
        CustomArrayList<Integer> list = new CustomArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(2); // This duplicate will not be added

        System.out.println(list); // Output: [1, 2, 3]
    }
}
Enter fullscreen mode Exit fullscreen mode

This custom ArrayList implementation checks if an element already exists before adding it. If the element is not found, it adds the element using the superclass's add method. Otherwise, it returns false, indicating that the element was not added due to duplication.

streams Article's
30 articles in total
Favicon
How does Optional.ifPresent() differ from Optional.orElse()?
Favicon
Beyond the Basics: Mastering Streams in Node.JS
Favicon
The streaming bridges — A Kafka, RabbitMQ, MQTT and CoAP example
Favicon
Java Streams | What is the difference between sorted() and distinct() in streams?
Favicon
How to handle large file uploads in SvelteKit using streams
Favicon
Execução preguiçosa com Lambdas
Favicon
Руководство по Java 8 Stream API
Favicon
In Java how to create a custom ArrayList that doesn't allow duplicate? #Interview Question
Favicon
Why Set Doesn't Allow Duplicates in Java
Favicon
Optional Class in Java and its methods
Favicon
A Few About: Streams NodeJs (PT-BR)
Favicon
File reading in python
Favicon
Asynchronous Streams in C#: Effortless Async Data Processing
Favicon
Working with streams in Node.js
Favicon
Migrating to Phoenix Liveview Streams
Favicon
What is Java Stream and why does it exist?
Favicon
-
Favicon
Streams em Node.js para iniciantes (guia básico)
Favicon
From a data stream to the data warehouse
Favicon
Gentle Intro to Node Streams
Favicon
The Java `.boxed()` Method
Favicon
Request and Response Stream - Observations
Favicon
Getting a near-real-time view of a DynamoDB stream with Python
Favicon
Exploring Java Streams
Favicon
Service Worker Side Rendering (SWSR)
Favicon
Listening to payments (real-time) on your Stellar wallet
Favicon
Supporting Cross Node Interactive Queries In Kafka Streams
Favicon
Summing Java Streams Api
Favicon
Handling Slow Servers in NodeJS
Favicon
Java 8 Non ce n’est pas que les streams

Featured ones: