-1

i have a function:

public static bool Append<T>(this List<T> list, T value)
        where T : IComparable<T>
    {
        int l = 0;
        int r = list.Count() - 1;

        int c = 0;

        var com = 0;
        while (l <= r)
        {
            c = (l + r) / 2;
            com = list[c].CompareTo(value);
            if (com > 0) r = c - 1;
            else if (com < 0) l = c + 1;
            else
            {
                list[c] = value;
                return false;
            }

        }

        if (c >= list.Count())
            list.Add(value);

        else
        {
            if (com < 0) c++;
            list.Insert(c, value);
        }

        return true;
    }

and i want to recreate it in java, but is there something equivalent to the where T: IComparable<T> in java?

public static <T> void append(List<T> list)
{
}

i know java have the interface java.lang.Comparable but i dont know how to implement it in a function

thiago
  • 9
  • 6

1 Answers1

0

You can use Wildcards in Generics to achieve the same. Your method signature would look like this:

public static void append(List<? extends Comparable> list)

Or even better and exactly the same meaning as your C# code:

public static <T> void append(List<? extends Comparable<T>> list, T value)
Ulrich Stark
  • 401
  • 3
  • 12