Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package genericsdemo;

public class Box<T> {
private T value;
public Box(T value) { this.value = value; }
public T get() { return value; }
}

public class Main {
public static void main(String[] args) {
Box<String> b = new Box<>("hello");
NumberBox<Integer> nb = new NumberBox<>(42);

Utils.print("Generic method!");
Wildcards.printBox(b);
Wildcards.sumNumbers(nb);

Pair<String, Integer> p = new Pair<>("age", 30);
SelfComparable<MyCmp> a = new MyCmp(10);
SelfComparable<MyCmp> c = new MyCmp(20);

System.out.println(a.compareTo(c));
}
}

class MyCmp extends SelfComparable<MyCmp> {
public MyCmp(int data) {
super(data);
}
}

public class NumberBox<T extends Number> {
private T num;
public NumberBox(T num) { this.num = num; }
public double doubleValue() { return num.doubleValue(); }
}

public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
}

public class SelfComparable<T extends SelfComparable<T>> implements Comparable<T> {
private int data;
public SelfComparable(int data) { this.data = data; }

@Override
public int compareTo(T other) {
return Integer.compare(this.data, other.data);
}
}

public class Wildcards {
public static void printBox(Box<?> box) {
System.out.println("Box contains: " + box.get());
}

public static void sumNumbers(NumberBox<? extends Number> nb) {
System.out.println("Double value: " + nb.doubleValue());
}
}

public class Utils {
public static <T> void print(T value) {
System.out.println(value);
}
}
79 changes: 79 additions & 0 deletions 1-0-java-basics/1-3-1-crazy-generics/1-3-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
class Box<T> {
private T value;

public void set(T value) {
this.value = value;
}

public T get() {
return value;
}
}

class NumericBox<T extends Number> {
private T number;

public void set(T number) {
this.number = number;
}

public T get() {
return number;
}
}

class Pair<K, V> {
private K key;
private V value;

public Pair(K key, V value) {
this.key = key;
this.value = value;
}

public K getKey() {
return key;
}

public V getValue() {
return value;
}
}

class ComparableBox<T extends Comparable<T>> {
private T value;

public ComparableBox(T value) {
this.value = value;
}

public boolean isGreaterThan(T other) {
return value.compareTo(other) > 0;
}
}

class Utils {
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
}

class WildcardDemo {
public static void showBox(Box<?> box) {
System.out.println("Box contains: " + box.get());
}
}

class BoundedWildcardDemo {
public static void sumNumbers(NumericBox<? extends Number> box) {
System.out.println("Number: " + box.get());
}
}

public class Main {
public static void main(String[] args) {
}
}
56 changes: 43 additions & 13 deletions 5-0-functional-programming/5-0-1-lambda-functions-map/README.MD
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Lambda Functions Map
Start learning functional programming in Java by writing simple math functions using Lambdas 💪
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntUnaryOperator;

### Objectives
public class LambdaFunctionsMap {

* implement **abs** (absolute) function using lambda ✅
* implement **sgn** (signum) function using lambda ✅
* implement **increment** function using lambda ✅
* implement **decrement** function using lambda ✅
* implement **square** function using lambda ✅
* add all those functions to the function map ✅
private final Map<String, IntUnaryOperator> functionMap = new HashMap<>();

---
public LambdaFunctionsMap() {

IntUnaryOperator abs = x -> x >= 0 ? x : -x;

#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction)

IntUnaryOperator sgn = x -> x > 0 ? 1 : (x < 0 ? -1 : 0);

##
<div align="center"><img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/animation/GitHub%20Star_3.gif" height=50/></div>

IntUnaryOperator increment = x -> x + 1;


IntUnaryOperator decrement = x -> x - 1;


IntUnaryOperator square = x -> x * x;


functionMap.put("abs", abs);
functionMap.put("sgn", sgn);
functionMap.put("increment", increment);
functionMap.put("decrement", decrement);
functionMap.put("square", square);
}


public Map<String, IntUnaryOperator> getFunctionMap() {
return functionMap;
}


public static void main(String[] args) {
LambdaFunctionsMap lfm = new LambdaFunctionsMap();

System.out.println("abs(-5) = " + lfm.getFunctionMap().get("abs").applyAsInt(-5));
System.out.println("sgn(-5) = " + lfm.getFunctionMap().get("sgn").applyAsInt(-5));
System.out.println("increment(7) = " + lfm.getFunctionMap().get("increment").applyAsInt(7));
System.out.println("decrement(7) = " + lfm.getFunctionMap().get("decrement").applyAsInt(7));
System.out.println("square(4) = " + lfm.getFunctionMap().get("square").applyAsInt(4));
}
}
25 changes: 11 additions & 14 deletions 5-0-functional-programming/5-0-2-stream-sum-of-squares/README.MD
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# <img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/image/logo_transparent_background.png" height=50/>Stream sum of squares
Learn functional programming by refactoring a piece of code using Stream API 💪

### Objectives
* **refactor imperative-style code** with for loop using Stream API ✅
* **create a `IntStream`** based on given the first, and the last values ✅
* **transform each element** of the stream into its square value ✅
* **calculate a sum** of all elements in the stream ✅

---
#### 🆕 First time here? – [See Introduction](https://github.com/bobocode-projects/java-fundamentals-exercises/tree/main/0-0-intro#introduction)

##
<div align="center"><img src="https://raw.githubusercontent.com/bobocode-projects/resources/master/animation/GitHub%20Star_3.gif" height=50/></div>
public static int sumOfSquares(int start, int end) {
return IntStream.rangeClosed(start, end)
.map(x -> x * x)
.sum();
}
public static void main(String[] args) {
int start = 1;
int end = 5;
int result = sumOfSquares(start, end);
System.out.println(result);
}