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 6795215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:00+00:00 2026-05-26T18:19:00+00:00

I wonder if there’s a List implementation allowing fast contains . I’m working with

  • 0

I wonder if there’s a List implementation allowing fast contains. I’m working with quite a long List and I can’t switch to Set since I need the access by index. I can ignore the performance, which may be acceptable now and may or may not be acceptable in the future. I can create a HashSet and do all modifying operations on both, but doing it manually is quite boring and error prone.

I know that it’s impossible to have a class working like both List and Set (because of the different equals semantics), but I wonder if there’s List implementing RandomAccess and employing an HashSet for speeding up contains.

  • 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-26T18:19:01+00:00Added an answer on May 26, 2026 at 6:19 pm

    you can wrap a list and hashSet that combines best of both worlds

    public class FastContainsList<T> extends AbstractSequentialList<T> implements RandomAccess{
    //extending sequential because it bases itself of the ListIterator(int) and size() implementation
    
        private List<T> list=new ArrayList<T>();
        private Set<T> set=new HashSet<T>();
    
    
        public int size(){
            return list.size();
        }
    
        public boolean contains(Object o){//what it's about
            return set.contains(o);
        }
    
        public ListIterator<T> listIterator(int i){
            return new ConIterator(list.listIterator(i));
        }
    
        /*for iterator()*/
        private ConIterator implements ListIterator<T>{
    
            T obj;
            ListIterator<T> it;
    
            private ConIterator(ListIterator<T> it){
            this.it = it
            }
    
            public T next(){
            return obj=it.next();
            }
    
            public T previous(){
            return obj=it.previous();
            }
    
            public void remove(){
            it.remove();//remove from both
            set.remove(obj);
            }
    
            public void set(T t){
                it.set(t);
                set.remove(obj);
                set.add(obj=t);
            }
    
             public void add(T t){
                 it.add(t);
                 set.add(t);
             }
    
            //hasNext and hasPrevious + indexes still to be forwarded to it
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if there is a way to create a Custom List in Sharepoint,
I wonder if there is a way to set the value of #define in
I wonder if there is a service where I can clean-up html-code so that
I wonder if there exists some logic to reverse a singly-linked list using only
I wonder if there is any optimization I can do to achieve faster SELECTs
I wonder whether there are some guidelines that I can use for coding Winform
I wonder there are any tools or online tools which one can construct tree
I wonder if there's any tool/website where I can plot some run times as
I just wonder there a way to display the files in document directory with
I wonder if there is a way to either programatically or using a third

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.