mkodekar / guava-libraries

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

Multimaps.newListMultiMap(map, factory) does not work when factory supplies Doubles.asList(double[]) #1827

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If a factory supplies instances of Doubles.asList(double[]) to 
Multimaps.newListMultiMap(map, factory), and an EnumMap is used (I doubt this 
is relevant) calls to ListMultimap.get() return a  List<Double>, but the list 
is never stored as it would be if the factory returned an ArrayList<Double>

For example:

    public static void main(String[] args) {

        Map<CaseFormat, Collection<Double>> map = Maps.newEnumMap(CaseFormat.class);
        final int size = 3;

        Supplier<List<Double>> factory = new Supplier<List<Double>>() {
            @Override public List<Double> get() {
                return Doubles.asList(new double[size]);
            }
        };

        ListMultimap<CaseFormat, Double> multimap = Multimaps.newListMultimap(map, factory);
        List<Double> list = multimap.get(CaseFormat.LOWER_UNDERSCORE);
        System.out.println(list);
        list.set(1, 5.0);
        System.out.println(list);
        System.out.println(multimap.get(CaseFormat.LOWER_UNDERSCORE));
        System.out.println(multimap);
    }

produces:

[0.0, 0.0, 0.0]
[0.0, 5.0, 0.0]
[0.0, 0.0, 0.0]
{}

Original issue reported on code.google.com by geowe...@gmail.com on 5 Aug 2014 at 3:36

GoogleCodeExporter commented 9 years ago
The Supplier passed to that method must return *empty* lists, and 
Doubles.asList returns lists that do not change in size.  There's really no way 
for Doubles.asList to work properly here.

Original comment by lowas...@google.com on 5 Aug 2014 at 3:39

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07