Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34277

Re: Working with an array of TreeSet

From Marcel Mueller <news.5.maazl@spamgourmet.org>
Newsgroups comp.lang.java.programmer
Subject Re: Working with an array of TreeSet
Date 2016-05-10 18:19 +0200
Organization MB-NET.NET for Open-News-Network e.V.
Message-ID <ngt1mp$a3t$1@gwaiyur.mb-net.net> (permalink)
References <87lh3ijg9u.fsf@Equus.decebal.nl>

Show all headers | View raw


On 10.05.16 13.37, Cecil Westerhof wrote:
> I would like to have an array of TreeSet<Integer>. But that is not
> possible in Java.
> So I use:
>      TreeSet<?>[] allNodes;

Since Java uses ->type erasure the types TreeSet<?>[] and 
TreeSet<Integer>[] are identical.
The strong typing is mainly a hint for the compiler to check whether you 
inserted the correct type and to avoid the downcast required otherwise.

> I fill this with:
>      for (int i = 1; i < maxNode; ++i) {
>          allNodes[i] = new TreeSet<Integer>();
>      }

Well, if you really intend to insert a large number of consecutive 
integers TreeSet<Integer> is amazingly inefficient. Each Integer wraps 
an int with an Object. Each entry in the TreeSet wraps the Integer 
Object with another Object. So you get an overhead of at least one order 
of magnitude.

> And to add an Integer I use:
>      @SuppressWarnings("unchecked")
>      private static void addConnection(int from, int to) {
>          TreeSet<Integer> treeSet;
>
>          treeSet = (TreeSet<Integer>) 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?
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.


Marcel

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Working with an array of TreeSet Cecil Westerhof <Cecil@decebal.nl> - 2016-05-10 13:37 +0200
  Re: Working with an array of TreeSet Eric Douglas <e.d.programmer@gmail.com> - 2016-05-10 04:52 -0700
    Re: Working with an array of TreeSet Cecil Westerhof <Cecil@decebal.nl> - 2016-05-10 15:07 +0200
  Re: Working with an array of TreeSet Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-10 08:03 -0400
  Re: Working with an array of TreeSet Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-05-10 18:19 +0200
    Re: Working with an array of TreeSet Cecil Westerhof <Cecil@decebal.nl> - 2016-05-10 23:22 +0200
      Re: Working with an array of TreeSet Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-05-10 20:14 -0400
        Re: Working with an array of TreeSet Cecil Westerhof <Cecil@decebal.nl> - 2016-05-11 09:05 +0200
          Re: Working with an array of TreeSet FredK <fred.l.kleinschmidt@gmail.com> - 2016-05-11 14:33 -0700
            Re: Working with an array of TreeSet Lew <lewbloch@gmail.com> - 2016-05-11 14:45 -0700

csiph-web