Always get warning when using raw type Lists/Sets/Iterators etc.

public java.util.List /* <-- Warning here */ getUsersByAge(int minAage) 
{ 
     org.hibernate.Query q = session.createQuery("from User u where u.age >= :minAge"); 
     q.setInteger("minAge", minAage); 
     return q.list(); /* <-- Warning here */
}

I've chaged this by

public java.util.List<User> getUsersByAge(int minAage) 
{ 
    org.hibernate.Query q = session.createQuery("from User u where u.age >= :minAge");     
    q.setInteger("minAge", minAage); 
    return q.list(); /* <-- How do i get rid of the warning here ???? */ }

and

private java.util.Set phoneNumbers = new java.util.HashSet(); 
/* Warning: Set is a raw type. References to generic type Set<E> should be parameterized */

Anywayz, is there any danger if i ignore the warnings?

Yes, the fact that you can store items that aren't what other programmers may expect when handling the data. That and most likely issues in memory, though I can't confirm this.

The only reason raw types are allowed is for compatibility between legacy versions of Java (before Erasure types) and of course the current standard.

import java.util.*;

public class BigOopsDaisyClass{

	private static ArrayList<String> supposedlyStrings;
	private static ArrayList supposedlyObjects = new ArrayList<Integer>(0);

	public static void main(String... args){

		ArrayList badList = new ArrayList(0);
		badList.add(new Double(2.2));
		badList.add(new Byte((byte)200));

		supposedlyStrings = badList; // Very bad!

		supposedlyStrings.add(new String("HORRIBLE"));

		supposedlyObjects.add(new Short((short)30000)); // not even an Integer, but legal?
		supposedlyObjects.add(new Long((long)1919191)); // again!?!?

		System.out.println( "Result of supposedlyStrings: " + supposedlyStrings);
		System.out.println("\n\n\n\n\n\n\n");
		System.out.println("Result of supposedlyObjects: " +supposedlyObjects);
	}
}

Always get warning when using raw type Lists/Sets/Iterators etc.

public java.util.List /* <-- Warning here */ getUsersByAge(int minAage) 
{ 
     org.hibernate.Query q = session.createQuery("from User u where u.age >= :minAge"); 
     q.setInteger("minAge", minAage); 
     return q.list(); /* <-- Warning here */
}

I've chaged this by

public java.util.List<User> getUsersByAge(int minAage) 
{ 
    org.hibernate.Query q = session.createQuery("from User u where u.age >= :minAge");     
    q.setInteger("minAge", minAage); 
    return q.list(); /* <-- How do i get rid of the warning here ???? */ }

and

private java.util.Set phoneNumbers = new java.util.HashSet(); 
/* Warning: Set is a raw type. References to generic type Set<E> should be parameterized */

Anywayz, is there any danger if i ignore the warnings?

In the first case you should annotate the method with @SuppressWarnings("unchecked"). Additionally, in Eclipse you can set the compiler to ignore this situation but I won't recommend that. As Alex said - raw types exist to be compatible wtih old code. So when you get in touch with old code, code that you can't change add annotation (mark that you use unchecked conversion), otherwise use parameterized types.

In the first case you should annotate the method with @SuppressWarnings("unchecked"). Additionally, in Eclipse you can set the compiler to ignore this situation but I won't recommend that. As Alex said - raw types exist to be compatible wtih old code. So when you get in touch with old code, code that you can't change add annotation (mark that you use unchecked conversion), otherwise use parameterized types.

My question was more on "how" than "what" ... Hence 've given code here so that one can show me how to fix those warnings if possible in here ..

Thanx anyway...

My question was more on "how" than "what" ... Hence 've given code here so that one can show me how to fix those warnings if possible in here ..

Thanx anyway...

Very simple, use parameterized types like you did on the first example. If you ignore the warnings, in runtime you can recieve ClassCastException when you will cast the object that you retrieved from the Collection. Actually that is one of the the reasons why generics were introduced - to avoid type unsafety.

This article has been dead for over six months. Start a new discussion instead.