forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
112 changed files
with
1,422 additions
and
453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
core-groovy/src/test/groovy/com/baeldung/date/DateTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.baeldung.groovy.sql | ||
|
||
import static org.junit.Assert.* | ||
import java.util.Calendar.* | ||
import java.time.LocalDate | ||
import java.text.SimpleDateFormat | ||
import org.junit.Test | ||
|
||
|
||
class DateTest { | ||
|
||
def dateStr = "2019-02-28" | ||
def pattern = "yyyy-MM-dd" | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDate() { | ||
def dateFormat = new SimpleDateFormat(pattern) | ||
def date = dateFormat.parse(dateStr) | ||
|
||
println(" String to Date with DateFormatter : " + date) | ||
|
||
def cal = new GregorianCalendar(); | ||
cal.setTime(date); | ||
|
||
assertEquals(cal.get(Calendar.YEAR),2019) | ||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28) | ||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY) | ||
} | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertWithDateUtilsExtension() { | ||
|
||
def date = Date.parse(pattern, dateStr) | ||
|
||
println(" String to Date with Date.parse : " + date) | ||
|
||
def cal = new GregorianCalendar(); | ||
cal.setTime(date); | ||
|
||
assertEquals(cal.get(Calendar.YEAR),2019) | ||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28) | ||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY) | ||
} | ||
|
||
@Test | ||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDateWithLocalDate() { | ||
def date = LocalDate.parse(dateStr, pattern) | ||
|
||
println(" String to Date with LocalDate : " + date) | ||
|
||
assertEquals(date.getYear(),2019) | ||
assertEquals(date.getMonth(),java.time.Month.FEBRUARY) | ||
assertEquals(date.getDayOfMonth(),28) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
core-java-collections/src/main/java/com/baeldung/java/sort/CollectionsSortCompare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.baeldung.java.sort; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CollectionsSortCompare { | ||
|
||
public static void main(String[] args) { | ||
sortPrimitives(); | ||
sortReferenceType(); | ||
sortCollection(); | ||
} | ||
|
||
private static void sortReferenceType() { | ||
Integer[] numbers = {5, 22, 10, 0}; | ||
Arrays.sort(numbers); | ||
System.out.println(Arrays.toString(numbers)); | ||
} | ||
|
||
private static void sortCollection() { | ||
List<Integer> numbersList = new ArrayList<>(); | ||
numbersList.add(5); | ||
numbersList.add(22); | ||
numbersList.add(10); | ||
numbersList.add(0); | ||
|
||
Collections.sort(numbersList); | ||
|
||
numbersList.forEach(System.out::print); | ||
} | ||
|
||
private static void sortPrimitives() { | ||
int[] numbers = {5, 22, 10, 0}; | ||
Arrays.sort(numbers); | ||
System.out.println(Arrays.toString(numbers)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core-java-collections/src/main/java/com/baeldung/performance/ArraySortBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.baeldung.performance; | ||
|
||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.options.Options; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.SingleShotTime) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@Measurement(batchSize = 100000, iterations = 10) | ||
@Warmup(batchSize = 100000, iterations = 10) | ||
public class ArraySortBenchmark { | ||
|
||
@State(Scope.Thread) | ||
public static class Initialize { | ||
Integer[] numbers = {5, 22, 10, 0}; | ||
int[] primitives = {5, 22, 10, 0}; | ||
} | ||
|
||
@Benchmark | ||
public Integer[] benchmarkArraysIntegerSort(ArraySortBenchmark.Initialize state) { | ||
Arrays.sort(state.numbers); | ||
return state.numbers; | ||
} | ||
|
||
@Benchmark | ||
public int[] benchmarkArraysIntSort(ArraySortBenchmark.Initialize state) { | ||
Arrays.sort(state.primitives); | ||
return state.primitives; | ||
} | ||
|
||
|
||
public static void main(String[] args) throws Exception { | ||
Options options = new OptionsBuilder() | ||
.include(ArraySortBenchmark.class.getSimpleName()).threads(1) | ||
.forks(1).shouldFailOnError(true) | ||
.shouldDoGC(true) | ||
.jvmArgs("-server").build(); | ||
new Runner(options).run(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
core-java/src/main/java/com/baeldung/urlconnection/PostJSONWithHttpURLConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.baeldung.urlconnection; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class PostJSONWithHttpURLConnection { | ||
|
||
public static void main (String []args) throws IOException{ | ||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body | ||
URL url = new URL ("https://reqres.in/api/users"); | ||
|
||
HttpURLConnection con = (HttpURLConnection)url.openConnection(); | ||
con.setRequestMethod("POST"); | ||
|
||
con.setRequestProperty("Content-Type", "application/json; utf-8"); | ||
con.setRequestProperty("Accept", "application/json"); | ||
|
||
con.setDoOutput(true); | ||
|
||
//JSON String need to be constructed for the specific resource. | ||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json | ||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}"; | ||
|
||
try(OutputStream os = con.getOutputStream()){ | ||
byte[] input = jsonInputString.getBytes("utf-8"); | ||
os.write(input, 0, input.length); | ||
} | ||
|
||
int code = con.getResponseCode(); | ||
System.out.println(code); | ||
|
||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){ | ||
StringBuilder response = new StringBuilder(); | ||
String responseLine = null; | ||
while ((responseLine = br.readLine()) != null) { | ||
response.append(responseLine.trim()); | ||
} | ||
System.out.println(response.toString()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
## Relevant articles: | ||
|
||
- [Void Type in Kotlin](https://www.baeldung.com/kotlin-void-type) | ||
- [How to use Kotlin Range Expressions](https://www.baeldung.com/kotlin-ranges) |
47 changes: 47 additions & 0 deletions
47
core-kotlin-2/src/test/kotlin/stringcomparison/StringComparisonTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package stringcomparison | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class StringComparisonUnitTest { | ||
|
||
@Test | ||
fun `compare using equals operator`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first == second } | ||
assertFalse { first == firstCapitalized } | ||
} | ||
|
||
@Test | ||
fun `compare using referential equals operator`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val copyOfFirst = buildString { "kotlin" } | ||
assertTrue { first === second } | ||
assertFalse { first === copyOfFirst } | ||
} | ||
|
||
@Test | ||
fun `compare using equals method`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first.equals(second) } | ||
assertFalse { first.equals(firstCapitalized) } | ||
assertTrue { first.equals(firstCapitalized, true) } | ||
} | ||
|
||
@Test | ||
fun `compare using compare method`() { | ||
val first = "kotlin" | ||
val second = "kotlin" | ||
val firstCapitalized = "KOTLIN" | ||
assertTrue { first.compareTo(second) == 0 } | ||
assertTrue { first.compareTo(firstCapitalized) == 32 } | ||
assertTrue { firstCapitalized.compareTo(first) == -32 } | ||
assertTrue { first.compareTo(firstCapitalized, true) == 0 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.