Results 1 to 7 of 7

Thread: Generic methods or Wildcard types?{Resolved}

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Resolved 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);
      }
     }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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);
      }
     }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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")); 
      }
     }

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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?

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width