Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -7,4 +7,6 @@
|
||||
- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences)
|
||||
- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- [Implementing a 2048 Solver in Java](https://www.baeldung.com/2048-java-solver)
|
||||
- [Finding Top K Elements in an Array](https://www.baeldung.com/java-array-top-elements)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-5)
|
||||
|
||||
@@ -6,4 +6,4 @@ This module contains articles about Apache Shiro
|
||||
|
||||
- [Introduction to Apache Shiro](https://www.baeldung.com/apache-shiro)
|
||||
- [Permissions-Based Access Control with Apache Shiro](https://www.baeldung.com/apache-shiro-access-control)
|
||||
|
||||
- [Spring Security vs Apache Shiro](https://www.baeldung.com/spring-security-vs-apache-shiro)
|
||||
|
||||
@@ -13,4 +13,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||
- [Convert String to Integer in Groovy](https://www.baeldung.com/groovy-convert-string-to-integer)
|
||||
- [Groovy Variable Scope](https://www.baeldung.com/groovy/variable-scope)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
|
||||
@@ -8,7 +8,7 @@ This module contains articles about Java 11 core features
|
||||
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
|
||||
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
||||
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
|
||||
- [Exploring the New HTTP Client in Java](https://www.baeldung.com/java-9-http-client)
|
||||
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
|
||||
- [Guide to jlink](https://www.baeldung.com/jlink)
|
||||
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
|
||||
|
||||
@@ -13,3 +13,4 @@ This module contains articles about core Java features that have been introduced
|
||||
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
||||
- [Multi-Release JAR Files with Maven](https://www.baeldung.com/maven-multi-release-jars)
|
||||
- [The Difference between RxJava API and the Java 9 Flow API](https://www.baeldung.com/rxjava-vs-java-flow-api)
|
||||
|
||||
@@ -9,3 +9,4 @@ This module contains articles about Java 9 core features
|
||||
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Immutable ArrayList in Java](https://www.baeldung.com/java-immutable-list)
|
||||
- [Easy Ways to Write a Java InputStream to an OutputStream](https://www.baeldung.com/java-inputstream-to-outputstream)
|
||||
|
||||
@@ -5,4 +5,4 @@ This module contains complete guides about arrays in Java
|
||||
### Relevant Articles:
|
||||
- [Arrays in Java: A Reference Guide](https://www.baeldung.com/java-arrays-guide)
|
||||
- [Guide to the java.util.Arrays Class](https://www.baeldung.com/java-util-arrays)
|
||||
- [What is [Ljava.lang.Object;?]](https://www.baeldung.com/java-tostring-array)
|
||||
- [What is \[Ljava.lang.Object;?](https://www.baeldung.com/java-tostring-array)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
## Core Java Collections Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Time Comparison of Arrays.sort(Object[]) and Arrays.sort(int[])](https://www.baeldung.com/arrays-sortobject-vs-sortint)
|
||||
- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector)
|
||||
- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences)
|
||||
@@ -10,3 +11,4 @@
|
||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||
- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack)
|
||||
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
|
||||
|
||||
@@ -15,4 +15,5 @@ This module contains articles about advanced topics about multithreading with co
|
||||
- [The ABA Problem in Concurrency](https://www.baeldung.com/cs/aba-concurrency)
|
||||
- [Introduction to Lock-Free Data Structures](https://www.baeldung.com/lock-free-programming)
|
||||
- [Introduction to Exchanger in Java](https://www.baeldung.com/java-exchanger)
|
||||
- [Why Not To Start A Thread In The Constructor?](https://www.baeldung.com/java-thread-constructor)
|
||||
- [[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
This module contains articles about basic Java concurrency
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
- [wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
|
||||
- [Difference Between Wait and Sleep in Java](https://www.baeldung.com/java-wait-and-sleep)
|
||||
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
|
||||
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
|
||||
- [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference)
|
||||
- [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic)
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.consoleout;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ConsoleAndOut {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
printConsoleObject();
|
||||
readPasswordFromConsole();
|
||||
} catch (Exception ex) {
|
||||
// Eating NullPointerExcpetion which will occur when this
|
||||
// program will be run from mediums other than console
|
||||
}
|
||||
printSysOut();
|
||||
}
|
||||
|
||||
static void printConsoleObject() {
|
||||
Console console = System.console();
|
||||
console.writer().print(console);
|
||||
}
|
||||
|
||||
static void readPasswordFromConsole() {
|
||||
Console console = System.console();
|
||||
char[] password = console.readPassword("Enter password: ");
|
||||
console.printf(String.valueOf(password));
|
||||
}
|
||||
|
||||
static void printSysOut() {
|
||||
System.out.println(System.out);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.consoleout;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConsoleAndOutUnitTest {
|
||||
|
||||
@Test
|
||||
void whenRetreivingConsole_thenPrintConsoleObject() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
ConsoleAndOut.printConsoleObject();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReadingPassword_thenReadPassword() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
ConsoleAndOut.readPasswordFromConsole();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRetrievingSysOut_thenPrintSysOutObject() {
|
||||
ConsoleAndOut.printSysOut();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Get the Current Date Prior to Java 8](https://www.baeldung.com/java-get-the-current-date-legacy)
|
||||
- [Skipping Weekends While Adding Days to LocalDate in Java 8](https://www.baeldung.com/java-localdate-add-days-skip-weekends)
|
||||
- [Checking If Two Java Dates Are on the Same Day](https://www.baeldung.com/java-check-two-dates-on-same-day)
|
||||
@@ -9,4 +10,5 @@ This module contains articles about date operations in Java.
|
||||
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
||||
- [How to determine day of week by passing specific date in Java?](https://www.baeldung.com/java-get-day-of-week)
|
||||
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
|
||||
- [Getting the Week Number From Any Date](https://www.baeldung.com/java-get-week-number)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
||||
|
||||
@@ -12,3 +12,5 @@ This module contains articles about core java exceptions
|
||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
||||
- [Java Global Exception Handler](https://www.baeldung.com/java-global-exception-handler)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Java IOException “Too many open files”](https://www.baeldung.com/java-too-many-open-files)
|
||||
- [When Does Java Throw the ExceptionInInitializerError?](https://www.baeldung.com/java-exceptionininitializererror)
|
||||
|
||||
@@ -3,5 +3,7 @@
|
||||
This module contains articles about core Java input and output (IO)
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java – Create a File](https://www.baeldung.com/java-how-to-create-a-file)
|
||||
- [Check If a Directory Is Empty in Java](https://www.baeldung.com/java-check-empty-directory)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
|
||||
@@ -46,41 +46,14 @@
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
|
||||
<dependency>
|
||||
<groupId>com.github.tomakehurst</groupId>
|
||||
<artifactId>wiremock</artifactId>
|
||||
<version>${wiremock.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-io-3</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>${maven-javadoc-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<wiremock.version>2.26.3</wiremock.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.emptiness;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DirectoryEmptinessUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenInvalid_thenReturnsFalse() throws IOException {
|
||||
assertThat(isEmpty(Paths.get("invalid-addr"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").getPath());
|
||||
assertThat(isEmpty(aFile)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotEmptyDir_thenReturnsFalse() throws IOException {
|
||||
Path currentDir = new File("").toPath().toAbsolutePath();
|
||||
assertThat(isEmpty(currentDir)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenIsEmpty_thenReturnsTrue() throws Exception {
|
||||
Path path = Files.createTempDirectory("baeldung-empty");
|
||||
assertThat(isEmpty(path)).isTrue();
|
||||
}
|
||||
|
||||
private static boolean isEmpty(Path path) throws IOException {
|
||||
if (Files.isDirectory(path)) {
|
||||
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
|
||||
return !directory.iterator().hasNext();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isEmpty2(Path path) throws IOException {
|
||||
if (Files.isDirectory(path)) {
|
||||
try (Stream<Path> entries = Files.list(path)) {
|
||||
return !entries.findFirst().isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isEmptyInefficient(Path path) {
|
||||
return path.toFile().listFiles().length == 0;
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,6 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [Measuring Object Sizes in the JVM](https://www.baeldung.com/jvm-measuring-object-sizes)
|
||||
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
||||
- [boolean and boolean[] Memory Layout in the JVM](https://www.baeldung.com/jvm-boolean-memory-layout)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
- [Where Is the Array Length Stored in JVM?](https://www.baeldung.com/java-jvm-array-length)
|
||||
- [Memory Address of Objects in Java](https://www.baeldung.com/java-object-memory-address)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
- [Class.isInstance vs Class.isAssignableFrom](https://www.baeldung.com/java-isinstance-isassignablefrom)
|
||||
- [Converting a Java String Into a Boolean](https://www.baeldung.com/java-string-to-boolean)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-2)
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
This module contains articles about methods in Java
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Methods in Java](https://www.baeldung.com/java-methods)
|
||||
- [Method Overloading and Overriding in Java](https://www.baeldung.com/java-method-overload-override)
|
||||
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
|
||||
- [Guide to hashCode() in Java](https://www.baeldung.com/java-hashcode)
|
||||
- [The Covariant Return Type in Java](https://www.baeldung.com/java-covariant-return-type)
|
||||
|
||||
@@ -12,4 +12,5 @@ This module contains articles about networking in Java
|
||||
- [Authentication with HttpUrlConnection](https://www.baeldung.com/java-http-url-connection)
|
||||
- [Download a File from an URL in Java](https://www.baeldung.com/java-download-file)
|
||||
- [Handling java.net.ConnectException](https://www.baeldung.com/java-net-connectexception)
|
||||
- [Getting MAC addresses in Java](https://www.baeldung.com/java-mac-address)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-networking)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Reading the Value of ‘private’ Fields from a Different Class in Java](https://www.baeldung.com/java-reflection-read-private-field-value)
|
||||
@@ -8,5 +8,4 @@
|
||||
- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params)
|
||||
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
|
||||
- [What Causes java.lang.reflect.InvocationTargetException?](https://www.baeldung.com/java-lang-reflect-invocationtargetexception)
|
||||
- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null)
|
||||
- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
## Core Java 8 Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [An Overview of Regular Expressions Performance in Java](https://www.baeldung.com/java-regex-performance)
|
||||
- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java)
|
||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||
@@ -11,3 +12,4 @@
|
||||
- [How to Use Regular Expressions to Replace Tokens in Strings](https://www.baeldung.com/java-regex-token-replacement)
|
||||
- [Regular Expressions \s and \s+ in Java](https://www.baeldung.com/java-regex-s-splus)
|
||||
- [Validate Phone Numbers With Java Regex](https://www.baeldung.com/java-regex-validate-phone-numbers)
|
||||
- [How to Count the Number of Matches for a Regex?](https://www.baeldung.com/java-count-regex-matches)
|
||||
|
||||
@@ -9,4 +9,5 @@ This module contains articles about core Java Security
|
||||
- [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing)
|
||||
- [SHA-256 and SHA3-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
|
||||
- [Checksums in Java](https://www.baeldung.com/java-checksums)
|
||||
- [How to Read PEM File to Get Public and Private Keys](https://www.baeldung.com/java-read-pem-file-keys)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Version Comparison in Java](https://www.baeldung.com/java-comparing-versions)
|
||||
@@ -3,6 +3,7 @@
|
||||
This module contains articles about core Kotlin collections.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Split a List Into Parts in Kotlin](https://www.baeldung.com/kotlin-split-list-into-parts)
|
||||
- [Finding an Element in a List Using Kotlin](https://www.baeldung.com/kotlin-finding-element-in-list)
|
||||
- [Overview of Kotlin Collections API](https://www.baeldung.com/kotlin-collections-api)
|
||||
@@ -12,3 +13,4 @@ This module contains articles about core Kotlin collections.
|
||||
- [Difference between fold and reduce in Kotlin](https://www.baeldung.com/kotlin/fold-vs-reduce)
|
||||
- [Guide to Sorting in Kotlin](https://www.baeldung.com/kotlin-sort)
|
||||
- [Working With Lists in Kotlin](https://www.baeldung.com/kotlin/lists)
|
||||
- [Iterating Collections by Index in Kotlin](https://www.baeldung.com/kotlin/iterating-collections-by-index)
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.index
|
||||
|
||||
fun main() {
|
||||
|
||||
// Index only
|
||||
val colors = listOf("Red", "Green", "Blue")
|
||||
for (i in colors.indices) {
|
||||
println(colors[i])
|
||||
}
|
||||
|
||||
val colorArray = arrayOf("Red", "Green", "Blue")
|
||||
for (i in colorArray.indices) {
|
||||
println(colorArray[i])
|
||||
}
|
||||
|
||||
(0 until colors.size).forEach { println(colors[it]) }
|
||||
for (i in 0 until colors.size) {
|
||||
println(colors[i])
|
||||
}
|
||||
|
||||
// Index and Value
|
||||
colors.forEachIndexed { i, v -> println("The value for index $i is $v") }
|
||||
for (indexedValue in colors.withIndex()) {
|
||||
println("The value for index ${indexedValue.index} is ${indexedValue.value}")
|
||||
}
|
||||
|
||||
for ((i, v) in colors.withIndex()) {
|
||||
println("The value for index $i is $v")
|
||||
}
|
||||
|
||||
colors.filterIndexed { i, _ -> i % 2 == 0 }
|
||||
colors.filterIndexed { _, v -> v == "RED" }
|
||||
}
|
||||
@@ -7,4 +7,5 @@ This module contains articles about Object-Oriented Programming in Kotlin
|
||||
- [Generics in Kotlin](https://www.baeldung.com/kotlin-generics)
|
||||
- [Delegated Properties in Kotlin](https://www.baeldung.com/kotlin-delegated-properties)
|
||||
- [Delegation Pattern in Kotlin](https://www.baeldung.com/kotlin-delegation-pattern)
|
||||
- [Anonymous Inner Classes in Kotlin](https://www.baeldung.com/kotlin/anonymous-inner-classes)
|
||||
- [[<-- Prev]](/core-kotlin-modules/core-kotlin-lang-oop)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user