diff --git a/docs/.vitepress/config.js b/docs/.vitepress/config.js index 51b8d46..f135428 100644 --- a/docs/.vitepress/config.js +++ b/docs/.vitepress/config.js @@ -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"), "/": [ { diff --git a/docs/java/basics/classes.md b/docs/java/basics/classes.md index 16fb492..00b2660 100644 --- a/docs/java/basics/classes.md +++ b/docs/java/basics/classes.md @@ -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 implements Comparator { + 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 employees = new ArrayList<>(); + + employees.sort(new Employee.EmployeeComparator<>()); +} +``` + +### Non-static + +```java +public class StoreEmployee extends Employee { + private String store; + + public class StoreComparator implements Comparator { + 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 storeEmployees = new ArrayList<>(); + + var genericEmployee = new StoreEmployee(); + var comparator = genericEmployee.new StoreComparator<>(); + var comparator = new StoreEmployee().new StoreComparator<>(); + storeEmployees.sort(comparator); +} +``` diff --git a/docs/java/basics/collections.md b/docs/java/basics/collections.md new file mode 100644 index 0000000..e69de29 diff --git a/docs/java/basics/controlflow.md b/docs/java/basics/controlflow.md new file mode 100644 index 0000000..d5c0a5f --- /dev/null +++ b/docs/java/basics/controlflow.md @@ -0,0 +1,28 @@ +# Control Flow + +## Loops + +### For Loop + +```java +for(int i = 0; i < 10; i++) { + // do something +} + +List 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 words = new ArrayList<>(List.of("hello", "world")); +words.forEach((s) -> System.out.println(s)); +``` diff --git a/docs/java/basics/lambda.md b/docs/java/basics/lambda.md new file mode 100644 index 0000000..9f883ed --- /dev/null +++ b/docs/java/basics/lambda.md @@ -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 operate(T a, T b); +} + +public static T calculator(Operation function, T v1, T v2) { + T result = function.operate(v1, v2); + return result; +} + +public static void main() { + List 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 void processPoint(T t1, T t2, BiConsumer consumer) { + consumer.accept(t1, t2); +} + +public static void main() { + BiConsumer 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 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 calculator(BinaryOperator 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 calculator(BinaryOperator 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); +} +``` diff --git a/docs/java/sidebar.json b/docs/java/sidebar.json index e5b3e84..34b65bd 100644 --- a/docs/java/sidebar.json +++ b/docs/java/sidebar.json @@ -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" } ] } ] diff --git a/docs/vue/index.md b/docs/vue/index.md new file mode 100644 index 0000000..ac759b8 --- /dev/null +++ b/docs/vue/index.md @@ -0,0 +1,7 @@ +# Vue + +## Creating a New Project + +```bash +npm create vue@latest +``` \ No newline at end of file diff --git a/docs/vue/sidebar.json b/docs/vue/sidebar.json new file mode 100644 index 0000000..8515178 --- /dev/null +++ b/docs/vue/sidebar.json @@ -0,0 +1,8 @@ +[ + { + "text": "Vue Basics", + "items": [ + { "text": "Introduction", "link": "/vue/" } + ] + } + ] \ No newline at end of file