Home » Android » What is List<?> in Java (Android)?

What is List<?> in Java (Android)?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Possible Duplicate:
What is Type<Type> called?
What does List<?> mean in java generics?

package com.xyz.pckgeName;

import java.util.ArrayList;
import java.util.List;

public class Statement {

// public String
public String status;
public String user_id;
public String name;
public String available_balance;
public String current_balance;
public String credit_card_type;
public String bank_id;
public List<Statements> statements = new ArrayList<Statement.Statements>();

public class Statements {
    public String month;
    public String account_id;
    public String user_id;
    public String id;
    public List<Transaction> transactions = new ArrayList<Transaction>();
}
}

Can anyone explain me what these two statements mean

public List<Statements> statements = new ArrayList<Statement.Statements>();

public List<Transaction> transactions = new ArrayList<Transaction>();
How to&Answers:

This is Generics in java

List<?> is essentially translated as a “List of unknowns”, i.e., a list of unknown types. The ? is known as a Wildcard (which essentially means unknown).


public List<Statements> statements = new ArrayList<Statement.Statements>();

This essentially creates a List that only accepts Statement.Statements. Anything outside of Statement.Statements that you want to add to statements will create a compilation error. The same applies to public List<Transaction> transactions = new ArrayList<Transaction>();. This means that the List is bounded to a type Statement.Statements (on statements variable).

Answer:

It is the use of generics. You are declaring a List of either Statement objects or Transaction objects.

Check out wikipedia for more info

http://en.wikipedia.org/wiki/Generics_in_Java

Answer:

You should read about Generics to understand this . List is a raw type which could be the list of Objects such as Strings,Wrappers and user defined objects .

public List<Statements> statements = new ArrayList<Statement.Statements>();

The above code says that statements is the reference to the ArrayList of Statement Objects . It does specifies that list would contain Statements objects which is inner class to the Statement class.

List<?> is used to say the list of unknown type.

Answer:

The statements List can hold only Statements object. It is called Generics. Check this for sample programs.

http://www.java2s.com/Code/Java/Language-Basics/Asimplegenericclass.htm