Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

linear search in list interface costly?

Thread view: 
cyprian - 28 May 2007 15:41 GMT
if the linear search in List interface are costly, according to the
api, are there alternative searches provided for in the api for List?
Tom Hawtin - 28 May 2007 16:23 GMT
> if the linear search in List interface are costly, according to the
> api, are there alternative searches provided for in the api for List?

Linear searches are O(n). So for very large length lists it is slow. For
short lists it doesn't really matter.

( http://en.wikipedia.org/wiki/Big_O_notation )

If you have a Comparator and sort the list, you can use
Collections.binarySearch. The will give O(log n) for a RandomAccess List
(such as ArrayList). For a LinkedList, I guess it will be O(n).

Alternatively you could have a separate Map, that maps onto indexes of
the List (possibly a List<Integer>, int[] if you the contents of the
List are not unique). That obviously requires keeping two data
structures (are you sure you need the List?), but runs at O(1).

If the List does not contain duplicates, then you might be able to use
LinkedHashSet. Again that will be O(1).

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.