Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

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

  1. #1
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default HashSet cannot be cast to java.util.ArrayList

    public class Employee{
     
        private int id;
        private String name;
        private String address;
        private double salary;
    //setter & getter
    }

    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:
    HTML 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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    Are you asking us why you thought what you thought? We can't answer that.

    If you're also wondering how to convert a HashSet to an ArrayList, here's an example of how to do that:
    // found here:
    // http://www.xyzws.com/javafaq/how-to-convert-a-hashset-to-an-arraylist/180
     
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
     
    public class TestClass {
     
      public static void main(String[] args) {
     
        Set<String> hashset = new HashSet<String>();
        hashset.add("A");
        hashset.add("B");
        hashset.add("C");
        List<String> list = new ArrayList<String>(hashset);
        System.out.println(list.toString());
     
      }
    }

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    46
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    ok the same what i did, i also make the same
    ArrayList list = new ArrayList(set);
    but still get the error

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/80700-hashset-cannot-cast-java-util-arraylist.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    If you want help with updated code, please post it. We can't accurately imagine what you've done.
    ArrayList list = new ArrayList(set);
    but still get the error[/QUOTE]
    No, that change is not equivalent to what I posted. This is:
    public class TestClass
    {
        // modified to simply make the point, not provide the same function
        public static void coll()
        {
            Set<Employee> set = new HashSet<Employee>();
     
            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 and print the results
            ArrayList<Employee> list = new ArrayList<Employee>(set);
            System.out.println( list.toString() );
     
            // this is your old line, commented out
    //        list.add(set);
     
        } // end method coll()
     
        public static void main( String[] args )
        {
            coll();
        }
    }

Similar Threads

  1. Replies: 4
    Last Post: April 16th, 2013, 06:50 AM
  2. java.lang.String cannot be cast to java.util.Hashtable
    By ashin12 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 1st, 2012, 06:17 AM
  3. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  4. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM