TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

provide a fill method similar to Collections.fill() that uses a Supplier #38

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
 Collections.fill() allows a List to be populated with a single instance, but there are times where 
you'd want to fill with separate instances.  My usecase was pre-initializing a 
matrix 
(ArrayList<ArrayList<T>>).

something like <T> void fill(List<? super T> list, Supplier<? extends
T> objSupplier) would be great.

Original issue reported on code.google.com by gk5...@gmail.com on 14 Nov 2007 at 4:51

GoogleCodeExporter commented 9 years ago
It's not clear that this is any better than filling the list the old-fashioned 
way. 
Please show before and after examples, thanks.

Original comment by kevin...@gmail.com on 28 Nov 2007 at 11:52

GoogleCodeExporter commented 9 years ago
The problem with the traditional fill() method is that each row of the matrix 
will point to the same column, while 
you actually want separate columns.

Thus by using the following:
ArrayList<ArrayList<T>> matrix = Collections.fill(Lists.newArrayList(5), 
                                                                                          new Supplier<ArrayList<T>> { 
                                                                                              public ArrayList<T> get() {
                                                                                                  return Lists.newArrayList(5);
                                                                                         }});

You are actually able to instantiate the expected matrix.

Original comment by romain.r...@gmail.com on 29 Nov 2007 at 12:14

GoogleCodeExporter commented 9 years ago
Ok, I asked for a before and after and you've provided the "after".  I'll go 
ahead
and provide the "before":

ArrayList<ArrayList<T>> matrix = Lists.newArrayList(5);
for (int i = 0; i < 5; i++) {
  matrix.add(Lists.newArrayList(5));
}

Now perhaps you could explain what is better about the 'after' than the 
"before",
because I just don't see it.

Original comment by kevin...@gmail.com on 29 Nov 2007 at 12:46

GoogleCodeExporter commented 9 years ago
Let me make some corrections to both examples. Lists.newArrayList(5) does not 
do what
you think it does (you expect it to create a list of size 5, but it creates a 
list of
size one containing the *element* 5).

BEFORE:

List<List<T>> matrix = new ArrayList<List<T>>(5);
for (int i = 0; i < 5; i++) {
  matrix.add(new ArrayList<T>(5)); // add elements later
}

AFTER:

@SuppressWarnings("unchecked")
List<List<T>> matrix = Arrays.asList((List<T>[]) new List[5]);
Lists.fill(matrix, new Supplier<List<T>>() {
      @Override public List<T> get() {
        return new ArrayList<T>(5); // add elements later
      }
    });

Have I got that right?

Original comment by kevin...@gmail.com on 29 Nov 2007 at 1:01

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
You are right, and for the time being, I do not especially see the benefit of 
it and it seems to make things much 
more 
complicated than they are... but I am not the one who submitted the request :)

Maybe having a method like Lists.newArrayListWithExpectedSize(int size, 
Supplier default) would be more 
relevant for providing 
on-demand default values for a fixed array. This might be interesting for 
saving memory in case of large matrix 
storing a small set of values.

Original comment by romain.r...@gmail.com on 29 Nov 2007 at 1:10

GoogleCodeExporter commented 9 years ago
heh!  Sorry, I just assumed it was the original submitted replying again.  
Anyway,
I'm closing this.

Original comment by kevin...@gmail.com on 29 Nov 2007 at 1:12

GoogleCodeExporter commented 9 years ago
after neglecting to follow my own issue for a long time and now seeing your 
discussion, i entirely agree.  the 
only way that this would have saved any typing would be if you could reuse the 
supplier, which you probably 
couldn't.  maybe someday with closures when it takes fewer than 57 lines to get 
the effect of the supplier...

Original comment by gk5...@gmail.com on 4 Dec 2007 at 4:30