Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.hanitlg.generics;
- import java.util.*;
- public class App {
- public static <T extends Comparable<T>> T max(Collection<T> collection) {
- T maxValue = null;
- for (T element: collection) {
- if (maxValue == null) {
- maxValue = element;
- } else {
- if (element.compareTo(maxValue) > 0) {
- maxValue = element;
- }
- }
- }
- return maxValue;
- }
- public static void main(String[] args) {
- List<Contact> contactList = new ArrayList<>();
- contactList.add(new Contact("Juergen", "Falb"));
- contactList.add(new Contact("Michael", "Strommer"));
- contactList.add(new Contact("Jochen", "Hense"));
- System.out.println("Maximum: " + max(contactList).getFirstName());
- for (Contact c: contactList) {
- System.out.println("Name: " + c.getFirstName() + " " + c.getLastName());
- }
- List<Integer> integerList = new ArrayList<>();
- integerList.add(12);
- integerList.add(45);
- integerList.add(99);
- System.out.println("Maximum: " + max(integerList));
- List<String> stringList = new ArrayList<>();
- stringList.add("Hello");
- stringList.add("World");
- stringList.add("FH");
- System.out.println("Letzter Buchstabe im Alphabet der Liste (Wort): " + max(stringList));
- MyList<Integer> IntegerMyList = new MyList<>();
- IntegerMyList.add(45);
- IntegerMyList.add(366);
- IntegerMyList.add(35);
- for (int i = 0; i < IntegerMyList.getSize(); i++) {
- System.out.println("Element: " + i + IntegerMyList.get(i));
- }
- Map<Integer, Contact> map = new HashMap<>();
- map.put(1001, contactList.get(0));
- map.put(1002, contactList.get(1));
- map.put(1003, contactList.get(2));
- Contact c = map.get(1002);
- System.out.println("Mitarbeiter 1002: " + c.getFirstName());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment