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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:54:01+00:00 2026-05-27T05:54:01+00:00

There are two sub-problems. 1- Comparing two huge arraylists 2- Sorting elements of arraylist

  • 0

There are two sub-problems.

1- Comparing two huge arraylists

2- Sorting elements of arraylist based on the values of its object to achieve (1).

I have an ArrayList of objects of a class. i.e.

Class X
{
    double x;
    double y;
    int sortVal;
}

ArrayList<X> alX = new ArrayList<X>(); //size = 10,000
ArrayList<Integer> myValue = new ArrayList<Integer>(); //size = 15

I want to check if myValue is present in sortVal.

X ob = new X();
for(i=0;i<myValue.size();i++)
{
   for(j=0;j<alX.size();j++)
   {  
      ob = alX.get(j)
      **if (myValues.get(i) == ob.sortVal)**
   }
}

As the size of the arraylist ‘alX’ is huge, it takes high computation time.

I thought the better way would be to sort the elemets of ArrayList alX based on the values of sortVal of Class X. By sorting, once sortVal is greater than myValue, I can break from the loop.

1) how can I sort the elements of arraylist alX based on the value ‘sortVal’.

2) Is there a better approach, than sorting the arraylist, to compare the two values. i.e. (myValues.get(i) == alX.ob.sortVal)

[edit] Consider the values being,

ArrayList<X>:
x      : 1,1,1,2,3,5,4,5
y      : 2,4,6,4,4,6,2,1
sortVal: 10,20,30,10,10,20,30

ArrayList<Integer>:
myValue: 10,20,30
  • 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-27T05:54:02+00:00Added an answer on May 27, 2026 at 5:54 am

    You could construct a Map<Integer, X> if sortVal values are unique or Map<Integer, Lists<X>> if they are not. This has a time complexity of O(n) instead or O(n * log n) which is the cost of doing a sort.


    EDIT: This builds a MultiMap of keys and the set of objects for that key in one pass.

    List<X> xs = ...
    Map<Integer, Set<X>> mapBySortVal = new LinkedHashMap<>();
    for(X x: xs) {
       Set<X> set = mapBySortVal.get(x.sortVal);
       if (set == null)
          mapBySortVal.put(x.sortVal, set = new LinkedHashSet<>());
       set.add(x);
    }
    
    for(Integer value: myValues) {
       Set<X> xs = mapBySortVal.get(value);
       if (xs != null) 
           // found some.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two popular naming conventions: vc90/win64/debug/foo.dll foo-vc90-win64-debug.dll Please discuss the problems/benefits associated with
There are two different ways to create an empty object in JavaScript: var objectA
I have these two modules : package G1; sub new { my $class =
There are two weird operators in C#: the true operator the false operator If
There are two popular closure styles in javascript. The first I call anonymous constructor
There are two scenarios I need to clarify: An executable compiled with .NET 3.5
There are two databases in SQL Server 2005: One called A and another one
There are two schools of thought on how to best extend, enhance, and reuse
There is two available Eclipse plugins for Maven : Eclipse IAM (old name is
There are two pictureboxes with two different images. If I click on one picture

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.