Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!usenet.ukfsn.org!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: Inserting In a List Date: Wed, 3 Apr 2013 22:43:51 +0000 (UTC) Organization: UK Free Software Network Lines: 45 Message-ID: References: <1vzadyb0cgq7k.rsclf5bucb9k$.dlg@40tude.net> NNTP-Posting-Host: 84.45.235.129 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: localhost.localdomain 1365029031 9971 84.45.235.129 (3 Apr 2013 22:43:51 GMT) X-Complaints-To: usenet@localhost.localdomain NNTP-Posting-Date: Wed, 3 Apr 2013 22:43:51 +0000 (UTC) User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Xref: csiph.com comp.lang.java.programmer:23234 On Wed, 03 Apr 2013 02:04:19 +0200, Joerg Meier wrote: > On Tue, 2 Apr 2013 22:52:20 +0000 (UTC), Martin Gregorie wrote: > >> On Tue, 02 Apr 2013 18:22:55 -0400, Eric Sosman wrote: >>> On 4/2/2013 5:06 PM, Martin Gregorie wrote: >>>>[...] >>>> Its also not clear to me whether the OP is expecting some form of >>>> sorted list of filenames. If he is expecting that, it would be best >>>> to use a TreeMap rather than an ArrayList to store >>>> the filenames. >>> Is there a reason to prefer TreeMap (or other SortedMap) over >>> accumulate-disordered-and-sort-afterward? >> I think so, yes, because none of File's list() and listFiles() methods >> guarantee the order of the returned files. By using TreeMap or >> equivalent the OP gets the sort for free, should it be required. > > For free ? > I meant in terms of coding effort. > The cost is just distributed amongst the insert calls, and is > likely considerably higher than with an unsorted list that has a single > sort call once it is filled. > Not necessarily so (see above) and that's why I specified a TreeMap rather than any other type of ordered map because of the relative drop in access costs as the collection size increases. > SortedMaps are for things that need sorting > accessible before all elements are inserted > And isn't the case with the TreeMap either - red-black binary trees have rather nice characteristics there. > Java doesn't have ArrayMaps. > Yeah, I meant ArrayList and should have been obvious from the context provided by the OP. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |