Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convert a non-generic List to a List<String>?

When I try to convert using Java 8 streams, I get a compile error:

Incompatible types
Required: List
Found: java.lang.Object

    ArrayList list = new ArrayList();
    list.add("Test");
    list.add("Java");
    list.add("Other");

    //This won't compile
    List<String> strings = list.stream()
        .map(object -> Objects.toString(object, null))
        .collect(Collectors.toList());
like image 898
Don Rhummy Avatar asked Apr 25 '18 15:04

Don Rhummy


People also ask

What is the difference between generic and non-generic?

While brand name drug refers to the name giving by the producing company, generic drug refers to a drug produced after the active ingredient of the brand name drug. Generic drugs will, however, be sold under different brand names, but will contain the same active ingredients as the brand-name drug.

What is non-generic in java?

The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects. It makes the code stable by detecting the bugs at compile time. Before generics, we can store any type of objects in the collection, i.e., non-generic. Now generics force the java programmer to store a specific type of objects.

Is ArrayList a subtype of list?

That is, ArrayList<String> is a subtype of List<String> , since ArrayList is a subtype of List and both have the same parametric type String .

What is a non-generic class?

Non-generic, in other words, concrete, classes can inherit from closed constructed base classes, but not from open constructed classes or from type parameters because there is no way at run time for client code to supply the type argument required to instantiate the base class.

How do you convert a list to a string in Python?

List comprehension in python generates a list of elements from an existing list. It then employs the for loop to traverse the iterable objects in an element-wise pattern. To convert a list to a string, use Python List Comprehension and the join () function.

Can you refactor your old non-generic list to generic list?

While refactoring old code, you might have already encountered that the classes are inundated with lots of non-generic List. It is important for a codebase to evolve and that’s why I would like to discuss about how to refactor your old non-generic List to generic List.

What is the difference between list<T> and list<L>?

T is the type of each item in the resulting list. L is the type of the list that I wish to create (which extends List<T> ). untypedList is the "untyped" input list, effectively the same as List<Object>. Show activity on this post.

What is the difference between a list and a string?

The point to be noted here is that a list is an ordered sequence of object types and a string is an ordered sequence of characters. This is the main difference between the two. A sequence is a data type composed of multiple elements of the same data type, such as integer, float, character, etc.


1 Answers

From comment:

I'm actually calling a 3rd party method that returns List

Then cast the value to a List<Object> or List<?> (thanks, Sean Van Gorder) so it is not a raw generic anymore, and the stream code will work:

List<?> list = thirdPartyMethod();

List<String> strings = list.stream()
        .map(object -> Objects.toString(object, null))
        .collect(Collectors.toList());

Logically, there is no difference between a List and a List<Object>, but there is a big difference to the compiler, because a raw generic forces backwards-compatibility mode in the compiler, where all generics are ignored, causing the entire stream-chain to behave badly.

like image 173
Andreas Avatar answered Sep 28 '22 13:09

Andreas