Skip to content

Commit

Permalink
Last lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
alvisespano committed Jan 12, 2025
1 parent ddf19f2 commit a0b9877
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
7 changes: 5 additions & 2 deletions 2024-25/LezioniJava24-25/src/misc/Functional.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

public class Functional {



public interface Function<A, B> {
B apply(A x);
}
Expand Down Expand Up @@ -102,12 +104,13 @@ public Integer apply(String x) {

{
List<Integer> l = List.of(-56, 345, 11, 0, -456, 23);
final int k = 0;
Collection<Boolean> r = map(l, new Function<Integer, Boolean>() {
public Boolean apply(Integer x) {
return x > 0;
return x > k;
}
});
Collection<Boolean> r2 = map(l, (x) -> x > 0);
Collection<Boolean> r2 = map(l, (x) -> x > k);
}

{
Expand Down
47 changes: 47 additions & 0 deletions 2024-25/LezioniJava24-25/src/misc/Sorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package misc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorting {


public static <T extends Comparable<? super T>> void sort(List<T> list) { ... }


/*public interface Comparator<T> {
int compare(T o1, T o2);
}*/

public static void main(String[] args) {
{
List<Zoo.Dog> l = new ArrayList<>();
l.add(new Zoo.Dog(20, "mio"));
l.add(new Zoo.Dog(29, "mio"));
l.add(new Zoo.Dog(28, "mio"));
sort(l);




Collections.sort(l, new Comparator<>() {
@Override
public int compare(Zoo.Animal a, Zoo.Animal b) {
return a.weight - b.weight;
}
});
}
{
List<String> l = List.of("ciao", "pippo", "gigio", "byebye");
Collections.sort(l, new Comparator<>() {
public int compare(String a, String b) {
return b.length() - a.length();
}
});
}
}


}
16 changes: 15 additions & 1 deletion 2024-25/LezioniJava24-25/src/misc/Zoo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Zoo {

public static class Animal {
public static class Animal implements Comparable<Animal> {

protected int weight;

Expand All @@ -13,6 +13,11 @@ public Animal(int weight) {
public void eat(Animal a) {
this.weight += a.weight;
}

@Override
public int compareTo(Animal o) {
return this.weight - o.weight;
}
}

public static class Dog extends Animal {
Expand All @@ -23,6 +28,15 @@ public Dog(int weight, String owner) {
this.owner = owner;
}

@Override
public int compareTo(Animal o) {
if (o instanceof Dog) {
Dog d = (Dog) o;
...
}
else return super.compareTo(o);
}

public void bark() {
System.out.println("BAUUU!");
}
Expand Down

0 comments on commit a0b9877

Please sign in to comment.