Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.java.programmer Subject: Re: Working with an array of TreeSet Date: Tue, 10 May 2016 23:22:09 +0200 Organization: Decebal Computing Lines: 48 Message-ID: <87a8jxk3r2.fsf@Equus.decebal.nl> References: <87lh3ijg9u.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="12baf5125b19322b899c79c2e3878015"; logging-data="27664"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18vq72a9+G7qJb4QqYG5j/F/SrkGJ2rdbg=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:aSxDP7ZbNHCl7Nimj83uHBDfXLI= sha1:gqulnISUJsP8sxuRMGrH5gmukBQ= Xref: csiph.com comp.lang.java.programmer:34280 On Tuesday 10 May 2016 18:19 CEST, Marcel Mueller wrote: > On 10.05.16 13.37, Cecil Westerhof wrote: >> I would like to have an array of TreeSet. But that is not >> possible in Java. >> So I use: >> TreeSet[] allNodes; > > Since Java uses ->type erasure the types TreeSet[] and > TreeSet[] are identical. The strong typing is mainly a hint I got a compiler error on TreeSet[]. But ArrayList saved my day. >> And to add an Integer I use: >> @SuppressWarnings("unchecked") >> private static void addConnection(int from, int to) { >> TreeSet treeSet; >> >> treeSet = (TreeSet) allNodes[from]; >> treeSet.add(to); >> } >> >> Is this a correct way of doing this, or is there a better way? > > This is syntactically OK. > > The more interesting question is why you need an array of TreeSet. > What is the semantic meaning of from? And what is the meaning of to? I am mapping the connections of paths between nodes in a graph. From a node to a node. > It looks like an 1:n or m:n mapping. If the number of entries in one > TreeSet is small a sorted list would perform better. Unfortunately > such a type is missing from the Java class library. So you need to > deal with an array or ArrayList and maintain the sort order by your > own. I prefer an algorithm that is clear. Only when there is a real problem with performance I would sacrifice clearness of code. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof