0

Considering Java dynamic lists or Maps like LinkedList, LinkedHashMap, TreeMap, etc. I suppose that any object has a reference (next->) to the next object (am I right?). Why there are no ways (e.g., a method) to access these references (the next->) in Java?

My concern is that, if this is possible then a LinkedList can be split into two lists like a charm, saving a lot of CPU overhead. What I do now is that I copy one part of the List (new Sublist()...) and then I clear the corresponding entries in the original List.

aLogic
  • 125
  • 1
  • 8
  • LinkedArrayList? Does that exist? – hvgotcodes Apr 17 '13 at 12:39
  • Thanks, I have just edited, I meant LinkedList. – aLogic Apr 17 '13 at 12:48
  • 1
    Don't forget that the collections shipped with Java are *general purpose collections*. In other words: they are meant to work for *most* use cases and not necessarily for all of them. If destructive splitting of a list is a common (and performance-relevant!) use case for you, then you might consider implementing your own list (or using something like [pcollections](https://code.google.com/p/pcollections/)). – Joachim Sauer Apr 17 '13 at 12:50

3 Answers3

2

My concern is that, if this is possible then a LinkedArrayList can be split into two lists like a charm, saving a lot of CPU overhead. What I do now is that I copy one part of the List (new Sublist()...) and then I clear the corresponding entries in the original List.

If you did that, you would be modifying the internal data structure of your list outside of the API of the list, so the other internal parameters of the list would by out of sync. In other words, the nodes that make up the linked list and hold the objects you put into the list are private to the list; i.e. when you create a LinkedList instance, you don't get access to the nodes.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

Having access to the "next" pointer is an implementation detail. You are right that it would save performance, but from the object oriented perspecitvy, you shouldn't rely on such details. After all, the implementation might be totally different and doesn't even have a next pointer. If you are worried about performance, you should implement your own specific solution, or use a different approach/language.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

Different collection classes have different implementation about how to access the individual objects in the collection. For ex. the Arraylist class stores the individual objects as an array of Object, whereas the LinkedList class uses an inner class Entry to store the individual objects. However, these members are encapsulated and marked as private so that they are not visible to the programmer. The main reason behind this is to provide the programmer with a simple set of methods to perform various operations without caring about how the method is implemented.

My concern is that, if this is possible then a LinkedList can be split into two lists like a charm, saving a lot of CPU overhead. What I do now is that I copy one part of the List (new Sublist()...) and then I clear the corresponding entries in the original List.

Normally, you won't get hold of the members which manage your collection and perform operations like splitting. But, if you still want to access these members to improve the performance, you can always create your own classes, which allow you to get hold of the 'next'. You can always tweak the existing code in the predefined classes like LinkedList and create your own class, say MyLinkedList. Or you can extend these classes and write your own methods.

Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48