Willkommen

Übung Prog 2

22/23.11.23

Was machen wir heute?

  • Generelle Fragen?
  • Itterationen
    • for int i
      • lists/sets
      • maps
    • for each
      • lists/sets
      • maps
    • streams
      • lists/sets
      • maps
  • Abgaben / Eigenarbeit

Generelle Fragen?

  • Git-Probleme
  • Sachen die wir heute zusätzlich besprechen sollen
  • Fragen zur Abgabe 2 oder 1?

for int i

Lists

List<String> myList = List.of("My", "spectacular", "List");
for (int i = 0; i < myList.size(); i++) {
  System.out.println(String.format("An Stelle %s befindet sich Wert %s", i + 1, myList.get(i)));
 }
An Stelle 1 befindet sich Wert My
An Stelle 2 befindet sich Wert spectacular
An Stelle 3 befindet sich Wert List
List<String> myList = List.of("My", "spectacular", "List");
for (int i = 0; i < myList.size(); i++) {
  String newValue = myList.get(i) + "Blub";
  myList.remove(myList.get(i));
  myList.add(i,newValue);
}

for int i

Sets

Geht nicht!

Set<String> mySet = new HashSet<>(List.of("My", "spectacular", "Set"));
for (int i = 0; i < mySet.size(); i++) {
  System.out.println(String.format("An Stelle %s befindet sich Wert %s", i + 1, mySet.get(i).substring(0,1);));
}
public static void main(String[] args) {
  Set<String> mySet = new HashSet<>(List.of("My", "spectacular", "Set"));
  Iterator<String> it = mySet.iterator();
  int count = 1;
  while(it.hasNext()){
    System.out.println(String.format("An Stelle %s befindet sich Wert %s", count++, it.next()));
  }
}
An Stelle 1 befindet sich Wert spectacular
An Stelle 2 befindet sich Wert Set
An Stelle 3 befindet sich Wert My

for int i

Maps

Map<String, String> myMap = new HashMap<>() {{
  put("key1", "value1");
  put("key2", "value2");
  put("key3", "value3");
}};
for (int i = 0; i < myMap.size(); i++){
  Iterator<String> it = myMap.keySet().iterator();
  int count = 1;
  while (it.hasNext()){
    String key = it.next();
    System.out.println(String.format("An Stelle %s befindet sich Wert %s", key,myMap.get(key)));
  }
}

for each

Lists / Sets

List<String> myList = List.of("My", "spectacular", "List");
int count = 1
for (String s: myList) {
  System.out.println(String.format("An Stelle %s befindet sich Wert %s", count++, s));
 }
An Stelle 1 befindet sich Wert My
An Stelle 2 befindet sich Wert spectacular
An Stelle 3 befindet sich Wert List
List<String> myList = List.of("My", "spectacular", "List");
List<String> yourList = List.of("your", "top", "otherList");
for (String my : myList) {
  for (String your : yourList){
    System.out.println(my.substring(0,1) + " -> " + your);
  } 
}

for each

Maps

Map<String, String> myMap = new HashMap<>() {{
      put("key1", "value1");
      put("key2", "value2");
      put("key3", "value3");
}};
for (Map.Entry entry : myMap.entrySet()) {
  System.out.println(String.format("An Stelle %s befindet sich Wert %s", entry.getKey(), entry.getValue()));
}

Streams

  • Ein wenig langsamer
  • nicht allzweckwaffe
  • ermöglicht komplexe opperationen auf Listen/Sets
  • Gut für Nebenläufigkeit geeignet
  • Lambda
SameButDifferentMeme

Streams

Lists / Sets

List<String> myList = List.of("My", "spectacular", "List");
myList.stream().forEach(s -> System.out.println(s)); // git alle inhalte aus
myList.stream().map(e -> e + "BLUB").forEach(System.out::println); // verändert alle Inhalte
List<String> mapedList = myList.stream().map(String::toLowerCase).collect(Collectors.toList());
mapedList.forEach(System.out::println);
My
spectacular
List
MyBLUB
spectacularBLUB
ListBLUB
my
spectacular
list

Beispiel

private static Set<String> getHttpsApisFromFeatureFiles() {
    List<File> featureFiles = getFeatureFiles(new File(FEATURE_PATH)); // read multiple files
    List<GherkinDocument> features = featureFiles.stream() // Files transformed to
        .map(TestsuiteInitializer::transformToGherkin).toList();  // Java-Objects
    return features.stream() // open stream
        .map(GherkinDocument::getFeature) // get all Optional<Features>
        .filter(Optional::isPresent) // filter all empty optionals
        .map(Optional::get) // transormed from optional to Feature
        .map(Feature::getChildren) // get childes from Feature
        .flatMap(Collection::stream) // got list of list -> so tranform all to on list
        .map(FeatureChild::getScenario) // transormed from FeatureChild to scenario
        .filter(Optional::isPresent) // filter all empty optionals
        .map(Optional::get) // transormed from optional to scenario
        .map(Scenario::getExamples) // transormed from scenario to example
        .flatMap(Collection::stream) // got list of list -> so tranform all to on list
        .map(Examples::getTableBody) /// transormed from example to table
        .flatMap(Collection::stream)// got list of list -> so tranform all to on list
        .map(TableRow::getCells) // transormed from table row to tableCell
        .flatMap(Collection::stream)// got list of list -> so tranform all to on list
        .map(TableCell::getValue) // get only value from tablecell
        .filter(e -> e.startsWith("https://")) // filter for values that starts with ...
        .map(e -> URI.create(e).getHost()) // transormed from string to URL and get host
        .filter(Objects::nonNull) // filter all empty optionals
        .collect(Collectors.toSet()); // collect all to Set<String>
  }

Beispiel

File das im oberen Beispiel geparsed wurde (FEATURE FILE)

# language: de
@File:FeatureFile_00 
Funktionalität: 00. Hier könnte Ihre Beschreibung stehen //(Feature)

  Szenariogrundriss: 00.01 Ein Student fragt seine Noten ab // Scenario
    Angenommen Student "A" ruft die Webseite <URL1> auf // URL1 cames from Table 
    Dann       sieht Student "A" eine Einloggmaske
    Dann       ....

    Beispiele: // Examples
    | URL1 | // table
    | https://lsf.htw-berlin.de //TableCell | // TableRow <- This is the result
    | https://lsf.htw-Bonn.de //TableCell | // TableRow<- This is the result

  Szenariogrundriss: 00.02 Ein Student fragt seinen Stundenplan ab // Scenario
    Angenommen Student "A" ruft die Webseite <URL1> auf // URL1 cames from Table 
    Dann       sieht Student "A" eine Einloggmaske
    Dann       ....

    Beispiele: // Examples
    | URL1 | // table
    | https://lsf.htw-berlin.de //TableCell | // TableRow <- This is the result
    | https://lsf.htw-Bonn.de //TableCell | // TableRow<- This is the result