Method has same erasure: How should I go about refactoring? Currently, in a utility class I have methods like so:

public static String getNextAvailableNumber(List&lt;MyObject&gt; currentObjects)
{
    int nextNumber = UtilClass.getNextAvailableNumberAsInteger(currentObjects, null);
    return UtilClass.formatNumber(nextNumber); //formats number as string
}

public static int getNextAvailableNumberAsInteger(List&lt;MyObject&gt; currentObjects, List&lt;String&gt; otherUnavailableNumbers)
{
    //returns nextNumber as int.
}


I now find myself wanting to add another method...

public static String getNextAvailableNumber(List&lt;String&gt; unavailableNumbers)
{
    int nextNumber = UtilClass.getNextAvailableNumberAsInteger(null, unavailableNumbers);
    return UtilClass.formatNumber(nextNumber);
}


You may notice that I would end up with a List argument in both places, causing a method has same erasure compiler error.

I'm considering the following solution..

//replace old getNextAvailableNumber() with generic version
public static String getNextAvailableNumber(List&lt;?&gt; objects)
{
    List&lt;MyObject&gt; objectList = new ArrayList&lt;MyObject&gt;();
    List&lt;String&gt; stringList = new ArrayList&lt;String&gt;();

    if (objects.get(0) instanceof MyObject)
    {
        objectList = (List&lt;MyObject&gt;) objects;
    }
    else if (objects.get(0) instanceof String)
    {
        stringList = (List&lt;String&gt;) objects;
    }
    int nextNumber = UtilClass.getNextAvailableNumberAsInteger(objectList, stringList);
    return UtilClass.formatNumber(nextNumber);
}


From what I've read, using instanceof is generally a sign that something can be done in a better way. Is there a better way? How should I go about refactoring this?
I strongly suspect that you have the wrong problem -- this feels like UtilClass.getNextAvailableNumberAsInteger is implemented incorrectly.  It's also not clear why you want to use the same method name when the argument has a completely different meaning.

That said... my recommendation would be to create a new function object.

class NumberGenerator {
    private List&lt;MyObject&gt; objects = null;
    private List&lt;String&gt; unavailableNumbers = null ;

    public NumberGenerator withObjects(List&lt;MyObject&gt; objects) {
        this.objects = objects;
        return this;
    }

    public NumberGenerator withUnavailableNumbers(List&lt;String&gt; unavailableNumbers) {
        this.unavailableNumbers = unavailableNumbers;
        return this;
    }

    public int getNext() {
        return UtilClass.getNextAvailableNumberAsInteger(objects, unavailableNumbers);
    }

    // OPTIONAL
    public static NumberGenerator newInstance () {
        return new NumberGenerator();
    }
}


Then your clients can use the NumberGenerator to get what they need, without having to worry about the details

int nextNumber = NumberGenerator.newInstance().withUnableNumbers(otherNumbers).getNext();


Other comments: it looks really odd that your "other unavailable numbers" are stored in a collection of String.  It looks somewhat odd that you are using a collection of MyObject to produce integers as well.  Why do the collections need to be List, as opposed to Collection or Iterable?

    if (objects.get(0) instanceof MyObject)


That's an awful idea; you're rooked if the array you are passed is empty.  If it's true that an empty list means you don't care what kind of list it is, that very strongly suggests that the entire interface is broken.

List&lt;MyObject&gt; objectList = new ArrayList&lt;MyObject&gt;();


If the list isn't supposed to by modified by the function, then you might be better off using Collections.EMPTY_LIST; it would help clarify your intent.