Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generics Compile Warnings 1

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
0
0
US
I'm using a TreeList to store data. The key is a String and the value is an ArrayList<String>.
The source of the ArrayList is an array because the ArrayList members have to be sorted.

I created the following test code:
Code:
TreeMap<String, ArrayList> dataObjects = new TreeMap<String, ArrayList>();
ArrayList<String> fieldArrayList;

TreeMap<String, ArrayList> dataObjects = new TreeMap<String, ArrayList>();
ArrayList<String> fieldArrayList;

// 1. Create a String array that will be sorted
String [] fieldArray = new String[3];
fieldArray[0] = "omar";
fieldArray[1] = "philippe";
fieldArray[2] = "hua";
// 2. Sort the array
Arrays.sort(fieldArray);
// 3. Turn the array into a java.util.List so the ArrayList constructor will accept it
List fieldList = Arrays.asList(fieldArray);
// 4. Make the ArrayList
fieldArrayList = new ArrayList<String>( fieldList);
// 5. Put the ArrayList into the TreeMap
dataObjects.put("Friends", fieldArrayList);

// 6. Print contents of the TreeMap
for ( Map.Entry t: dataObjects.entrySet() )
{
	System.out.println( t.getKey() );
	// 7. Get the ArrayList from the TreeMap entry
	ArrayList<String> list = (ArrayList<String>) t.getValue();
	for (String name : list )
	{
		System.out.println(list);
	}
}




When I compile my sample:

Step 4 yields the following warning:
Code:
warning: [unchecked] unchecked conversion
found   : java.util.List
required: java.util.Collection<? extends java.lang.String>
		fieldArrayList = new ArrayList<String>( fieldList);
^

Step 7 yields the following warning:
Code:
warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.ArrayList<java.lang.String>
			ArrayList<String> list = (ArrayList<String>) t.getValue();

Can anyone explain why I get these warnings and what I can to do eliminate them?

Thanks.
 
The warnings you are getting means that you compiled your program under Java 5, but are not using Java 5 generics for your Map, List and ArrayList. The compiler is "worried" about the type safety of those Collections. It expected therefore that you specify a type parameter. To eliminate these warnings, you should either add those parameters, like done here:

TreeMap<String, ArrayList<String>> dataObjects = new TreeMap<String, ArrayList<String>>();
ArrayList<String> fieldArrayList;

// 1. Create a String array that will be sorted
String[] fieldArray = new String[3];
fieldArray[0] = "omar";
fieldArray[1] = "philippe";
fieldArray[2] = "hua";
// 2. Sort the array
Arrays.sort(fieldArray);
// 3. Turn the array into a java.util.List so the ArrayList constructor will accept it
List<String> fieldList = Arrays.asList(fieldArray);
// 4. Make the ArrayList
fieldArrayList = new ArrayList<String>(fieldList);
// 5. Put the ArrayList into the TreeMap
dataObjects.put("Friends", fieldArrayList);

// 6. Print contents of the TreeMap
for (Map.Entry<String, ArrayList<String>> t : dataObjects.entrySet()) {
System.out.println(t.getKey());
// 7. Get the ArrayList from the TreeMap entry
ArrayList<String> list = t.getValue(); // no casting required anymore
for (String name : list) {
System.out.println(name);
}
}

or you could suppress the warnings by adding the annnotation @SuppressWarnings("unchecked") before your method declaration
 
Hi tom62,

Thanks. It looks like I was not looking at the Javadoc closely enough. I think whenever I see a <E> type notation on a class or interface, I need to make sure I include it with the correct type in <> brackets in my code.

Thanks very much -- I learned a lot from this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top