Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package f_sechs;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6.  
  7. /**
  8.  * Eine Beispielklasse für innere Klassen und Sortierung.
  9.  */
  10. public class Student implements Comparable<Student> {
  11.     protected int matNr;
  12.     protected int alter;
  13.  
  14.  
  15.     /*
  16.     Sortierung via Interface Comparable<T>
  17.      */
  18.  
  19.     public static ArrayList<Student> SortByNrViaInterface(ArrayList<Student> in)
  20.     {
  21.         // die Klasse implementiert die Methode compareTo und kann daher einfach sortiert werden.
  22.  
  23.         Collections.sort(in);
  24.  
  25.         return in;
  26.     }
  27.  
  28.     public int compareTo(Student other)
  29.     {
  30.         if(other == null)
  31.             return 1;
  32.  
  33.         return matNr - other.matNr;
  34.     }
  35.  
  36.     /*
  37.     Sortierung via statischer innerer Klasse
  38.      */
  39.  
  40.     private static class StaticInnerSortingClass implements Comparator<Student> {
  41.         @Override
  42.         public int compare(Student o1, Student o2) {
  43.             if(o1 == null)
  44.                 return o2 == null ? 0 : -1;
  45.  
  46.             return o1.getAlter() - o2.getAlter();
  47.         }
  48.     }
  49.  
  50.     public static ArrayList<Student> SortByAgeViaStaticInnerClass(ArrayList<Student> in)
  51.     {
  52.         StaticInnerSortingClass s = new StaticInnerSortingClass();
  53.  
  54.         in.sort(s);
  55.  
  56.         return in;
  57.     }
  58.  
  59.     /*
  60.     Sortierung via innerer Klasse
  61.      */
  62.  
  63.      private class InnerSortingClass implements Comparator<Student> {
  64.         @Override
  65.         public int compare(Student o1, Student o2) {
  66.             if(o1 == null)
  67.                 return o2 == null ? 0 : -1;
  68.  
  69.             return o1.getAlter() - o2.getAlter();
  70.         }
  71.     }
  72.  
  73.     // Note that this Method may not be static!
  74.     public ArrayList<Student> SortByAgeViaInnerClass(ArrayList<Student> in)
  75.     {
  76.         InnerSortingClass s = new InnerSortingClass();
  77.  
  78.         in.sort(s);
  79.  
  80.         return in;
  81.     }
  82.  
  83.     /*
  84.     Sortierung via lokaler Klasse
  85.      */
  86.  
  87.     public static ArrayList<Student> SortByAgeViaLocalClass(ArrayList<Student> in)
  88.     {
  89.         class LocalSortingClass implements Comparator<Student> {
  90.             @Override
  91.             public int compare(Student o1, Student o2) {
  92.                 if(o1 == null)
  93.                     return o2 == null ? 0 : -1;
  94.  
  95.                 return o1.getAlter() - o2.getAlter();
  96.             }
  97.         }
  98.  
  99.         LocalSortingClass s = new LocalSortingClass();
  100.  
  101.         in.sort(s);
  102.  
  103.         return in;
  104.     }
  105.  
  106.     /*
  107.     Sortierung via anonymer Klasse
  108.      */
  109.  
  110.     public static ArrayList<Student> SortByAgeViaAnonymousClass(ArrayList<Student> in)
  111.     {
  112.         Comparator<Student> s = new Comparator<Student>() {
  113.             @Override
  114.             public int compare(Student o1, Student o2) {
  115.                 if(o1 == null)
  116.                     return o2 == null ? 0 : -1;
  117.  
  118.                 return o1.getAlter() - o2.getAlter();
  119.             }
  120.         };
  121.  
  122.         in.sort(s);
  123.  
  124.         return in;
  125.     }
  126.  
  127.     /*
  128.     Sortierung via Lambda (nicht Klausurrelevant)
  129.      */
  130.     public static ArrayList<Student> SortByAgeViaLambda(ArrayList<Student> in)
  131.     {
  132.  
  133.         in.sort((o1, o2) -> {
  134.             if(o1 == null)
  135.                 return o2 == null ? 0 : -1;
  136.  
  137.             return o1.getAlter() - o2.getAlter();
  138.         });
  139.  
  140.         return in;
  141.     }
  142.  
  143.     /*
  144.     Java 8 Kurzschreibweise: Method Reference (nicht Klausurrelevant)
  145.      */
  146.     public static ArrayList<Student> SortByAgeViaMethodReference(ArrayList<Student> in)
  147.     {
  148.         Collections.sort(in, Comparator.comparing(Student::getAlter));
  149.  
  150.         return in;
  151.     }
  152.  
  153.  
  154.     /*
  155.     Standardmethoden wie ctor, getter, setter etc.
  156.     Hier ist nichts mehr interessantes.
  157.      */
  158.  
  159.     public static void main(String[] args) {
  160.         // ein paar zufällige Stundenten generieren
  161.         ArrayList<Student> ls = new ArrayList<>();
  162.         for (int i = 0; i < 20; i++)
  163.             ls.add(new Student((int)(Math.random() * 2000000 + 5000000), (int)(Math.random() * 20 + 18)));
  164.  
  165.         // irgendwie sortieren
  166.         ls.get(0).SortByAgeViaInnerClass(ls);
  167.  
  168.         // und ausgeben
  169.         for(Student s : ls)
  170.             System.out.println(s);
  171.     }
  172.  
  173.     // Standard Constructor
  174.     public Student(int matNr, int alter) {
  175.         this.matNr = matNr;
  176.         this.alter = alter;
  177.     }
  178.  
  179.     public String toString()
  180.     {
  181.         return "Der Student mit der Nr. " + matNr + " ist " + alter + " Jahre alt";
  182.     }
  183.  
  184.     public int getMatNr() {
  185.         return matNr;
  186.     }
  187.  
  188.     public void setMatNr(int matNr) {
  189.         this.matNr = matNr;
  190.     }
  191.  
  192.     public int getAlter() {
  193.         return alter;
  194.     }
  195.  
  196.     public void setAlter(int alter) {
  197.         this.alter = alter;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement