Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

indexOfSubList in Collections

 
sam White
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I found the logic of indexOfSubList() in Collections is not so correct, as shown below:

1. if source list is not empty and target list is empty, the function returns 0.
2. if source list and target list is empty , the function returns 0.

if 0 means the first element, the function should returns -1 as per the above two points.

Here is my own implementation to address the above issues:



any suggestion is highly appreciated.

Thanks
S
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An empty list can be found between any two elements of a list, as well as before the first and after the last. This is similar to how there's an empty String between any two characters of any String, as well as before the first and after the last.

Following the above logic, indexOfSubList should return 0 (before the first element) when looking for an empty list. -1 means the empty list cannot be found, and that's certainly not the case.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49413
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The empty list is a sublist of every list, the empty set is a subset of every set, and the empty String is a substring of every String. If there are positions, those sub-things can be found at any positions.

Something we forget so often, and get confused about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic