Generic « List « Java Collection Q&A

Java Collection Q&A
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
14.Garbage Collection
Java Collection Q&A » List » Generic 

2. generic interface: list of something specific

I want to define an interface MyList which is a list of interface MyThing. Part of the semantics of MyList is that its operations don't have any meaning on objects which ...

3. Java: Deriving from a generic List/Collection

I have a little problem understanding the Java language

public class PhonebookEntryList extends List<PhonebookEntry>
    public PhonebookEntryList(String filename) throws IOException

4. simple generic list in java

Why are java generics so tricky? I thought I finally understood, but eclipse gives me an error at the line in somOtherMethod below using either of the getOuterList methods below. ...

6. Java List parameterized?

I am quite new to Java ... I wrote a class called DLPFile which is basically a container of other objects like Strings, ints, floats, etc. When putting my files into a ...

7. java list of objects that all implement an interface

Suppose I have a class that implements an interface:

public class A implements IB
and I have a List<A> that I would like to reference to: List<? implements IB> list. So that I can ...

8. What does List mean in java generics

What does List<?> mean, does it mean simply a list of objects of unspecified type? Googling for the string <?> returns nothing useful (:

9. Java Prevent code duplication when getting a list of a property in each Object from a list of Objects

I have several objects that look like this:

class PhoneNumber
   String   getNumber();
   String   getExtension();
   DateTime getLastCalled();
class Address
   String getCity();

10. How to iterate over a mixed list using foreach?

I'm wondering how to iterate over a List with mixed contents using foreach. See the example code below.

public class GenericsForeach {

    class A {

11. Transfering a Set with a Wildcarded Generic to a List in Java

I have a data type that contains a set and a method that expects List<? extends MyClass>. The data type has Set<? extends MyClass>. I need to be able ...

12. Generics List Interface...newb question

The List interface is the following:

public interface List<E>{
public boolean add(Object e);
public boolean remove(Object e);
public boolean contains(Object e);
Why aren't the add, remove and contains methods written like the following?
public boolean add(E e)
public ...

13. Why doesn't Java allow "new List"?

To create a List, why doesn't Java allow them to be created then elements added one by one? This works:

public static List<TrackedItem> create(List<Item> items)
    TrackedItem[] arr = new TrackedItem[items.size()];


14. Using @JsonTypeInfo annotation for generic object lists

I am trying to serialize and de-serialize (with Json and ObjectMapper) an polymorphic list of Objects that have the following annotation on the base class:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class")
When I write ...

16. How to iterate over this generic List with wildcards?

I have a List of objects that extend another class:

List<? extends Fruit> arguments;
Now, I want to invoke a method on these objects. The invoking class has a method wash for each ...

17. Get rid of warnings in List>.toArray()

Another one of those how do I do toArray() with no warnings questions, but it's different from most of the ones posted here. How do I rewrite the method implementation (without changing ...

18. Need to filter a list to a particular subclass using generics

I have a List that contains a certain superclass (like Vehicle), and I would like to write a method that returns the objects in that list that are instances of a ...

19. Java Generics in a list, how to process List>

I have a class, MyObject<T> and a list: List<MyObject<?>>. Contents of the list (example):

  • MyObject<Date>();
  • MyObject<Integer>();
Then the MyObject<T> class contains a function:
public T getValue() {
    return this.someAttribute;
How can I iterate over ...

20. Apache Commons BeanUtils get list properties

In Apache Commons BeanUtil, how to get a type inside a list ? for example

class Track {
   List<Actor> actorList = new ArrayList<Actor>();

System.err.println(PropertyUtils.getPropertyType(trackBean, "actorList"));
// it should give me Actor instead ...

21. Doclet- Get generics of a list

I am writing a doclet extending com.sun.javadoc.Doclet. When i want want to document an ArrayList as a field of a method i want to get the type of the generic (e.g. when ...

22. Java generics confusion

I am a bit of confused about java generics Here is the code

class Base{}

class Derived extends Base{}
WE can instantiate a list like this
List<? extends Base> list = new ArrayList<Base>();
Why cannot I add ...

23. return a list of interfaced objects

I have a class Category that implements the interface CategoryManager. Next there is Card that maintains a List<Category>. Now I would like to return this list, but with categories as CategoryManager, hence ...

24. how to instantiate an object based on

I want to instantiate an object based on the type of the list(that is Truck) and add to the list, I try something below, but it warns me with "sun.reflect.generics.reflectiveObjects.TypeVariableImpl incompatible ...

26. Is it possible to use generics in lists in Java 1.4?

I have to use java 1.4 and am using arraylist structures. I now need to do some re-factoring and it would help if I can use generics. I currently have code ...

27. Java List toArray(T[] a) implementation

I was just looking at the method defined in the List interface:

Returns an array containing all of the elements in this list in the correct order; the runtime type ...

29. Determine generic arguments of List<>

I'm using reflection to walk the field members of a class and I need to know for List<> subclasses, what the generic type parameters are. Given a field that has a type ...

30. List of Implementors?

I have some classes that all implement the same Java interface, which essentially allows fetching SQLite ContentValues associated with the particular implementor, and these classes are grouped into lists in various ...

31. List with generics is not being identified by RAD6

I am using RAD6. I wrote the following code.

private List<C1> list;
public List<C1> getList() {
return list;
But on second line of above code compiler is giving following two errors. 1: Syntax error on token ...

33. generic list

Hi All, I have a generic java List something like this List. The Car object looks like this --------------- public class Car { private String name; private String model; private int year; } --------------- The List collection could be huge in size. I need to create a sublist from this huge list based on a condition. What i am currently doing ...

34. best way to iterate a generic list

You will have to walk through the list until you find the object you are looking for. For example: Custom find(List list, String name) { Custom result = null; for (Custom c : list) { if (c.getName().equals(name)) { result = c; break; } } return result; } (assuming your class Custom has a getName() method). But you should consider using a ...

35. nullifying an object in a generics list collection

Hi All, Currently i have a scenario where in i get a generic list collection as an input parameter. In the method I scrutinize the collection in a for loop and based on some condition I want to nullify the custom object which is part of this collection. I am setting this object to null but still when in another method ...

36. Implementing interface with list of generics

Hi, I am experimenting with generics and running into a problem where I cannot set a List I have the following: public interface Order { public List getItems(); public void setItems(List items); } public interface OrderItem { } public calss OrderItemImp implements OrderItem { } public class OrderImp implements Order { public List getItems() { ...

37. Reflectively Instantiating a Generic List

Forgive me if this has been asked in another form (I did search and didn't find much), but I'd like to instantiate a List, with generic type information reflectively. For example, here's some code that I'd like to make work (pseudo-java): String someClassname = "java.util.Vector"; Class> classInstance = Class.forName(someClassname); List instantiatedList = classInstance.newInstance(); I've been looking around for a ...

38. generic linked lists

39. Difference between List & Generic List in 1.5

With the generic List you are telling the compiler that the List will contain only Customer objects. This modifies the signatures of those methods in List that set, add or retrieve items from the list so that they take or return Customer instead of Object. The code doesn't change in adding an item but the compiler checks that what you're adding ...

40. Generics: Handling List>  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.