mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-02 22:10:55 -04:00
Updated java up to lambdas
This commit is contained in:
@@ -17,6 +17,7 @@ export default {
|
||||
"/aws/": require("../aws/sidebar.json"),
|
||||
"/go": require("../go/sidebar.json"),
|
||||
"/react": require("../react/sidebar.json"),
|
||||
"/vue": require("../vue/sidebar.json"),
|
||||
"/java": require("../java/sidebar.json"),
|
||||
"/": [
|
||||
{
|
||||
|
||||
@@ -7,3 +7,67 @@ Generics allow you to create a class that can hold many different kinds of data
|
||||
```java
|
||||
|
||||
```
|
||||
|
||||
## Nested Classes
|
||||
|
||||
You can also have classes defined within another classes. If the nested class is static, then it can be instantiated from the class itself just like any other static methods. Otherwise, you need an instance of the class to access the nested class.
|
||||
|
||||
### Static
|
||||
|
||||
```java
|
||||
public class Employee {
|
||||
public static class EmployeeComparator <T extends Employee> implements Comparator<Employee> {
|
||||
private String sortType;
|
||||
|
||||
public EmployeeComparator(String sortType) {
|
||||
this.sortType = sortType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Employee o1, Employee o2) {
|
||||
if (sortType == "yearStarted") {
|
||||
// sort by year started
|
||||
}
|
||||
// able to access private variables in Employee, and vice versa
|
||||
return o1.name.compareTo(o2.name);
|
||||
}
|
||||
}
|
||||
|
||||
private int employeeId;
|
||||
private String name;
|
||||
private int yearStarted;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
List<Employee> employees = new ArrayList<>();
|
||||
|
||||
employees.sort(new Employee.EmployeeComparator<>());
|
||||
}
|
||||
```
|
||||
|
||||
### Non-static
|
||||
|
||||
```java
|
||||
public class StoreEmployee extends Employee {
|
||||
private String store;
|
||||
|
||||
public class StoreComparator <T extends Employee> implements Comparator<StoreEmployee> {
|
||||
public int compare(StoreEmployee o1, StoreEmployee o2) {
|
||||
int result = o1.store.compareTo(o2.store);
|
||||
if (result == 0) {
|
||||
return new Employee.EmployeeComparator<>("yearStarted").compare(o1, o2);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
List<StoreEmployee> storeEmployees = new ArrayList<>();
|
||||
|
||||
var genericEmployee = new StoreEmployee();
|
||||
var comparator = genericEmployee.new StoreComparator<>();
|
||||
var comparator = new StoreEmployee().new StoreComparator<>();
|
||||
storeEmployees.sort(comparator);
|
||||
}
|
||||
```
|
||||
|
||||
0
docs/java/basics/collections.md
Normal file
0
docs/java/basics/collections.md
Normal file
28
docs/java/basics/controlflow.md
Normal file
28
docs/java/basics/controlflow.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# Control Flow
|
||||
|
||||
## Loops
|
||||
|
||||
### For Loop
|
||||
|
||||
```java
|
||||
for(int i = 0; i < 10; i++) {
|
||||
// do something
|
||||
}
|
||||
|
||||
List<String> myStrings = new ArrayList<>(List.of("hello", "world"));
|
||||
for (String s : myStrings) {
|
||||
// do something
|
||||
}
|
||||
for (var s : myStrings) {
|
||||
// do something
|
||||
}
|
||||
```
|
||||
|
||||
#### Interable Interfaces
|
||||
|
||||
Many collections implement the `Iterable` interface, which has a method for each.
|
||||
|
||||
```java
|
||||
List<String> words = new ArrayList<>(List.of("hello", "world"));
|
||||
words.forEach((s) -> System.out.println(s));
|
||||
```
|
||||
94
docs/java/basics/lambda.md
Normal file
94
docs/java/basics/lambda.md
Normal file
@@ -0,0 +1,94 @@
|
||||
# Lambdas
|
||||
|
||||
In order to pass a lambda function as an argument, it needs to be passed to a place that expects an interface that is a **functional interface**, or it only has one abstract method. This way the compiler can be sure that the function passed is the correct one for that interface.
|
||||
|
||||
The lambda function can also use variables in the calling scope, as long as the variables are final or effectively final.
|
||||
|
||||
```java
|
||||
@FunctionalInterface
|
||||
public interface Operation<T> {
|
||||
T operate(T a, T b);
|
||||
}
|
||||
|
||||
public static <T> T calculator(Operation<T> function, T v1, T v2) {
|
||||
T result = function.operate(v1, v2);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
List<Person> people = new ArrayList<>();
|
||||
people.sort((o1, o2) -> o1.lastName.compareTo(o2.lastName));
|
||||
|
||||
int result = calculator((a,b) -> a + b, 5, 2);
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Common Functional Interfaces
|
||||
|
||||
These are found in the `java.util.function` package. There are four basic types of functional interfaces.
|
||||
|
||||
- Consumer -> `void accept(T t, U u?)` - Executes code without returning data (BiConsumer takes 2 args)
|
||||
- Function -> `R apply(T t, U u?)` - Return a result of an operation or function
|
||||
- Predicate -> `boolean test(T t, U u?)` - Test if a condition is true or false
|
||||
- Supplier -> `T get()` - Return an instance of something
|
||||
|
||||
### Consumer
|
||||
|
||||
A Consumer interface takes one or two arguments and performs some operation on them without returning anything.
|
||||
|
||||
```java
|
||||
public static <T> void processPoint(T t1, T t2, BiConsumer<T,T> consumer) {
|
||||
consumer.accept(t1, t2);
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
BiConsumer<Double, Double> p1 = (lat, lng) -> System.out.printf("[lat:%.3f, long:%.3f]%n", lat, lng)
|
||||
processPoint(30.245, 50.343, p1);
|
||||
}
|
||||
```
|
||||
|
||||
### Predicate
|
||||
|
||||
```java
|
||||
public static void main() {
|
||||
List<String> names = new ArrayList<>(List.of("Harry", "Ron", "Hermoine"))
|
||||
names.removeIf(s -> s.startsWith("H"));
|
||||
}
|
||||
```
|
||||
|
||||
### Function
|
||||
|
||||
Similar to the Operation interface example above, this interface takes a lambda function and two values, performing some operation on the two with the `apply` method.
|
||||
|
||||
#### Operator
|
||||
|
||||
Operators have the same return and argument types, so only one type `T`. There are both `UnaryOperators` and `BiOperators`.
|
||||
|
||||
```java
|
||||
public static <T> T calculator(BinaryOperator<T> function, T v1, T v2) {
|
||||
T result = function.apply(v1, v2);
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
#### Function
|
||||
|
||||
Functions can have different types for the return values and all of its arguments. There are both `Functions` and `BiFunctions`.
|
||||
|
||||
## Method References
|
||||
|
||||
Method references allow you to pass a static method instead of a lambda function, and java can infer how to pass the arguments and what the return value should be. You pass a method reference by using the class name, two colons, and then the function name.
|
||||
|
||||
```java
|
||||
public static <T> T calculator(BinaryOperator<T> function, T v1, T v2) {
|
||||
T result = function.apply(v1, v2);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main() {
|
||||
calculator(Integer::sum, 2, 5);
|
||||
calculator(Double::sum, 7.5, 2.5);
|
||||
}
|
||||
```
|
||||
@@ -4,8 +4,11 @@
|
||||
"items": [
|
||||
{ "text": "Introduction", "link": "/java/" },
|
||||
{ "text": "Types", "link": "/java/basics/types" },
|
||||
{ "text": "Control Flow", "link": "/java/basics/controlflow" },
|
||||
{ "text": "Classes", "link": "/java/basics/classes" },
|
||||
{ "text": "Interfaces", "link": "/java/basics/interfaces" }
|
||||
{ "text": "Interfaces", "link": "/java/basics/interfaces" },
|
||||
{ "text": "Collections", "link": "/java/basics/collections" },
|
||||
{ "text": "Lambdas", "link": "/java/basics/lambda" }
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
7
docs/vue/index.md
Normal file
7
docs/vue/index.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Vue
|
||||
|
||||
## Creating a New Project
|
||||
|
||||
```bash
|
||||
npm create vue@latest
|
||||
```
|
||||
8
docs/vue/sidebar.json
Normal file
8
docs/vue/sidebar.json
Normal file
@@ -0,0 +1,8 @@
|
||||
[
|
||||
{
|
||||
"text": "Vue Basics",
|
||||
"items": [
|
||||
{ "text": "Introduction", "link": "/vue/" }
|
||||
]
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user