Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Object-Oriented Programming (OOP)

Generic And Collections


Abdella Nurahmed
DEBRE BERHAN UNIVERSITY
DEBRE BERHAN
2019
Generics
Ability for a function or type to be written in such a way that it
handles values identically independent of their types.
An interface or class may be declared to take one or more type
parameters.
Similar to C++ templates but with more features/flexibility
Motivation:
to write flexible, general code without sacrificing type safety.
to implement type safety collections.

4/3/22 Files And Streams 2


Generics Cont… > Java collections ≤ v1.4
The initial Java collections stored values of type Object.
Could store any type, since all types are subclasses of Object (Missing type
safety).
Need to cast the results, which was tedious and error-prone.

ArrayList fruits = new ArrayList();


names.add(“Orange");
names.add(“banana");
String f1 = (String) names.get(0);

// this will compile but crash at runtime; bad


Product f2 = (Product) names.get(1);

4/3/22 Files And Streams 3


Generics Cont… > Java collections > v1.5
Uses generics to restricts the Objects to be put in a collection
Java collections stored only specified values type.
Allow to specify the type between < and >.

List<Type> name = new ArrayList<Type>();

This means ArrayList class accepts a type parameter, or that it is a generic class.

List<String> fruits = new ArrayList<String>();


names.add(“Orange");
names.add(“banana");
String f1 = names.get(0); // no cast

Product f2 = (Product) names.get(1); // error


4/3/22 Files And Streams 4
Implementing generics
// a parameterized (generic) class
public class ClassName<Type> {
or
public class ClassName<Type, Type, ..., Type> {

Note:
any class that creates an object must supply a type parameter.
• rest of the class's code can refer to that type by name.
• can create variables of that type, accept them as parameters,
return them

4/3/22 Files And Streams 5


Generics and arrays
public class Foo<T> {
private T myField; // ok
private T[] myArray; // ok

public Foo(T param) {


myField = new T(); // error
myArray = new T[10]; // error
}
}

You cannot create objects or arrays of a parameterized type.


4/3/22 Files And Streams 6
Generic methods
public static <Type> returnType name(params) {

To make just a static method generic in a class, precede its return type by type
parameter(s).

public class Collections {


...
public static <T> void copy(List<T> dst, List<T> src) {
for (T t : src) {
dst.add(t);
}
}
}
4/3/22 Files And Streams 7
Collection Framework
Set of interfaces and classes that makes easy to manage data in
application
Can manage either ordered or unordered data
And you can use one of four categories of classes
Each of the categories represents by interface named set, list, deque
and map
The Java Collections Framework provides many benefits:
 Reduces programming effort (already there)
Increases performance (tested and optimized)
Part of the core API (available, easy to learn)
Promotes software reuse (standard interface)
Easy to design APIs based on generic collections
4/3/22 Files And Streams 8
Collection Implementations
General-purpose Implementations

Interfaces Implementations

Tree
  Hash table Resizable array Linked list Hash table + Linked list
(sorted)

TreeSet
Set HashSet     LinkedHashSet
(sorted)

LinkedList
List   ArrayList    

Queue          

TreeMap
Map HashMap     LinkedHashMap
(sorted)

4/3/22 Files And Streams 9


ArrayList
Public class Main {
public static void main(String[] args)
List<String> countries = new ArrayList<>();

countries.add(“Kenya”);
countries.add(“Sudan”);
countries.add(“Ethiopia”);
System.out.println(countries);
countries.remove(0);
System.out.println(countries)
}
}
4/3/22 Files And Streams 10
HashMap
Public class Main {
public static void main(String[] args)
Map<String, String> countries = new HashMap<>();

countries.put(“Ke”, “Kenya”);
countries.put(“Su”, “Sudan”);
countries.put(“Et“, “Ethiopia”);
System.out.println(countries);
countries.remove(“Su”);
System.out.println(countries)

}
}
4/3/22 Files And Streams 11
Looping through ArrayList
Public class Main {
public static void main(String[] args)
List<String> countries = new ArrayList<>();

countries.add(“Kenya”);
countries.add(“Sudan”);
countries.add(“Ethiopia”);
for(String country : countries){
System.out.println(country);
}

}
}
4/3/22 Files And Streams 12
Looping through HashMap
Public class Main {
public static void main(String[] args)
Map<String, String> countries = new HashMap<>();

countries.put(“Ke”, “Kenya”);
countries.put(“Su”, “Sudan”);
countries.put(“Et“, “Ethiopia”);
Set<String> keys = countries.keySet();
for(String key : keys)
System.out.println(key + “ is code for” + countries.get(key));
}
}
4/3/22 Files And Streams 13
Thank You
4/3/22 Files And Streams 14

You might also like