Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JosAH

Thread: HashSet cannot be cast to java.util.ArrayList

  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default HashSet cannot be cast to java.util.ArrayList

    Java Code:
    public class Employee{
        
        private int id;
        private String name;
        private String address;
        private double salary;
    //setter & getter
    }
    Java Code:
    public class test{
    
    		ArrayList check= coll (); 
    		CheckCollection(check);
    
    public static ArrayList coll (){
    
    		ArrayList list = new ArrayList();
    		Set set = new HashSet();
    
    Employee ts1 = new Employee (10, "Jad", "L.A", 200.0);
            Employee ts2 = new Employee (11, "Amal", "Berlin", 600.0);
            Employee ts3 = new Employee (12, "Jasmin", "London", 250.0);
            Employee ts4 = new Employee (10, "sam", "Paris", 200.0);
            Employee ts5 = new Employee (14, "Adam", "NY", 210.0);
    
    set.add(ts1);
    set.add(ts2);
    set.add(ts3);
    set.add(ts4);
    set.add(ts5);
    
    //now convet the Set to List
    list.add(set)
    
    return list;
    }
    
    public static void CheckCollection(ArrayList data) {
    ArrayList xxx= null;
    for (int i = 0; i < data.size(); i++) {
    	       		
    	       	    xxx = (ArrayList)data.get(i);
    }
    }
    any way when i run it i get error:
    XML Code:
    java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.ArrayList
    so why i thought i could convert the Set totally to List Type !!

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,835
    Rep Power
    19

    Default Re: HashSet cannot be cast to java.util.ArrayList

    Java Code:
    list.add(set);
    That doesn't convert the Set to a List, it adds the Set to the List.

    Why (exactly) do you feel you need to convert your Set into a List?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,369
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashSet cannot be cast to java.util.ArrayList

    In line #24 you're adding an entire Set as a single element to a List; in line #33 you're trying to cast that single element t an ArrayList; that does'nt work. Did you want to add all elements of the Set to the List? There's the addAll( ... ) method or there''s a constructor of the ArrayList that can do it.

    kind regards,

    Jos
    gimbal2 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,730
    Rep Power
    5

    Default Re: HashSet cannot be cast to java.util.ArrayList

    Indeed, what Jos says. And this problem would have never happened if generics were used.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,835
    Rep Power
    19

    Default Re: HashSet cannot be cast to java.util.ArrayList

    By the way, this is a continuation of this thread here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: HashSet cannot be cast to java.util.ArrayList

    Why (exactly) do you feel you need to convert your Set into a List?
    because as you see, i need to use the result as ArrayList in another method

    Did you want to add all elements of the Set to the List?
    Yes i want to add every element of Set to List (not as one object)

    There's the addAll( ... ) method or there''s a constructor of the ArrayList that can do it.
    I used dataSheet.addAll(set);before but still got the same error
    constructor of the ArrayList, do you meant:ArrayList(Collection<? extends E> c) as generic

    this problem would have never happened if generics were used
    do you mean if i used them like this
    Java Code:
     ArrayList<Employee> list = new ArrayList<Employee> ();
            Set<Employee>  set = new HashSet<Employee> ();
    then i will get no error?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,369
    Blog Entries
    7
    Rep Power
    20

    Default Re: HashSet cannot be cast to java.util.ArrayList

    Quote Originally Posted by vector_ever View Post
    do you mean if i used them like this
    Java Code:
     ArrayList<Employee> list = new ArrayList<Employee> ();
            Set<Employee>  set = new HashSet<Employee> ();
    then i will get no error?
    Yes, you will get an error but it would be an error message issued by the compiler, not from the JVM itself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: HashSet cannot be cast to java.util.ArrayList

    Yes, you will get an error but it would be an error message issued by the compiler, not from the JVM itself
    ok any Way i still the same error, what ever if it from compiler or JVM

  9. #9
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. change arraylist to hashSet or TreeSet
    By rain528919 in forum New To Java
    Replies: 6
    Last Post: 07-13-2012, 10:27 AM
  2. Replies: 1
    Last Post: 06-19-2012, 02:55 AM
  3. HashSet/arraylist
    By jjitss in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 12:43 AM
  4. Problem in ArrayList class(java.util.*)
    By Cosmos in forum New To Java
    Replies: 4
    Last Post: 08-15-2011, 10:50 AM
  5. Using java.util.ArrayList in J2ME (jre 1.4)
    By anam siddiqui in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-06-2011, 07:50 AM

Posting Permissions

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