3 ms·
Specifically about ArrayList<Integer> -- there are specifically a bunch of projects (Goldman Sachs Collections, trove, FastUtil, Koloboke) that solve the array
by astral303 11y ago
Specifically about ArrayList<Integer> -- there are specifically a bunch of projects (Goldman Sachs Collections, trove, FastUtil, Koloboke) that solve the array of primitives problem.
I would say that's a Java-standard-libary-specific problem.
- TheLoneWolfling 11y agoAnd then you have to do a copy just to pass it around to any other external libraries. And you lose the advantages of generics. You end up with 9 copies of everything, that are 99% the same except for a couple find-replaces. If not more. (For instance, if you have a method that takes two generic arrays, you need 81 copies! Even if they are the same type you still need 36 (!) copies.) Not a good solution.
- astral303 11y agoHow do you end up with 9 copies of everything? I'm not sure I follow that. If you are passing in massive ArrayList<Integer> lists to external libraries, you need to re-evaluate what you're doing. What will that external library do with this?
- TheLoneWolfling 11y agoLook at java.util.Arrays for a concrete example of the problem: static int binarySearch(byte[] a, byte key) static int binarySearch(char[] a, char key) static int binarySearch(double[] a, double key) static int binarySearch(float[] a, float key) static int binarySearch(int[] a, int key) static int binarySearch(long[] a, long key) static int binarySearch(Object[] a, Object key) static int binarySearch(short[] a, short key) This, sort of works. It's a lot of code duplication, but it's all in the library. Except that a relatively common thread goes like this: You start by having a generic array that gets passed to said binary search. It works, but it's too memory-hungry when it gets called with a primitive. So, then what do you do? Well, you go "huh, I could specialize". And then you start specializing, and realize that every function that calls binarySearch with a generic type also needs 8 implementations (would be 9, but no sane person would binary search a boolean[] ). Basically, its complexity that you cannot even punt off to an external library. If you have code that needs to be able to be called with generics without inefficiencies for primitive types, you'll end up with ~9x code duplication at a minimum.