-3

I want to create the method that accepts both the Generic element type (T t) and its list (List tList), and adds the element to the list.

I get the error:

The method addDataToList(T, List) in the type IteratorExample is not applicable for the arguments (String, List)
public class IteratorExample {
    public static <T> void main(String[] args) {

        List<T> tList = new LinkedList<>();
        addDataToList("First Element", tList);
        addDataToList("String", tList);
        addDataToList(100986, tList);
        addDataToList(1.98F, tList);
        addDataToList(1009L, tList);
        addDataToList('x', tList);
    }

    static <T> void addDataToList(T t, List<T> tList) {
        tList.add(t);
    }
}
1
  • 1
    First, your main method should not be generic. What exactly are you trying to do here? You want to add string, int, long, floating point, char in the list, why not just use List<Object>? Commented Dec 7, 2023 at 11:34

1 Answer 1

1

Every class is a subtype of Object. If you want to have a List of everything, make it List<Object>. Primitive types are not objects, but when calling a method which requires an argument of type Object and doesn't provide an overload for the appropriate primitive, the primitive gets auto-boxed to its representative boxed type (e.g. int -> Integer) which then gets downcasted to Object such that it fits the type of the parameter.

List<Object> myEverythingBagel = new ArrayList<Object>();
myEverythingBagel.add(1);
myEverythingBagel.add("String");
// etc.

That is how you could do that, but the bigger question is why would you want to do that?

An everything-bagel collection can consume anything but will cause trouble when reading from it. Rather consider having dedicated collections for your different values.

Or better yet, when you have a relationship between those values, like name and age introduce a dedicated type for that entity. And for that take a look at Java records.

record Person(String name, int age)

Then you can store these people in a List<Person> people instead.

Tangent

Having a method parameter List<T> list is for when you want to pass differently typed Lists into the same method.

Keeping this method or it having generic parameters is not necessary when the initially created one is of type List<Object>. Because you just can call list.add(value) on it directly.

If you want to keep your "addDataToList" method (you could have planned it to do some logging or have other logic), you could change its signature to the following:

static <T> void addDataToList(Object t, List<Object> tList) {
    tList.add(t);
}

And your main method shouldn't be generic.

2nd Tangent

Forget that LinkedLists exists. They are only academically interesting when teaching data structures.

Most of the time you would want to use an ArrayList or if you want to represent a Stack/Queue take a look at ArrayDeque it implements the same Interface (Deque = Double-ended Queue) as LinkedList but performs much better.

Sign up to request clarification or add additional context in comments.

4 Comments

Why is your suggested method in Tangent 1 generic? You don't use T.
@AndyTurner, I suspected OP of having issues with generic collections in the first place, which I tried to address in the main body of the answer. And as mentioned in the tangent the method as-is is redundant.
It's not redundant in OP's declaration, though: that method could be called with a List<Object>, a List<String> etc (given a compatible t, ofc). The problem isn't there, it's that OP creates a List<T> in the main method, and then tries to invoke the method with non-compatible values of t (just as you couldn't invoke tList.add("..."); directly).
@AndyTurner If your tList is of generic type Object you surely can invoke tList.add("..."); Everything is either of type object or will be auto-boxed to object. I will stand to my observation that the addDataToList method is redundant when the only collection concerning is of type Object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.