0

I have a list in Java, but there is a condition: if the list size exceeds 10, then i have the remove the excess contents the starting without iterating,

Eg: if the list contains {1,2,3,4,5,6,7,8,9,10,11,12,13,14} then the result should be

{5,6,7,8,9,10,11,12,13,14}

Also, please note that the Java version am using is Java 6.

I have tried with subList, but even this operation internally does the iteration which is causing performance issue.

public List<E> subList(int fromIndex,
                       int toIndex)
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
abyin007
  • 361
  • 2
  • 4
  • 14
  • 1
    You cannot do that without iterating the list. You cannot avoid it, not even avoid internal iterations inside methods of `List` class implementation. – Luiggi Mendoza Oct 31 '14 at 06:29
  • Check this link: http://stackoverflow.com/questions/4870188/delete-item-from-array-and-shrink-array – user1274820 Oct 31 '14 at 06:31
  • "I have tried with subList, but even this operation internally does the iteration which is causing performance issue." - What performance issues are you experiencing? Have you done some test and can you provide the results? Also, have you done some test against alternate list? How did you do the test in general? – Buhake Sindi Oct 31 '14 at 06:33
  • @buhake I am dealing with a very large list and my client is not happy with time its taking, so just need to find out whether there is any work around. – abyin007 Oct 31 '14 at 07:03
  • do you intend to achieve this via inbuilt list? or you are allowed to implement your own flavour? – Techfist Oct 31 '14 at 07:06
  • @Techfist using inbuild list, so the only better option would be to use subList(). I guess I need to convince my client. :-) – abyin007 Oct 31 '14 at 07:12
  • hmm in that case thats the only way, but I dont understand usage of list for this purpose as u say size is fixed,without iteration this can be arranged by other means such as through arrays. – Techfist Oct 31 '14 at 07:15
  • @abyin007 I understand that but don't say that it has performance issues if there was no test case and benchmark done. – Buhake Sindi Oct 31 '14 at 07:55
  • "I have tried with subList, but even this operation internally does the iteration which is causing performance issue" - you can look at the implementation of `subList` - there's no iteration involved - all it does is calculate offsets into the original list. That's as fast as you're going to get. – Krease Oct 31 '14 at 16:39

2 Answers2

4

It is not clear to me why you would want this, perhaps you could explain what problem you're trying to solve by avoiding iteration? There's likely a better answer to that problem. If you post a SSCCE, we can run it and replicate the issue you're having directly.

You claim that ArrayList.subList() does iterations, which is simply not true. As the documentation states:

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.... The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa.

This function returns a wrapper around the existing list in O(1) time. If you are seeing performance problems, it is almost certainly not due to ArrayList.subList().

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • And even if it were, iterations are fast. There is some other mistake (an O(n^2) algorithm, perhaps?) in OP's code. – dimo414 Oct 31 '14 at 15:50
1

Simply you cannot do that. It is not possible without iteration. Getting top n element won't cause that much performance difference.

subList() method which is inbuilt method enough optimized and don't worry about that.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307