|
-
May 17th, 2005, 10:50 PM
#1
Thread Starter
Dazed Member
Generic methods or Wildcard types?{Resolved}
I've been recently converting some legacy code to use Generics and ive been running into problems. Below is an example of one wall ive run into. A ClassCastException is throw where new Employee Ojects are added to the Set. I understand why but i can't figure out a work around using Wildcards. Does anyone have any suggestions? Perhaps i should use Generics instead? Thanks.
Code:
import java.util.*;
class Person{
public String fname;
private String lname;
public Person(String fname, String lname){
this.fname = fname;
this.lname = lname;
}
}
class Employee extends Person{
private String dept;
private String empid;
public Employee(String dept, String empid, String fname, String lname){
super(fname,lname);
this.dept = dept;
this.empid = empid;
}
}
class Aux{
public static void listEmployee(Collection<? extends Person> person){
for(Person p: person){
System.out.print(p.fname);
}
}
}
public class Directory{
public static void main(String[] args){
Set<Employee> directory = new TreeSet<Employee>();
directory.add(new Employee("Shipping","9878","Bill","Gates"));
directory.add(new Employee("Mailroom","6546","John","Holmes"));
Aux.listEmployee(directory);
}
}
Last edited by Dilenger4; May 22nd, 2005 at 09:28 AM.
Reason: Resolved
-
May 21st, 2005, 08:30 AM
#2
Re: Generic methods or Wildcard types?
You mean this line:
directory.add(new Employee("Shipping","9878","Bill","Gates"));
throws a ClassCastException? It shouldn't.
Or am I misunderstanding your problem?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 21st, 2005, 03:28 PM
#3
Thread Starter
Dazed Member
Re: Generic methods or Wildcard types?
Yeah i thought i understood why but i was thinking about somthing else. I just doesn't make any sense to me. If the Collection is marked to hold a specific type then is should be legal to insert that type into the Collection. I simplified the code by taking out the super class.
Code:
import java.util.*;
class Employee{
public String dept;
public String empid;
public String fname;
public String lname;
public Employee(String dept, String empid, String fname, String lname){
this.dept = dept;
this.empid = empid;
this.fname = fname;
this.lname = lname;
}
}
class Aux{
public static void listEmployee(Collection<Employee> e){
for(Employee employee: e){
System.out.print(employee.fname);
}
}
}
public class Directory{
public static void main(String[] args){
Set<Employee> directory = new TreeSet<Employee>();
directory.add(new Employee("Shipping","9878","Bill","Gates"));
directory.add(new Employee("Mailroom","6546","John","Holmes"));
Aux.listEmployee(directory);
}
}
-
May 21st, 2005, 03:30 PM
#4
Thread Starter
Dazed Member
Re: Generic methods or Wildcard types?
If native types are used then everything is fine. I think somthing needs to be done to a user defined type when using that type as a type parameter to a Collection.
Code:
import java.util.*;
public class Directory{
public static void main(String[] args){
Set<String> s = new TreeSet<String>();
s.add(new String("Hello"));
s.add(new String("Goodbye"));
printElements(s);
addElements(s);
}
public static void printElements(Collection<String> s){
for(Object o :s){
System.out.println(o);
}
}
public static void addElements(Collection<String> s){
s.add(new String("Welcome"));
}
}
-
May 21st, 2005, 06:03 PM
#5
Re: Generic methods or Wildcard types?
I took the original code and went into the debugger. I believe the problem is that you insert the object into a TreeSet, which requires an ordering imposed on its element. Since you don't supply a custom comparator, it attempts to cast the Employees to Comparable, which throws the exception.
Edit: I seem to be right. After replacing the TreeSet with a HashSet, the thing worked.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 21st, 2005, 06:25 PM
#6
Thread Starter
Dazed Member
Re: Generic methods or Wildcard types?
Thanks CB. Element ordering is not necessary so i should probably be able to use. List<Employee> directory = new ArrayList<Employee>(); Yes?
-
May 22nd, 2005, 09:27 AM
#7
Thread Starter
Dazed Member
Re: Generic methods or Wildcard types?
Yup everything runs fine. Thanks CB.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|