Guest User

Untitled

a guest
Nov 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. package de.hanitlg.generics;
  2.  
  3. import java.util.*;
  4.  
  5. public class App {
  6.  
  7. public static <T extends Comparable<T>> T max(Collection<T> collection) {
  8. T maxValue = null;
  9. for (T element: collection) {
  10. if (maxValue == null) {
  11. maxValue = element;
  12. } else {
  13. if (element.compareTo(maxValue) > 0) {
  14. maxValue = element;
  15. }
  16. }
  17. }
  18. return maxValue;
  19. }
  20. public static void main(String[] args) {
  21. List<Contact> contactList = new ArrayList<>();
  22.  
  23. contactList.add(new Contact("Juergen", "Falb"));
  24. contactList.add(new Contact("Michael", "Strommer"));
  25. contactList.add(new Contact("Jochen", "Hense"));
  26. System.out.println("Maximum: " + max(contactList).getFirstName());
  27.  
  28. for (Contact c: contactList) {
  29. System.out.println("Name: " + c.getFirstName() + " " + c.getLastName());
  30. }
  31.  
  32. List<Integer> integerList = new ArrayList<>();
  33. integerList.add(12);
  34. integerList.add(45);
  35. integerList.add(99);
  36. System.out.println("Maximum: " + max(integerList));
  37.  
  38. List<String> stringList = new ArrayList<>();
  39. stringList.add("Hello");
  40. stringList.add("World");
  41. stringList.add("FH");
  42. System.out.println("Letzter Buchstabe im Alphabet der Liste (Wort): " + max(stringList));
  43.  
  44. MyList<Integer> IntegerMyList = new MyList<>();
  45. IntegerMyList.add(45);
  46. IntegerMyList.add(366);
  47. IntegerMyList.add(35);
  48. for (int i = 0; i < IntegerMyList.getSize(); i++) {
  49. System.out.println("Element: " + i + IntegerMyList.get(i));
  50. }
  51.  
  52. Map<Integer, Contact> map = new HashMap<>();
  53. map.put(1001, contactList.get(0));
  54. map.put(1002, contactList.get(1));
  55. map.put(1003, contactList.get(2));
  56.  
  57. Contact c = map.get(1002);
  58. System.out.println("Mitarbeiter 1002: " + c.getFirstName());
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment