Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:32:12+00:00 2026-05-26T06:32:12+00:00

I have a data structure, for which I am currently using an ArrayList .

  • 0

I have a data structure, for which I am currently using an ArrayList. I realised that in this structure I do not want any duplicates to be present. My first thought was to use some form of set, however the order is also important. After a bit of googling and searching the Collections docs I found LinkedHashSet which almost does the job. Unfortunately, one of the primary reasons for preserving order is because I am using the get(int index) method of the ArrayList for random access, and I can’t see any way around this.

More concisely – I need a set that preserves order and allows random access. None of the classes I have so far looked at provide this functionality. Does anyone know of a class that offers this, or will I have to make it myself? If it is the latter case are there any pitfalls when creating such a structure that people are aware of?

(Alternatively, a quick and easy way of checking for and removing duplicates form an ArrayList or similar structure would suffice)

EDIT: for clarity, it is the order that elements are added to the list that is important, not how they compare to one another

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:32:12+00:00Added an answer on May 26, 2026 at 6:32 am

    I’d just extend ArrayList.

    public class SetList<E> extends ArrayList<E> {
    
        @Override
        public boolean add(E e) {
            return contains(e) ? false : super.add(e);
        }
    
        @Override
        public void add(int index, E e) {
            if (!contains(e)) {
                super.add(index, e);
            }
        }
    
        @Override
        public boolean addAll(Collection<? extends E> c) {
            return addAll(size(), c);
        }
    
        @Override
        public boolean addAll(int index, Collection<? extends E> c) {
            Collection<E> copy = new ArrayList<E>(c);
            copy.removeAll(this);
            return super.addAll(index, copy);
        }
    
    }
    

    Note that the add() method conforms the contract:

    Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have a data structure which uses composite ids (Which I dont wish to
I have a data structure which is as given below: class File { public
Guys, I have a data structure which has 25 distinct keys (integer) and a
I have a speed critical multithreaded program which involves data in a tree structure.
I have a hierarchical data structure which I'm displaying in a webpage as a
I'm currently struggling to understand how I should organize/structure a class which I have
I have a C# data structure which consists of classes and collections which have
In .Net/C# Application, I have data structures which have references to each other. When
I have a restful webservice which receives some structured data which is put straight

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.