Results 1 to 5 of 5
  1. #1
    Ashish Soni is offline Member
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0

    Post Java Generic Method

    private <String, Integer extends Comparable<? super Integer>> List<Entry<String, Integer>> mapSortByValues(Map<String, Integer> map)
    this is the function header which i found on google, this method gives me Map in sorted order by values. But i am able to pick up the actual syntax of function header. Can anyone help me out to get it.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Java Generic Method

    You do not need the first part: <String, Integer extends Comparable<? super Integer>>

    It doesn't even make sense: Comparable is an interface, so Integer cannot extend it, only implement it. On top of that, Integer already implements Comparable. Leave that part out and the method definition starts to make sense.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java Generic Method

    Quote Originally Posted by SurfMan View Post
    Comparable is an interface, so Integer cannot extend it, only implement it.
    Except for generic typing. For interfaces and classes use extends in generic type constructs. For Comparable, the general format for some class
    would be

    Java Code:
    public class MySort(T extends Comparable<? super T>>) {
    
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Java Generic Method

    Quote Originally Posted by jim829 View Post
    Except for generic typing. For interfaces and classes use extends in generic type constructs. For Comparable, the general format for some class
    would be

    Java Code:
    public class MySort(T extends Comparable<? super T>>) {
    
    }
    Regards,
    Jim
    I agree on the "extends" part. It's valid but confusing use of "extends".

    Still, I can't see the use case for OP's generic method. First of all he named the type parameters the same as the classes which is confusing. And both String and Integer are final, so no class can extend from it that would justify the use of generics.

    Even if you renamed the type parameters to the more traditional T and U, it doesn't make sense. The super classes of Integer are Number and Object, and both of them don't implement Comparable, so they are no candidates for U.

    The only way I can see this work is this:

    Java Code:
    import java.util.*;
    
    public class Generics {
    
        private static <T, U extends Comparable<? super U>> List<Map.Entry<T, U>> mapSortByValues(Map<T, U> map) {
            List<Map.Entry<T, U>> entries = new ArrayList<>();
            return entries;
        }
    
        public static void main(String[] args) {
            Map<String, Integer> map1 = new HashMap<>();
            List<Map.Entry<String, Integer>> result1 = mapSortByValues(map1);
    
            Map<String, MyClass> map2 = new HashMap<>();
            List<Map.Entry<String, MyClass>> result2 = mapSortByValues(map2);
        }
    
    
        private static class MyClass implements Comparable<MyClass> {
            @Override
            public int compareTo(MyClass o) {
                return 0;
            }
        }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java Generic Method

    I wasn't referring to the code, just the use of extends with interfaces. However, the first part after the private declaration doesn't really mean String or Integer as those would be type parameters (normally a capital letter). So as written, they conflict with the actual types of String and Integer. So I don't know what the OP is trying to do.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2016, 04:53 PM
  2. Question about a generic method
    By Manddd in forum New To Java
    Replies: 2
    Last Post: 08-17-2015, 02:28 PM
  3. Generic method Java tutoring help
    By judemartin99 in forum New To Java
    Replies: 1
    Last Post: 05-01-2013, 04:13 AM
  4. About generic, cant use method
    By oszc in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 06:35 PM
  5. Generic method
    By jomypgeorge in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 05:51 AM

Tags for this Thread

Posting Permissions

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