Java 8 版本合并
This commit is contained in:
@@ -18,4 +18,7 @@
|
||||
- [Interface With Default Methods vs Abstract Class](https://www.baeldung.com/java-interface-default-method-vs-abstract-class)
|
||||
- [Convert Between Byte Array and UUID in Java](https://www.baeldung.com/java-byte-array-to-uuid)
|
||||
- [Create a Simple “Rock-Paper-Scissors” Game in Java](https://www.baeldung.com/java-rock-paper-scissors)
|
||||
- [VarArgs vs Array Input Parameters in Java](https://www.baeldung.com/varargs-vs-array)
|
||||
- [Lambda Expression vs. Anonymous Inner Class](https://www.baeldung.com/java-lambdas-vs-anonymous-class)
|
||||
- [Java Helper vs. Utility Classes](https://www.baeldung.com/java-helper-vs-utility-classes)
|
||||
- [Java 8 开始新增的 Optional 类](https://www.ossez.com/t/java-8-optional/13964)
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.ossez.anonymousclass;
|
||||
|
||||
public class AnonymousClassExample{
|
||||
|
||||
public static void main(String[] args){
|
||||
Thread t1 = new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("Thread: "+Thread.currentThread().getName()+" started");
|
||||
}
|
||||
});
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.ossez.argsVsvarargs;
|
||||
|
||||
public class StringArrayAndVarargs {
|
||||
public static void capitalizeNames(String[] args) {
|
||||
for(int i = 0; i < args.length; i++){
|
||||
args[i] = args[i].toUpperCase();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String[] firstLetterOfWords(String... args) {
|
||||
String[] firstLetter = new String[args.length];
|
||||
for(int i = 0; i < args.length; i++){
|
||||
firstLetter[i] = String.valueOf(args[i].charAt(0));
|
||||
}
|
||||
return firstLetter;
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.ossez.helpervsutilityclasses;
|
||||
|
||||
class MyHelperClass {
|
||||
public double discount;
|
||||
public MyHelperClass(double discount) {
|
||||
if (discount > 0 && discount < 1) {
|
||||
this.discount = discount;
|
||||
}
|
||||
}
|
||||
public double discountedPrice(double price) {
|
||||
return price - (price * discount);
|
||||
}
|
||||
|
||||
public static int getMaxNumber(int[] numbers) {
|
||||
if (numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Ensure array is not empty");
|
||||
}
|
||||
int max = numbers[0];
|
||||
for (int i = 1; i < numbers.length; i++) {
|
||||
if (numbers[i] > max) {
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int getMinNumber(int[] numbers) {
|
||||
if (numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Ensure array is not empty");
|
||||
}
|
||||
int min = numbers[0];
|
||||
for (int i = 1; i < numbers.length; i++) {
|
||||
if (numbers[i] < min) {
|
||||
min = numbers[i];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.ossez.helpervsutilityclasses;
|
||||
|
||||
public final class MyUtilityClass {
|
||||
|
||||
private MyUtilityClass(){}
|
||||
|
||||
public static String returnUpperCase(String stringInput) {
|
||||
return stringInput.toUpperCase();
|
||||
}
|
||||
|
||||
public static String returnLowerCase(String stringInput) {
|
||||
return stringInput.toLowerCase();
|
||||
}
|
||||
|
||||
public static String[] splitStringInput(String stringInput, String delimiter) {
|
||||
return stringInput.split(delimiter);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.ossez.lambdaexpression;
|
||||
|
||||
public class LambdaExpressionExample{
|
||||
|
||||
public static void main(String[] args){
|
||||
Thread t1 = new Thread(()->System.out.println("Thread: "+Thread.currentThread().getName()+" started"));
|
||||
t1.start();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Runs all available formatter
|
||||
*
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.ossez.argsVsvarargs;
|
||||
|
||||
import static com.ossez.argsVsvarargs.StringArrayAndVarargs.capitalizeNames;
|
||||
import static com.ossez.argsVsvarargs.StringArrayAndVarargs.firstLetterOfWords;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StringArrayAndVarargsUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCheckingArgumentClassName_thenNameShouldBeStringArray() {
|
||||
String[] names = {"john", "ade", "kofi", "imo"};
|
||||
assertNotNull(names);
|
||||
assertEquals("java.lang.String[]", names.getClass().getTypeName());
|
||||
capitalizeNames(names);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCheckingReturnedObjectClass_thenClassShouldBeStringArray() {
|
||||
assertEquals(String[].class, firstLetterOfWords("football", "basketball", "volleyball").getClass());
|
||||
assertEquals(3, firstLetterOfWords("football", "basketball", "volleyball").length);
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -4,7 +4,6 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -71,7 +70,7 @@ public class BiFunctionalInterfacesUnitTest {
|
||||
List<String> list1 = Arrays.asList("a", "b", "c");
|
||||
List<Integer> list2 = Arrays.asList(1, 2, 3);
|
||||
|
||||
List<String> result = listCombiner(list1, list2, (a, b) -> a + b);
|
||||
List<String> result = listCombiner(list1, list2, (letter, number) -> letter + number);
|
||||
|
||||
assertThat(result).containsExactly("a1", "b2", "c3");
|
||||
}
|
||||
@@ -92,7 +91,7 @@ public class BiFunctionalInterfacesUnitTest {
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
// algorithm to determine if the value in list1 > value in list 2
|
||||
List<Boolean> result = listCombiner(list1, list2, (a, b) -> a > b);
|
||||
List<Boolean> result = listCombiner(list1, list2, (doubleNumber, floatNumber) -> doubleNumber > floatNumber);
|
||||
|
||||
assertThat(result).containsExactly(true, true, false);
|
||||
}
|
||||
@@ -117,7 +116,7 @@ public class BiFunctionalInterfacesUnitTest {
|
||||
List<Float> list1 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
List<Float> list2 = Arrays.asList(0.1f, 0.2f, 4f);
|
||||
|
||||
List<Boolean> result = listCombiner(list1, list2, (a, b) -> a.equals(b));
|
||||
List<Boolean> result = listCombiner(list1, list2, (float1, float2) -> float1.equals(float2));
|
||||
|
||||
assertThat(result).containsExactly(true, true, true);
|
||||
}
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.ossez.helpervsutilityclasses;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MyHelperClassUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCreatingHelperObject_thenHelperObjectShouldBeCreated() {
|
||||
MyHelperClass myHelperClassObject = new MyHelperClass(0.10);
|
||||
int[] numberArray = {15, 23, 66, 3, 51, 79};
|
||||
|
||||
assertNotNull(myHelperClassObject);
|
||||
|
||||
assertEquals(90, myHelperClassObject.discountedPrice(100.00));
|
||||
assertEquals( 79, MyHelperClass.getMaxNumber(numberArray));
|
||||
assertEquals(3, MyHelperClass.getMinNumber(numberArray));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.ossez.helpervsutilityclasses;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MyUtilityClassUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingUtilityMethods_thenAccessMethodsViaClassName(){
|
||||
assertEquals( "INIUBONG", MyUtilityClass.returnUpperCase("iniubong"));
|
||||
assertEquals("accra", MyUtilityClass.returnLowerCase("AcCrA"));
|
||||
}
|
||||
|
||||
}
|
||||
+2
@@ -6,6 +6,8 @@ import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.ossez.localization.ICUFormat;
|
||||
|
||||
public class ICUFormatUnitTest {
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user