0

Random Code:

// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPhoneNumber(cursor.getString(2));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

See where it says

public List<Contact> getAllContacts() {

So I want to ask, why does it need the class "Contact" inside tags next to List? Anyone able to help me here?

I even tried to Google the use of tags in Java, but I cannot find anything.

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Kevin
  • 51
  • 3
  • 13
  • I highly recommend Angelika Langer's FAQs on Java Generics. http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html – The_301 Jun 06 '13 at 15:22
  • It is called a [generic type](http://docs.oracle.com/javase/tutorial/java/generics/), [Wikipedia](http://en.wikipedia.org/wiki/Generics_in_Java). – Lion Jun 06 '13 at 15:25

4 Answers4

4

It doesn't need the class Contact inside of the return type. In fact in the old version of java, pre JDK 5, this was the only way to write the method.

public List getAllContacts() {...}

The problem with this is you don't know what type of Class the List contains, Generics allow you to specify this. So by using the <> you can specify what Class the List contains, in this case Contact.

public List<Contact> getAllContacts() {...}

By making the return class explicit it allows the compiler to do class cast checks and allows you to write code that is easier to debug as you know what you are getting inside of that List.

greedybuddha
  • 7,488
  • 3
  • 36
  • 50
1

It's called Generics, introduced since JDK 5. Java List has been Generi-fied to allow developers to specify a type (called parameterized type). This essentially can be translated as a "List of Contact". You can add Contact into the list and retrieve Contact from the List.

Follow the tutorial on Generics on the Oracle Website.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Is `List` the parameterized type or the type that the developer specifies? – Seeta Somagani Jun 06 '13 at 15:23
  • No, `Contact` is the parameterized type. Anything inside the `<>` is the parameterized type. – Buhake Sindi Jun 06 '13 at 15:24
  • But `Contact` doesn't have to know where it is used. If `Contact` IS the parameterized type, then won't all types be parameterized types? – Seeta Somagani Jun 06 '13 at 15:26
  • I don't understand what you mean. You're just using a List that contains `Contact`'s. Where it has been used is irrevelant. – Buhake Sindi Jun 06 '13 at 15:28
  • We agree that the qualifier 'parameterized type' signifies something. I don't think it signifies the context that a type is used in, rather the type itself. I hence argue that `List` is the parameterized type here since it accepts type parameters. – Seeta Somagani Jun 06 '13 at 15:30
  • Angelika Langer of course gives the excellent explanation that `List` would be a generic type and `List` would be a parameterized type. – Seeta Somagani Jun 06 '13 at 15:38
  • @seeta, `List` is a Generic interface that accepts any parameterized type. In your case, the parameterized type is `Contact`. This, essentially helps in the context of getting a `Contact` as in `Contact c = list.get(0)` and setting `Contact` as in `list.add(contact)`. This eliminates type casting that existed prior to JDK 5. – Buhake Sindi Jun 06 '13 at 16:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31345/discussion-between-seeta-and-buhake-sindi) – Seeta Somagani Jun 06 '13 at 16:45
1

It's not a tag, it's creating an instance of a generic type specialized to the appropriate type. List<> is a generic list; it can contain anything. List<Contact> is a list where the elements in the list are instances of the Contact class.

See http://docs.oracle.com/javase/tutorial/extra/generics/intro.html

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
AndyL
  • 341
  • 1
  • 2
  • 8
0

That is what is called a 'generic'. In this code it means "List of type Contact" Here is a question where it is answered well: Java Generics: List, List<Object>, List<?>

Community
  • 1
  • 1
Josh Maggard
  • 419
  • 3
  • 9