readFileInList(String filePath) {
+ File file = new File(filePath)
+ def lines = file.readLines()
+ return lines
+ }
+
+ /**
+ * reads file content in string using File.text
+ * @param filePath
+ * @return
+ */
+ String readFileString(String filePath) {
+ File file = new File(filePath)
+ String fileContent = file.text
+ return fileContent
+ }
+
+ /**
+ * reads file content in string with encoding using File.getText
+ * @param filePath
+ * @return
+ */
+ String readFileStringWithCharset(String filePath) {
+ File file = new File(filePath)
+ String utf8Content = file.getText("UTF-8")
+ return utf8Content
+ }
+
+ /**
+ * reads content of binary file and returns byte array
+ * @param filePath
+ * @return
+ */
+ byte[] readBinaryFile(String filePath) {
+ File file = new File(filePath)
+ byte[] binaryContent = file.bytes
+ return binaryContent
+ }
+
+ /**
+ * More Examples of reading a file
+ * @return
+ */
+ def moreExamples() {
+
+ //with reader with utf-8
+ new File("src/main/resources/utf8Content.html").withReader('UTF-8') { reader ->
+ def line
+ while ((line = reader.readLine())!=null) {
+ println "${line}"
+ }
+ }
+
+ //collect api
+ def list = new File("src/main/resources/fileContent.txt").collect {it}
+
+ //as operator
+ def array = new File("src/main/resources/fileContent.txt") as String[]
+
+ //eachline
+ new File("src/main/resources/fileContent.txt").eachLine { line ->
+ println line
+ }
+
+ //newInputStream with eachLine
+ def is = new File("src/main/resources/fileContent.txt").newInputStream()
+ is.eachLine {
+ println it
+ }
+ is.close()
+
+ //withInputStream
+ new File("src/main/resources/fileContent.txt").withInputStream { stream ->
+ stream.eachLine { line ->
+ println line
+ }
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/core-groovy/src/main/resources/fileContent.txt b/core-groovy/src/main/resources/fileContent.txt
new file mode 100644
index 0000000000..643e4af3de
--- /dev/null
+++ b/core-groovy/src/main/resources/fileContent.txt
@@ -0,0 +1,3 @@
+Line 1 : Hello World!!!
+Line 2 : This is a file content.
+Line 3 : String content
diff --git a/core-groovy/src/main/resources/sample.png b/core-groovy/src/main/resources/sample.png
new file mode 100644
index 0000000000..7d473430b7
Binary files /dev/null and b/core-groovy/src/main/resources/sample.png differ
diff --git a/core-groovy/src/main/resources/utf8Content.html b/core-groovy/src/main/resources/utf8Content.html
new file mode 100644
index 0000000000..61ff338f6c
--- /dev/null
+++ b/core-groovy/src/main/resources/utf8Content.html
@@ -0,0 +1,12 @@
+
+
+
+
+
+ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ
+ᛋᚳᛖᚪᛚ᛫ᚦᛖᚪᚻ᛫ᛗᚪᚾᚾᚪ᛫ᚷᛖᚻᚹᛦᛚᚳ᛫ᛗᛁᚳᛚᚢᚾ᛫ᚻᛦᛏ᛫ᛞᚫᛚᚪᚾ
+ᚷᛁᚠ᛫ᚻᛖ᛫ᚹᛁᛚᛖ᛫ᚠᚩᚱ᛫ᛞᚱᛁᚻᛏᚾᛖ᛫ᛞᚩᛗᛖᛋ᛫ᚻᛚᛇᛏᚪᚾ
+
+
+
\ No newline at end of file
diff --git a/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy
new file mode 100644
index 0000000000..a479c265c4
--- /dev/null
+++ b/core-groovy/src/test/groovy/com/baeldung/file/ReadFileUnitTest.groovy
@@ -0,0 +1,70 @@
+package com.baeldung.file
+
+import spock.lang.Specification
+
+class ReadFileUnitTest extends Specification {
+
+ ReadFile readFile
+
+ void setup () {
+ readFile = new ReadFile()
+ }
+
+ def 'Should return number of lines in File using ReadFile.readFileLineByLine given filePath' () {
+ given:
+ def filePath = "src/main/resources/fileContent.txt"
+ when:
+ def noOfLines = readFile.readFileLineByLine(filePath)
+ then:
+ noOfLines
+ noOfLines instanceof Integer
+ assert noOfLines, 3
+ }
+
+ def 'Should return File Content in list of lines using ReadFile.readFileInList given filePath' () {
+ given:
+ def filePath = "src/main/resources/fileContent.txt"
+ when:
+ def lines = readFile.readFileInList(filePath)
+ then:
+ lines
+ lines instanceof List
+ assert lines.size(), 3
+ }
+
+ def 'Should return file content in string using ReadFile.readFileString given filePath' () {
+ given:
+ def filePath = "src/main/resources/fileContent.txt"
+ when:
+ def fileContent = readFile.readFileString(filePath)
+ then:
+ fileContent
+ fileContent instanceof String
+ fileContent.contains("""Line 1 : Hello World!!!
+Line 2 : This is a file content.
+Line 3 : String content""")
+
+ }
+
+ def 'Should return UTF-8 encoded file content in string using ReadFile.readFileStringWithCharset given filePath' () {
+ given:
+ def filePath = "src/main/resources/utf8Content.html"
+ when:
+ def encodedContent = readFile.readFileStringWithCharset(filePath)
+ then:
+ encodedContent
+ encodedContent instanceof String
+ }
+
+ def 'Should return binary file content in byte array using ReadFile.readBinaryFile given filePath' () {
+ given:
+ def filePath = "src/main/resources/sample.png"
+ when:
+ def binaryContent = readFile.readBinaryFile(filePath)
+ then:
+ binaryContent
+ binaryContent instanceof byte[]
+ binaryContent.length == 329
+ }
+
+}
\ No newline at end of file
diff --git a/core-java-10/pom.xml b/core-java-10/pom.xml
index 9fcdd9a162..b15f8b5d63 100644
--- a/core-java-10/pom.xml
+++ b/core-java-10/pom.xml
@@ -3,9 +3,9 @@
4.0.0
com.baeldung
core-java-10
- jar
0.1.0-SNAPSHOT
core-java-10
+ jar
http://maven.apache.org
diff --git a/core-java-11/README.md b/core-java-11/README.md
index c8039f4bc5..3c8b94fa28 100644
--- a/core-java-11/README.md
+++ b/core-java-11/README.md
@@ -3,3 +3,4 @@
- [Java 11 Single File Source Code](https://www.baeldung.com/java-single-file-source-code)
- [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)
diff --git a/core-java-11/pom.xml b/core-java-11/pom.xml
index 4dcab49867..a9776d8f3b 100644
--- a/core-java-11/pom.xml
+++ b/core-java-11/pom.xml
@@ -3,9 +3,9 @@
4.0.0
com.baeldung
core-java-11
- jar
0.1.0-SNAPSHOT
core-java-11
+ jar
http://maven.apache.org
diff --git a/core-java-8/pom.xml b/core-java-8/pom.xml
index 112e8b764d..b4519a8161 100644
--- a/core-java-8/pom.xml
+++ b/core-java-8/pom.xml
@@ -4,8 +4,8 @@
com.baeldung
core-java-8
0.1.0-SNAPSHOT
- jar
core-java-8
+ jar
com.baeldung
diff --git a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java
index bad1c32e4a..9ace27e38f 100644
--- a/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java
+++ b/core-java-8/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java
@@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class Java8CollectorsUnitTest {
private final List givenList = Arrays.asList("a", "bb", "ccc", "dd");
+ private final List listWithDuplicates = Arrays.asList("a", "bb", "c", "d", "bb");
@Test
public void whenCollectingToList_shouldCollectToList() throws Exception {
@@ -48,12 +49,19 @@ public class Java8CollectorsUnitTest {
}
@Test
- public void whenCollectingToList_shouldCollectToSet() throws Exception {
+ public void whenCollectingToSet_shouldCollectToSet() throws Exception {
final Set result = givenList.stream().collect(toSet());
assertThat(result).containsAll(givenList);
}
+ @Test
+ public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception {
+ final Set result = listWithDuplicates.stream().collect(toSet());
+
+ assertThat(result).hasSize(4);
+ }
+
@Test
public void whenCollectingToCollection_shouldCollectToCollection() throws Exception {
final List result = givenList.stream().collect(toCollection(LinkedList::new));
@@ -83,6 +91,13 @@ public class Java8CollectorsUnitTest {
assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2);
}
+ @Test
+ public void givenContainsDuplicateElements_whenCollectingToMap_shouldThrowException() throws Exception {
+ assertThatThrownBy(() -> {
+ listWithDuplicates.stream().collect(toMap(Function.identity(), String::length));
+ }).isInstanceOf(IllegalStateException.class);
+ }
+
@Test
public void whenCollectingAndThen_shouldCollect() throws Exception {
final List result = givenList.stream().collect(collectingAndThen(toList(), ImmutableList::copyOf));
diff --git a/core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceExamples.java b/core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceUnitTest.java
similarity index 89%
rename from core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceExamples.java
rename to core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceUnitTest.java
index ede0ef9f70..835815aecd 100644
--- a/core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceExamples.java
+++ b/core-java-8/src/test/java/com/baeldung/java8/lambda/methodreference/MethodReferenceUnitTest.java
@@ -5,22 +5,21 @@ import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
+import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
-public class MethodReferenceExamples {
+public class MethodReferenceUnitTest {
private static void doNothingAtAll(Object... o) {
}
;
-
+
@Test
public void referenceToStaticMethod() {
List messages = Arrays.asList("Hello", "Baeldung", "readers!");
- messages.forEach((word) -> {
- System.out.println(word);
- });
- messages.forEach(System.out::println);
+ messages.forEach(word -> StringUtils.capitalize(word));
+ messages.forEach(StringUtils::capitalize);
}
@Test
@@ -63,7 +62,7 @@ public class MethodReferenceExamples {
@Test
public void limitationsAndAdditionalExamples() {
createBicyclesList().forEach(b -> System.out.printf("Bike brand is '%s' and frame size is '%d'%n", b.getBrand(), b.getFrameSize()));
- createBicyclesList().forEach((o) -> MethodReferenceExamples.doNothingAtAll(o));
+ createBicyclesList().forEach((o) -> MethodReferenceUnitTest.doNothingAtAll(o));
}
private List createBicyclesList() {
diff --git a/core-java-9/README.md b/core-java-9/README.md
index c96267dc95..d9586ba684 100644
--- a/core-java-9/README.md
+++ b/core-java-9/README.md
@@ -26,3 +26,4 @@
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
+- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
diff --git a/core-java-9/compile-aot.sh b/core-java-9/compile-aot.sh
new file mode 100755
index 0000000000..c3a39b0196
--- /dev/null
+++ b/core-java-9/compile-aot.sh
@@ -0,0 +1,5 @@
+#!/usr/bin/env bash
+cd src/main/java
+javac com/baeldung/java9/aot/JaotCompilation.java
+jaotc --output jaotCompilation.so com/baeldung/java9/aot/JaotCompilation.class
+
diff --git a/core-java-9/run-aot.sh b/core-java-9/run-aot.sh
new file mode 100755
index 0000000000..89fc616469
--- /dev/null
+++ b/core-java-9/run-aot.sh
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+cd src/main/java
+java -XX:AOTLibrary=./jaotCompilation.so com/baeldung/java9/aot/JaotCompilation
\ No newline at end of file
diff --git a/core-java-9/src/main/java/com/baeldung/java9/aot/JaotCompilation.java b/core-java-9/src/main/java/com/baeldung/java9/aot/JaotCompilation.java
new file mode 100644
index 0000000000..4d8a018d6b
--- /dev/null
+++ b/core-java-9/src/main/java/com/baeldung/java9/aot/JaotCompilation.java
@@ -0,0 +1,12 @@
+package com.baeldung.java9.aot;
+
+public class JaotCompilation {
+
+ public static void main(String[] argv) {
+ System.out.println(message());
+ }
+
+ public static String message() {
+ return "The JAOT compiler says 'Hello'";
+ }
+}
\ No newline at end of file
diff --git a/core-java-arrays/README.md b/core-java-arrays/README.md
index 400dd7793c..ed8221ebe4 100644
--- a/core-java-arrays/README.md
+++ b/core-java-arrays/README.md
@@ -14,3 +14,4 @@
- [Array Operations in Java](http://www.baeldung.com/java-common-array-operations)
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
+- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)
diff --git a/core-java-arrays/pom.xml b/core-java-arrays/pom.xml
index d2d0453e87..39ac764b27 100644
--- a/core-java-arrays/pom.xml
+++ b/core-java-arrays/pom.xml
@@ -4,8 +4,8 @@
com.baeldung
core-java-arrays
0.1.0-SNAPSHOT
- jar
core-java-arrays
+ jar
com.baeldung
diff --git a/core-java-collections-list/pom.xml b/core-java-collections-list/pom.xml
index a7e711088a..737be8ee34 100644
--- a/core-java-collections-list/pom.xml
+++ b/core-java-collections-list/pom.xml
@@ -3,8 +3,8 @@
4.0.0
core-java-collections-list
0.1.0-SNAPSHOT
- jar
core-java-collections-list
+ jar
com.baeldung
@@ -40,17 +40,17 @@
net.sf.trove4j
trove4j
- 3.0.2
+ ${trove4j.version}
it.unimi.dsi
fastutil
- 8.1.0
+ ${fastutil.version}
colt
colt
- 1.2.0
+ ${colt.version}
@@ -60,5 +60,8 @@
1.7.0
3.11.1
1.16.12
+ 3.0.2
+ 8.1.0
+ 1.2.0