<p>While uniform collections are the obvious example, you can also build or use a Pair class in which the types of the two components are given by generic arguments. If you don't want to build it, there is a built-in class with this property: <span class="math-container">$Pair&lt;K,V&gt;$</span></p>

<p>But building one like it is simple enough if that is your goal. </p>

<p>Once you have your desired class, Write a function/method that needs to return two things instead of one. Bundle them up in a Pair. </p>

CC BY-SA 4.0
2018-10-05T11:07:33.473Z
Perfect! Thank you! This is a very obvious one. I suppose that's what I get for making slides at night.
cryptic_star
CC BY-SA 4.0
2018-10-05T10:54:30.843Z
2018-10-05T10:54:30.843Z
Buffy
<p>What are some good example classes for introducing generics in Java?</p>

<p>I'm introducing this in the context of implementing <code>ArrayList</code>, but I don't want my first example to be <code>ArrayList</code> itself due to the inability to create arrays of a generic type. I would also like my example to be something that doesn't seem contrived/has a practical use.</p>

CC BY-SA 4.0
2018-10-05T10:48:03.327Z
"Implementing" ArrayList or using the library class ArrayList from Collections?
Buffy
CC BY-SA 4.0
2018-10-10T13:17:35.270Z
You can pretty easily do a linked list.
ncmathsadist
CC BY-SA 4.0
2018-10-10T13:49:04.050Z
@ncmathsadist Definitely, but I wanted to introduce generics between array list and linked list, so that we could start off using linked lists with generics. I'm happy with the pair example mentioned below - it went over well in class.
cryptic_star
<java><data-structure><data-types><generics>
Example for Introduction to Generics in Java
# Example for Introduction to Generics in Java

(Asked by: username_0 on 2018-10-05)

What are some good example classes for introducing generics in Java?

I'm introducing this in the context of implementing `ArrayList`, but I don't want my first example to be `ArrayList` itself due to the inability to create arrays of a generic type. I would also like my example to be something that doesn't seem contrived/has a practical use.

**Tags:** java, data-structure, data-types, generics

> **Comments:**
> **username_1**: "Implementing" ArrayList or using the library class ArrayList from Collections?  
> **username_2**: You can pretty easily do a linked list.  
> **username_0**: @username_2 Definitely, but I wanted to introduce generics between array list and linked list, so that we could start off using linked lists with generics. I'm happy with the pair example mentioned below - it went over well in class.  

---

## 1 answers

### Answer by username_1 on 2018-10-05. Score: 3 | Accepted answer

While uniform collections are the obvious example, you can also build or use a Pair class in which the types of the two components are given by generic arguments. If you don't want to build it, there is a built-in class with this property: $Pair$

But building one like it is simple enough if that is your goal.

Once you have your desired class, Write a function/method that needs to return two things instead of one. Bundle them up in a Pair.

> **Comments:**
> **username_0**: Perfect! Thank you! This is a very obvious one. I suppose that's what I get for making slides at night.  

---

