0

I came across an interesting piece of code while learning morphadorner manipulations. The code is below:

Collection<Object>[] nodes = someFunction()  

My question is in what scenario is this declaration necessary and/or valid:

Collection<Object>[] nodes

I have seen:

Collection<Object[]> nodes

But cannot think of a scenario where I would need an Array of Collections. So again the question is, when would this be used?

This is the javadoc:

java.util.Set<java.lang.String>[] 

findNames(java.lang.String text)
      Returns names from text.
Woot4Moo
  • 22,887
  • 13
  • 86
  • 143

3 Answers3

3

First of all,

Collection<Object[]> nodes;

and

Collection<Object>[] nodes;

are two different things. The first is a collection of arrays, whereas the second is an array of collections.

As to when you'd use the latter, my answer would be "rarely". While conceptually this is pretty simple, Java arrays and generics don't play together nicely.

It is therefore more common to see

ArrayList<Collection<Object>> nodes;

which is similar but much easier to deal with.

As to whether findNames() is an example of good design, my main objection is that it's completely impossible to guess from the function signature what the elements of the array are supposed to represent (or how many there are). For this reason, I would have done it differently, probably returning a custom class with two clearly-named accessors.

Community
  • 1
  • 1
NPE
  • 438,426
  • 93
  • 887
  • 970
  • yes I fully understand those are different things. Notice how I said i have seen the other syntax :) – Woot4Moo Mar 26 '13 at 15:50
1

The code compiles, so it's probably valid Java.

It's necessary when the designer couldn't think of a better solution.

Which leaves us with: Is it good design?

Maybe but probably not. One scenario would be if the function always returns two or three collections (i.e. more than one but the number never changes).

You could create an object for this but since this is Java, this would take many, many, many lines of deadly boring code. It would also mean that you would have to come up with some useful names for each collection.

Taking the JavaDoc into account that you posted, it seems the number of arrays depends on the number of sentences in the text.

So in this scenario, I would return a List of Collections (since the order of sentences never changes and you might want to get them by index).

The designer might argue that you can add elements to a list but not to an array but I'd use an unmodifiable list.

So in this example, I'd say it's bad design.

Aaron Digulla
  • 297,790
  • 101
  • 558
  • 777
  • I am agreeing with the bad design at this point. I thought I was missing some esoteric feature of Java. – Woot4Moo Mar 26 '13 at 15:51
0
Collection<Object>[] nodes

is Valid because it means that we are getting an array of Collection<Object> . Whereas,

Collection<Object[]> nodes

means we are getting a Collection that contains arrays of Objects.

My question is in what scenario is this declaration necessary and/or valid:

Consider the situation when you have different set of objects say (people). Each set belong to the people of particular country. And we create an array of set of specified size. In that case we use the following syntax:

Set<People>[] setOfPeople = new TreeSet<People>[5]; // We want to consider people of 5 different countries.
Vishal K
  • 12,676
  • 1
  • 22
  • 37