Results 1 to 14 of 14
  1. #1
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default Trouble comparing Elements while using Generics

    I have a linked list of skipElements(K key, V value) which I need to order based on key. However I don't know how I can compare key since it is a generic.

    Even implementing Comparable<skipElement> doesn't seem to work. I tried something like: if (this.key < c.key) return -1; But they're still objects and can't simply be compared with a <. Attempting to cast into int also doesn't seem to work.

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    if possible post your compateTo method code here. I think the problem is because of object casting.
    Mak
    (Living @ Virtual World)

  3. #3
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    public class skipElement<K,V> implements Comparable<skipElement>{
        private K key;
        private V value;
    
        skipElement(K key, V value){
            this.key = key;
            this.value = value;
        }
    
        public K returnKey(){
            return key;
        }
    
        public V returnValue(){
            return value;
        }
    
        public int compareTo(skipElement t) {
            if((int)this.key < (int)t.key)
                return -1;
        }
        
    }
    the error is expected int, found K

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since you defined your key as a generic, the complier doesn't know if the < operator can apply for this element. I think something of this kind could work:
    Java Code:
    public class MyCollection<K implements Comparable,V> {
      //bla bla bla
    }
    but I am working from memory here, so I'm not sure this works. If it does, you can them compare key of type K with
    Java Code:
    if(this.key.compareTo(element.key) < 0)
    In short, when you define a generic, you can set in some restrictions.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  5. #5
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    Trying that underlines V as a compile error, and still asks me to implement the compareTo method. (no difference except change in "implements..." statement position)

    It also still doesn't solve the problem of trying to compare items of type K. I'd like it to be a direct comparison when K is an integer, and number of letters when K is a string. Is there are more accepted method of doing so?

    I thought casting would work =/. It seems the only way to tell the compiler from within the class what type K will be.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    But that's not the point of generics, if you define something as a generic you don't know what the type will be. That is why you have interfaces, to help you generalize these things. If all you want is ints and strings as your keys, just make a StringKeyMap and IntegerKeyMap with defined behaviour.
    By the way, replace implements with extends in the snippet I posted above, just tested it.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    I thought the point was to have one class instead of having multiple classes that differ in just one variable type declaration. What if I wanted floats, doubles, or even other objects that needed to be compared?

    *I extended K: K extends Comparable. But I'm not sure what I need to do after...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm no expert in this, far from it, but I would try something like this:

    Java Code:
    public class SkipElement<K extends Comparable<K>,V> implements Comparable<SkipElement<K, V>> {
       private K key;
       private V value;
    
       SkipElement(K key, V value){
           this.key = key;
           this.value = value;
       }
    
       public K returnKey(){
           return key;
       }
    
       public V returnValue(){
           return value;
       }
    
       public int compareTo(SkipElement<K, V> t) {
          return key.compareTo(t.key);
       }
    
    }

    For this to work, K had better implement Comparable<K>.

  9. #9
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    Should you use generics in this program?
    Mak
    (Living @ Virtual World)

  10. #10
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since what the OP is trying to do is implement a Map, I believe it would be plain wrong to NOT use generics.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  11. #11
    ryuzog is offline Member
    Join Date
    Jan 2010
    Posts
    32
    Rep Power
    0

    Default

    would using a comparator help? Am currently trying out fubarable's code, but someone recommended a comparator
    Last edited by ryuzog; 12-16-2010 at 07:38 AM.

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by ryuzog View Post
    would using a comparator help? Am currently trying out fubarable's code, but someone recommended a comparator

    you will need the comparator if you have to sort one object in multiple ways.

  13. #13
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Since you only sort by keys, the Comparable iterface is sufficient. Even if you did have multiple fields by which you sort you could still keep a flag and have the compareTo() made with a switch statement, but that turn nasty very quickly, that's where you would use the Comparator interface.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default

    You need both the implements you currently have and an extends on K:
    Java Code:
    public class TestMap<K extends Comparable, V> implements Comparable<TestMap> {
        private K key;
        private V value;
    
        public int compareTo(TestMap o) {
            return this.key.compareTo(o.key);
        }
    }
    This means any key class you use will have to implement Comparable, but this will allow you to define precisely what you want for your Strings, ints, and any others that you decide to use this for.

    ETA: And, no. I don;t know why I called it a Map there...:)

Similar Threads

  1. Generics
    By sakshamkum in forum Advanced Java
    Replies: 3
    Last Post: 06-03-2010, 10:12 PM
  2. Trouble listing elements in an XML
    By circle in forum XML
    Replies: 1
    Last Post: 05-27-2010, 09:06 AM
  3. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  5. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 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
  •