Skip to content

Commit

Permalink
11b - generics
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Benov committed Nov 28, 2023
1 parent 82af3f7 commit c815881
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions materials/2023-2024/11b/2023-11-27-generics/generics/generics.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
31 changes: 31 additions & 0 deletions materials/2023-2024/11b/2023-11-27-generics/generics/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
// list.add(Integer.getInteger("123"));
list.add(123);
// list.add(true);

MyList<Integer> list2 = new MyList<>();
// list2.add(123);

MyList<Integer> list3 = MyList.<Integer>fromArray(
new Integer[]{1, 2, 3}
);

list3.<Object>foo();

list2.sort((o1, o2) -> o1.hashCode() - o2.hashCode());
list2.sort((o1, o2) -> o1 - o2);

list3.sort(Comparator.comparingInt(Object::hashCode));
list3.sort(Comparator.comparingInt(o -> o));

list3.sort();

MyList<Main> list4 = new MyList<Main>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.io.Serializable;
import java.util.Comparator;

public class MyList<E extends Comparable & Serializable> {
private class Node {
E value;
Node next = null;

public Node(E value) {
this.value = value;
}
};

private Node head = null;

public void add(E e) {
if(head == null) {
head = new Node(e);
return;
}

// ...
}

public static <K extends Comparable & Serializable> MyList<K> fromArray(K[] arr) {
MyList<K> l = new MyList<>();
// ...
// l.add(1);
l.add(arr[0]);

return l;
}

public <O> void foo() {}

public void sort(Comparator<E> comp) {
E o1 = head.value;
E o2 = head.next.value;
// for( ... )
int result = comp.compare(o1, o2);
// if(result < 0)
// swap();
}

public void sort() {
E o1 = head.value;
E o2 = head.next.value;
// for( ... )
// int result = ((Comparable)o1).compareTo(o2);
int result = o1.compareTo(o2);
// if(result < 0)
// swap();
}
}

0 comments on commit c815881

Please sign in to comment.