Results 1 to 5 of 5

Thread: uniq

  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default uniq

    In one word, what's wrong with this method?
    Idea is that it should return a list that contains no duplicates


    Java Code:
    private List<String> uniqueArguments(ArrayList<String> arrayList) {
       List<String> noDups = (List<String>) new HashSet<String>(arrayList);
    		return noDups;
    }
    In anticipation of why i think there is something wrong with it, the following is a warning i get "Type safety: Unchecked cast from HashSet<String> to List<String>"

    And also, it does not work :(
    Exception in thread "main" java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.List

    Java Code:
    private List<String> uniqueArguments(ArrayList<String> arrayList) {
    	System.out.println(arrayList);
    	HashSet<String> noDups = new HashSet<String>(arrayList);
    	System.out.println(noDups);
    	return (List<String>) noDups;
    }
    1 more comment, sysouts work and print what's expected, but still
    Exception in thread "main" java.lang.ClassCastException: java.util.HashSet cannot be cast to java.util.List
    Last edited by mac; 04-06-2010 at 06:38 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Ok ... any advise on how to go about it?

    Have a List, need to check if it contains unqiue elements

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by mac View Post
    Ok ... any advise on how to go about it?

    Have a List, need to check if it contains unqiue elements
    A you created a Set given a List so you can create another List given a Set.

    kind regards,

    Jos

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

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