Results 1 to 5 of 5

Thread: generic code

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

    Default generic code

    I have these 2 methods working independently, basically doing the exact same thing to different data structures. Can you suggest a way to create a generic method instead of using 2?

    Java Code:
    public String[] uniqueArguments(String[] arguments) {
      Set<String> noDups = new HashSet<String>(Arrays.asList(arguments));
    	return noDups.toArray(new String[noDups.size()]);
    }
    Java Code:
    public static ArrayList<String> uniqueArguments(List<String> u) {
      HashSet<String> mySet = new HashSet<String>(u);
      return new ArrayList<String>(mySet);
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    err... nope, cant do that

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

    Default

    Looking at the way you workout it's not possible. One reason I've seen is that the difference between those interfaces.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Arrays don't play well generics.
    The best you can do is
    Java Code:
            public static <T> List<T> uniqueList(Collection<T> collection) {
    		Set<T> mySet = new HashSet<T>(collection);
    		return new ArrayList<T>(mySet);
    	}
    But it's not as bad as you think. If you have an array you can still use the method after calling the Arrays.asList method on it e.g

    Java Code:
    String[] stringsArray = new String[] { "f", "g", "f" };
    System.out.println(uniqueList(Arrays.asList(stringsArray)));

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

    Default

    Quote Originally Posted by r035198x View Post
    But it's not as bad as you think. If you have an array you can still use the method after calling the Arrays.asList method on it
    Interesting point.

Similar Threads

  1. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 11:29 PM
  2. Generic methods
    By andre1011 in forum Advanced Java
    Replies: 7
    Last Post: 02-25-2009, 03:17 PM
  3. A generic interface example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:42 PM
  4. Generic array
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:12 AM
  5. Generic Hashtables
    By ShoeNinja in forum New To Java
    Replies: 0
    Last Post: 12-04-2007, 11:43 PM

Posting Permissions

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