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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:07:37+00:00 2026-05-17T21:07:37+00:00

In Java I want to store elements in ascending order by size. Before insertion

  • 0

In Java I want to store elements in ascending order by size. Before insertion I need to treat all the smaller elements. So I start enumerating the list treating smaller elements, and when I meet a larger element, I want to insert the new node before this larger element. What data structure would be the good choice? ArrayList may not be a good choice as it shifts elements to the right when inserting in the middle. I looked at the API of LinkedList, but I do not find methods for inserting before/after a given node in the list. There is an add method that “inserts the specified element at the specified position in this list”, but it requires the position as an integer. So does it mean that the Java implementation traverses the list from the beginning until it finds the specified position? It seems quite expensive. I have the position (a node) where I want to insert, just some references should be changed correctly to include the new node in the list. Any suggestions?

Thanks,

Jabba

Update: Thanks everyone for the answers, I could solve the problem with LinkedList using ListIterator. Then I made a comparative test and I got some surprising results. So, the goal is to maintain elements in a list/array in a sorted way. For this I compared four alternatives: (1) Vector, (2) ArrayList, (3) LinkedList, and (4) MyLinkedList, this is my own implementation (very simple). For the test I created an array with 1000 elements and I filled it with random integers between 1 and 100. Then I added these elements in each container in a loop. When this array was added 30 times: MyLinkedList (2.7 sec), LinkedList (6.6 sec), ArrayList (6.9 sec), Vector (7.5 sec). Adding the array 100 times: MyLinkedList (80.7 sec), ArrayList (82.5 sec), Vector (92.7 sec), LinkedList (176.3 sec). Adding the array 200 times: ArrayList (304.3 sec), Vector (332.7 sec), MyLinkedList (381.2 sec), LinkedList (610.4 sec).
Conclusion: ArrayList is always faster than Vector. Since it’s not synchronised, it’s not very surprising. Though the difference is not too significant. If the list is not too large (up to 100,000 elements in my case), I got the best performance with an own implementation. Surprisingly LinkedList performs quite poor. As the size of the list grows, LinkedList is worse and worse. Final conclusion: if the list is not too big, you can have the best performance with an own implementation. If the size of the list is very big, use ArrayList.
This result surprises me because ArrayList has to move lots of elements while a linked list only has to change some references. But as ArrayList is based upon an array, despite the high number of shifts it can outperform a linked list.

Extra info: In all four cases, when inserting a new element, I enumerate the list from the beginning until I find a larger element, then I insert the new node before it. I do this because the application needs to process the smaller elements. That’s why I don’t do binary search on Vector and ArrayList.

  • 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-17T21:07:37+00:00Added an answer on May 17, 2026 at 9:07 pm

    I looked at the API of LinkedList, but
    I do not find methods for inserting
    before/after a given node in the list.

    It exists, but it is hidden in the ListIterator interface returned by the LinkedList.listIterator() method.

    This is done to avoid exposing the list nodes themselves.

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

Sidebar

Related Questions

No related questions found

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.