Skip to content

Commit b94071b

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 5d5a786 + c26630f commit b94071b

26 files changed

Lines changed: 242 additions & 9 deletions

File tree

‎algorithms/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@
3131
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
3232
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
3333
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
34+
- [Quicksort Algorithm Implementation in Java](https://www.baeldung.com/java-quicksort)
35+
- [Insertion Sort in Java](https://www.baeldung.com/java-insertion-sort)
36+
- [Converting Between Byte Arrays and Hexadecimal Strings in Java](https://www.baeldung.com/java-byte-arrays-hex-strings)

‎apache-geode/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Relevant Articles:
2+
3+
- [A Quick Guide to Apache Geode](https://www.baeldung.com/apache-geode)

‎core-java-collections/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
- [Time Complexity of Java Collections](https://www.baeldung.com/java-collections-complexity)
3939
- [Operating on and Removing an Item from Stream](https://www.baeldung.com/java-use-remove-item-stream)
4040
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
41+
- [Guide to EnumSet](https://www.baeldung.com/java-enumset)

‎core-java-io/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
- [Create a Symbolic Link with Java](http://www.baeldung.com/java-symlink)
3232
- [Quick Use of FilenameFilter](http://www.baeldung.com/java-filename-filter)
3333
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
34-
- [ Read a File into an ArrayList](https://www.baeldung.com/java-file-to-arraylist)
34+
- [Read a File into an ArrayList](https://www.baeldung.com/java-file-to-arraylist)
35+
- [Guide to Java OutputStream](https://www.baeldung.com/java-outputstream)

‎core-java-io/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@
154154
<artifactId>async-http-client</artifactId>
155155
<version>${async-http-client.version}</version>
156156
</dependency>
157+
<dependency>
158+
<groupId>com.opencsv</groupId>
159+
<artifactId>opencsv</artifactId>
160+
<version>${opencsv.version}</version>
161+
<scope>test</scope>
162+
</dependency>
157163
</dependencies>
158164

159165
<build>
@@ -247,7 +253,7 @@
247253
<protonpack.version>1.13</protonpack.version>
248254
<streamex.version>0.6.5</streamex.version>
249255
<vavr.version>0.9.0</vavr.version>
250-
256+
<opencsv.version>4.1</opencsv.version>
251257
<!-- testing -->
252258
<assertj.version>3.6.1</assertj.version>
253259
<avaitility.version>1.7.0</avaitility.version>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.baeldung.csv;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
import java.io.IOException;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.Scanner;
13+
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
import com.opencsv.CSVReader;
18+
19+
public class ReadCSVInArrayUnitTest {
20+
public static final String COMMA_DELIMITER = ",";
21+
public static final String CSV_FILE = "src/test/resources/book.csv";
22+
public static final List<List<String>> EXPECTED_ARRAY = Collections.unmodifiableList(new ArrayList<List<String>>() {
23+
{
24+
add(new ArrayList<String>() {
25+
{
26+
add("Mary Kom");
27+
add("Unbreakable");
28+
}
29+
});
30+
add(new ArrayList<String>() {
31+
{
32+
add("Kapil Isapuari");
33+
add("Farishta");
34+
}
35+
});
36+
}
37+
});
38+
39+
@Test
40+
public void givenCSVFile_whenBufferedReader_thenContentsAsExpected() throws IOException {
41+
List<List<String>> records = new ArrayList<List<String>>();
42+
try (BufferedReader br = new BufferedReader(new FileReader(CSV_FILE))) {
43+
String line = "";
44+
while ((line = br.readLine()) != null) {
45+
String[] values = line.split(COMMA_DELIMITER);
46+
records.add(Arrays.asList(values));
47+
}
48+
} catch (Exception e) {
49+
e.printStackTrace();
50+
}
51+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
52+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
53+
.toArray(),
54+
records.get(i)
55+
.toArray());
56+
}
57+
}
58+
59+
@Test
60+
public void givenCSVFile_whenScanner_thenContentsAsExpected() throws IOException {
61+
List<List<String>> records = new ArrayList<List<String>>();
62+
try (Scanner scanner = new Scanner(new File(CSV_FILE));) {
63+
while (scanner.hasNextLine()) {
64+
records.add(getRecordFromLine(scanner.nextLine()));
65+
}
66+
} catch (FileNotFoundException e) {
67+
e.printStackTrace();
68+
}
69+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
70+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
71+
.toArray(),
72+
records.get(i)
73+
.toArray());
74+
}
75+
}
76+
77+
private List<String> getRecordFromLine(String line) {
78+
List<String> values = new ArrayList<String>();
79+
try (Scanner rowScanner = new Scanner(line)) {
80+
rowScanner.useDelimiter(COMMA_DELIMITER);
81+
while (rowScanner.hasNext()) {
82+
values.add(rowScanner.next());
83+
}
84+
}
85+
return values;
86+
}
87+
88+
@Test
89+
public void givenCSVFile_whenOpencsv_thenContentsAsExpected() throws IOException {
90+
List<List<String>> records = new ArrayList<List<String>>();
91+
try (CSVReader csvReader = new CSVReader(new FileReader(CSV_FILE));) {
92+
String[] values = null;
93+
while ((values = csvReader.readNext()) != null) {
94+
records.add(Arrays.asList(values));
95+
}
96+
} catch (Exception e) {
97+
e.printStackTrace();
98+
}
99+
for (int i = 0; i < EXPECTED_ARRAY.size(); i++) {
100+
Assert.assertArrayEquals(EXPECTED_ARRAY.get(i)
101+
.toArray(),
102+
records.get(i)
103+
.toArray());
104+
}
105+
}
106+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Mary Kom,Unbreakable
2+
Kapil Isapuari,Farishta

‎core-java/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,4 @@
157157
- [Hashing a Password in Java](https://www.baeldung.com/java-password-hashing)
158158
- [Java Switch Statement](https://www.baeldung.com/java-switch)
159159
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
160+
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.string;
2+
3+
import java.math.RoundingMode;
4+
import java.text.DecimalFormat;
5+
import java.text.NumberFormat;
6+
7+
public class DoubleToString {
8+
9+
public static String truncateByCast(double d) {
10+
return String.valueOf((int) d);
11+
}
12+
13+
public static String roundWithStringFormat(double d) {
14+
return String.format("%.0f", d);
15+
}
16+
17+
public static String truncateWithNumberFormat(double d) {
18+
NumberFormat nf = NumberFormat.getInstance();
19+
nf.setMaximumFractionDigits(0);
20+
nf.setRoundingMode(RoundingMode.FLOOR);
21+
return nf.format(d);
22+
}
23+
24+
public static String roundWithNumberFormat(double d) {
25+
NumberFormat nf = NumberFormat.getInstance();
26+
nf.setMaximumFractionDigits(0);
27+
return nf.format(d);
28+
}
29+
30+
public static String truncateWithDecimalFormat(double d) {
31+
DecimalFormat df = new DecimalFormat("#,###");
32+
df.setRoundingMode(RoundingMode.FLOOR);
33+
return df.format(d);
34+
}
35+
36+
public static String roundWithDecimalFormat(double d) {
37+
DecimalFormat df = new DecimalFormat("#,###");
38+
return df.format(d);
39+
}
40+
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.string;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class DoubleToStringUnitTest {
8+
9+
private static final double DOUBLE_VALUE = 3.56;
10+
private static final String TRUNCATED_DOUBLE = "3";
11+
private static final String ROUNDED_UP_DOUBLE = "4";
12+
13+
14+
@Test
15+
public void truncateByCastTest() {
16+
assertThat(DoubleToString.truncateByCast(DOUBLE_VALUE)).isEqualTo(TRUNCATED_DOUBLE);
17+
}
18+
19+
@Test
20+
public void roundingWithStringFormatTest() {
21+
assertThat(DoubleToString.roundWithStringFormat(DOUBLE_VALUE)).isEqualTo(ROUNDED_UP_DOUBLE);
22+
}
23+
24+
@Test
25+
public void truncateWithNumberFormatTest() {
26+
assertThat(DoubleToString.truncateWithNumberFormat(DOUBLE_VALUE)).isEqualTo(TRUNCATED_DOUBLE);
27+
}
28+
29+
@Test
30+
public void roundWithNumberFormatTest() {
31+
assertThat(DoubleToString.roundWithNumberFormat(DOUBLE_VALUE)).isEqualTo(ROUNDED_UP_DOUBLE);
32+
}
33+
34+
@Test
35+
public void truncateWithDecimalFormatTest() {
36+
assertThat(DoubleToString.truncateWithDecimalFormat(DOUBLE_VALUE)).isEqualTo(TRUNCATED_DOUBLE);
37+
}
38+
39+
@Test
40+
public void roundWithDecimalFormatTest() {
41+
assertThat(DoubleToString.roundWithDecimalFormat(DOUBLE_VALUE)).isEqualTo(ROUNDED_UP_DOUBLE);
42+
}
43+
44+
45+
}

0 commit comments

Comments
 (0)