• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Why declare a type for java collection classes?

Ive just found out that this works fine:

Set aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);

output: [1, 2, boobs]

Makes sense since these collection classes only store objects right? Whats the point in declaring a type for them? Also can someone explain the following:

Set<Integer> aSet;
aSet = new HashSet();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);

That dosent let me put in a string and dosent compile.

Set aSet;
aSet = new HashSet<Integer>();
aSet.add(1);
aSet.add(2);
aSet.add("boobs");
System.out.println(aSet);

This does let me put in a string and compiles fine. What gives?

Is declaring types for readability reasons and to prevent stuff that's not supposed to be in the set from being put in?
 
Is declaring types for readability reasons and to prevent stuff that's not supposed to be in the set from being put in?

Both of those reasons. We rarely in the real world have a collection of just plain anything, we normally have a Set of Integers or a List of people. Generics is a way of declaring that the collection we have created contains some particular known type so that future code that wants it doesn't need to do an unsafe type cast on the contents.

Its critical to also look at getting stuff out of a collection to start to see the benefits of Generic type information:

Code:
List a= new ArrayList();
a.add(1);
int i = (Integer)a.get(0)

List<Integer> b = new ArrayList<Integer>();
b.add(1);
int i = b.get(0)
 
Java generics are pretty simple (as compared to C++ templates, for example).

The underlying collections always hold type Object. Adding a type parameter does two things:
1) Validates that inserts are of the correct type
2) Casts retrievals to that type.

Before generics you would have to write code like this:
Code:
public class IntegerList {
    // This is a list of Objects
    private List list = new ArrayList();

    public void add(Integer i) {
        list.add(i);
    }

    public Integer get(int index) {
        return (Integer) list.get(index);
    }
}

Generics basically does that for you.
 
Its critical to also look at getting stuff out of a collection to start to see the benefits of Generic type information:

Code:
List a= new ArrayList();
a.add(1);
int i = (Integer)a.get(0)

List<Integer> b = new ArrayList<Integer>();
b.add(1);
int i = b.get(0)

Yes.
And also, it is bullshit, that this generates a warning

Set<Integer> aSet = new HashSet();

I know the 'reasons' why, but common, really? And with java7 we get

Set<Integer> aSet = new HashSet<>();

And now i am supposed to be happyfied?
I'd like to read the concrete example where code will BLOW up as a direct consequence of infering the generic type from the left hand side of the assignment when constructed like this. I am sure there are very very sound academic reasons why it would be considered ugly on the compiler side of things. The thing is, I dont care, programmers want usability too .. its not just for mac-freaks.
 
Yes.
And also, it is bullshit, that this generates a warning

Set<Integer> aSet = new HashSet();

I know the 'reasons' why, but common, really? And with java7 we get

Set<Integer> aSet = new HashSet<>();

And now i am supposed to be happyfied?
I'd like to read the concrete example where code will BLOW up as a direct consequence of infering the generic type from the left hand side of the assignment when constructed like this. I am sure there are very very sound academic reasons why it would be considered ugly on the compiler side of things. The thing is, I dont care, programmers want usability too .. its not just for mac-freaks.

If you don't mind an extra library. Google Guava has some good solutions for stuff like this.

For example:

Code:
List<Integer> myList = Lists.newArrayList();

Set<Integer> mySet = Sets.newHashSet();

http://docs.guava-libraries.googlec...ogle/common/collect/Lists.html#newArrayList()
http://docs.guava-libraries.googlec.../google/common/collect/Sets.html#newHashSet()
 
Last edited:
🙂
Yes, i know (well not of Guava), but i'd do a little util class myself doing just that before including yet another api into a project... But way before that, i am very content with the fact that the powers that be, have *forced* me to actively ignore such warnings with a vengence .. So it's all good, I'm just a loudmouth. 😉
 
Back
Top