Professional Documents
Culture Documents
Generic and Collections: Object-Oriented Programming (OOP)
Generic and Collections: Object-Oriented Programming (OOP)
This means ArrayList class accepts a type parameter, or that it is a generic class.
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
To make just a static method generic in a class, precede its return type by type
parameter(s).
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)
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