Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -85,5 +85,6 @@ transaction.log
|
||||
*-shell.log
|
||||
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
testing-modules/report-*.json
|
||||
|
||||
libraries-2/*.db
|
||||
@@ -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)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
This module contains articles about searching algorithms.
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Binary Search Algorithm in Java](https://www.baeldung.com/java-binary-search)
|
||||
- [Depth First Search in Java](https://www.baeldung.com/java-depth-first-search)
|
||||
- [Interpolation Search in Java](https://www.baeldung.com/java-interpolation-search)
|
||||
@@ -11,3 +12,4 @@ This module contains articles about searching algorithms.
|
||||
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](https://www.baeldung.com/java-monte-carlo-tree-search)
|
||||
- [Range Search Algorithm in Java](https://www.baeldung.com/java-range-search)
|
||||
- [Fast Pattern Matching of Strings Using Suffix Tree](https://www.baeldung.com/java-pattern-matching-suffix-tree)
|
||||
- [Find the Kth Smallest Element in Two Sorted Arrays](https://www.baeldung.com/java-kth-smallest-element-in-sorted-arrays)
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.algorithms.kthsmallest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static java.lang.Math.min;
|
||||
|
||||
public class KthSmallest {
|
||||
|
||||
public static int findKthSmallestElement(int k, int[] list1, int[] list2) throws NoSuchElementException, IllegalArgumentException {
|
||||
|
||||
checkInput(k, list1, list2);
|
||||
|
||||
// we are looking for the minimum value
|
||||
if(k == 1) {
|
||||
return min(list1[0], list2[0]);
|
||||
}
|
||||
|
||||
// we are looking for the maximum value
|
||||
if(list1.length + list2.length == k) {
|
||||
return max(list1[list1.length-1], list2[list2.length-1]);
|
||||
}
|
||||
|
||||
// swap lists if needed to make sure we take at least one element from list1
|
||||
if(k <= list2.length && list2[k-1] < list1[0]) {
|
||||
int[] list1_ = list1;
|
||||
list1 = list2;
|
||||
list2 = list1_;
|
||||
}
|
||||
|
||||
// correct left boundary if k is bigger than the size of list2
|
||||
int left = k < list2.length ? 0 : k - list2.length - 1;
|
||||
|
||||
// the inital right boundary cannot exceed the list1
|
||||
int right = min(k-1, list1.length - 1);
|
||||
|
||||
int nElementsList1, nElementsList2;
|
||||
|
||||
// binary search
|
||||
do {
|
||||
nElementsList1 = ((left + right) / 2) + 1;
|
||||
nElementsList2 = k - nElementsList1;
|
||||
|
||||
if(nElementsList2 > 0) {
|
||||
if (list1[nElementsList1 - 1] > list2[nElementsList2 - 1]) {
|
||||
right = nElementsList1 - 2;
|
||||
} else {
|
||||
left = nElementsList1;
|
||||
}
|
||||
}
|
||||
} while(!kthSmallesElementFound(list1, list2, nElementsList1, nElementsList2));
|
||||
|
||||
return nElementsList2 == 0 ? list1[nElementsList1-1] : max(list1[nElementsList1-1], list2[nElementsList2-1]);
|
||||
}
|
||||
|
||||
private static boolean kthSmallesElementFound(int[] list1, int[] list2, int nElementsList1, int nElementsList2) {
|
||||
|
||||
// we do not take any element from the second list
|
||||
if(nElementsList2 < 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(list1[nElementsList1-1] == list2[nElementsList2-1]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(nElementsList1 == list1.length) {
|
||||
return list1[nElementsList1-1] <= list2[nElementsList2];
|
||||
}
|
||||
|
||||
if(nElementsList2 == list2.length) {
|
||||
return list2[nElementsList2-1] <= list1[nElementsList1];
|
||||
}
|
||||
|
||||
return list1[nElementsList1-1] <= list2[nElementsList2] && list2[nElementsList2-1] <= list1[nElementsList1];
|
||||
}
|
||||
|
||||
|
||||
private static void checkInput(int k, int[] list1, int[] list2) throws NoSuchElementException, IllegalArgumentException {
|
||||
|
||||
if(list1 == null || list2 == null || k < 1) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
if(list1.length == 0 || list2.length == 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
if(k > list1.length + list2.length) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
public static int getKthElementSorted(int[] list1, int[] list2, int k) {
|
||||
|
||||
int length1 = list1.length, length2 = list2.length;
|
||||
int[] combinedArray = new int[length1 + length2];
|
||||
System.arraycopy(list1, 0, combinedArray, 0, list1.length);
|
||||
System.arraycopy(list2, 0, combinedArray, list1.length, list2.length);
|
||||
Arrays.sort(combinedArray);
|
||||
|
||||
return combinedArray[k-1];
|
||||
}
|
||||
|
||||
public static int getKthElementMerge(int[] list1, int[] list2, int k) {
|
||||
|
||||
int i1 = 0, i2 = 0;
|
||||
|
||||
while(i1 < list1.length && i2 < list2.length && (i1 + i2) < k) {
|
||||
if(list1[i1] < list2[i2]) {
|
||||
i1++;
|
||||
} else {
|
||||
i2++;
|
||||
}
|
||||
}
|
||||
|
||||
if((i1 + i2) < k) {
|
||||
return i1 < list1.length ? list1[k - i2 - 1] : list2[k - i1 - 1];
|
||||
} else if(i1 > 0 && i2 > 0) {
|
||||
return Math.max(list1[i1-1], list2[i2-1]);
|
||||
} else {
|
||||
return i1 == 0 ? list2[i2-1] : list1[i1-1];
|
||||
}
|
||||
}
|
||||
}
|
||||
+288
@@ -0,0 +1,288 @@
|
||||
package com.baeldung.algorithms.kthsmallest;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static com.baeldung.algorithms.kthsmallest.KthSmallest.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class KthSmallestUnitTest {
|
||||
|
||||
@Nested
|
||||
class Exceptions {
|
||||
|
||||
@Test
|
||||
public void when_at_least_one_list_is_null_then_an_exception_is_thrown() {
|
||||
|
||||
Executable executable1 = () -> findKthSmallestElement(1, null, null);
|
||||
Executable executable2 = () -> findKthSmallestElement(1, new int[]{2}, null);
|
||||
Executable executable3 = () -> findKthSmallestElement(1, null, new int[]{2});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, executable1);
|
||||
assertThrows(IllegalArgumentException.class, executable2);
|
||||
assertThrows(IllegalArgumentException.class, executable3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_at_least_one_list_is_empty_then_an_exception_is_thrown() {
|
||||
|
||||
Executable executable1 = () -> findKthSmallestElement(1, new int[]{}, new int[]{2});
|
||||
Executable executable2 = () -> findKthSmallestElement(1, new int[]{2}, new int[]{});
|
||||
Executable executable3 = () -> findKthSmallestElement(1, new int[]{}, new int[]{});
|
||||
|
||||
assertThrows(IllegalArgumentException.class, executable1);
|
||||
assertThrows(IllegalArgumentException.class, executable2);
|
||||
assertThrows(IllegalArgumentException.class, executable3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_smaller_than_0_then_an_exception_is_thrown() {
|
||||
Executable executable1 = () -> findKthSmallestElement(-1, new int[]{2}, new int[]{2});
|
||||
assertThrows(IllegalArgumentException.class, executable1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_smaller_than_1_then_an_exception_is_thrown() {
|
||||
Executable executable1 = () -> findKthSmallestElement(0, new int[]{2}, new int[]{2});
|
||||
assertThrows(IllegalArgumentException.class, executable1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_bigger_then_the_two_lists_then_an_exception_is_thrown() {
|
||||
Executable executable1 = () -> findKthSmallestElement(6, new int[]{1, 5, 6}, new int[]{2, 5});
|
||||
assertThrows(NoSuchElementException.class, executable1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
class K_is_smaller_than_the_size_of_list1_and_the_size_of_list2 {
|
||||
|
||||
@Test
|
||||
public void when_k_is_1_then_the_smallest_element_is_returned_from_list1() {
|
||||
int result = findKthSmallestElement(1, new int[]{2, 7}, new int[]{3, 5});
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_1_then_the_smallest_element_is_returned_list2() {
|
||||
int result = findKthSmallestElement(1, new int[]{3, 5}, new int[]{2, 7});
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_kth_element_is_smallest_element_and_occurs_in_both_lists() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(1, list1, list2);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_kth_element_is_smallest_element_and_occurs_in_both_lists2() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(2, list1, list2);
|
||||
assertEquals(1, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_kth_element_is_largest_element_and_occurs_in_both_lists_1() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(5, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_kth_element_is_largest_element_and_occurs_in_both_lists_2() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(6, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_kth_element_and_occurs_in_both_lists() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{0, 2, 3};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_kth_element_is_in_first_list() {
|
||||
int[] list1 = new int[]{1,2,3,4};
|
||||
int[] list2 = new int[]{1,3,4,5};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_kth_is_in_second_list() {
|
||||
int[] list1 = new int[]{1,3,4,4};
|
||||
int[] list2 = new int[]{1,2,4,5};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_elements_in_first_list_are_all_smaller_than_second_list() {
|
||||
int[] list1 = new int[]{1,3,7,9};
|
||||
int[] list2 = new int[]{11,12,14,15};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(7, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_elements_in_first_list_are_all_smaller_than_second_list2() {
|
||||
int[] list1 = new int[]{1,3,7,9};
|
||||
int[] list2 = new int[]{11,12,14,15};
|
||||
int result = findKthSmallestElement(4, list1, list2);
|
||||
assertEquals(9, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_only_elements_from_second_list_are_part_of_result() {
|
||||
int[] list1 = new int[]{11,12,14,15};
|
||||
int[] list2 = new int[]{1,3,7,9};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(7, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void and_only_elements_from_second_list_are_part_of_result2() {
|
||||
int[] list1 = new int[]{11,12,14,15};
|
||||
int[] list2 = new int[]{1,3,7,9};
|
||||
int result = findKthSmallestElement(4, list1, list2);
|
||||
assertEquals(9, result);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class K_is_bigger_than_the_size_of_at_least_one_of_the_lists {
|
||||
|
||||
@Test
|
||||
public void k_is_smaller_than_list1_and_bigger_than_list2() {
|
||||
int[] list1 = new int[]{1, 2, 3, 4, 7, 9};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(5, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void k_is_bigger_than_list1_and_smaller_than_list2() {
|
||||
int[] list1 = new int[]{1, 2, 3};
|
||||
int[] list2 = new int[]{1, 2, 3, 4, 7, 9};
|
||||
int result = findKthSmallestElement(5, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_bigger_than_the_size_of_both_lists_and_elements_in_second_list_are_all_smaller_than_first_list() {
|
||||
int[] list1 = new int[]{9, 11, 13, 55};
|
||||
int[] list2 = new int[]{1, 2, 3, 7};
|
||||
int result = findKthSmallestElement(6, list1, list2);
|
||||
assertEquals(11, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_bigger_than_the_size_of_both_lists_and_elements_in_second_list_are_all_bigger_than_first_list() {
|
||||
int[] list1 = new int[]{1, 2, 3, 7};
|
||||
int[] list2 = new int[]{9, 11, 13, 55};
|
||||
int result = findKthSmallestElement(6, list1, list2);
|
||||
assertEquals(11, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_bigger_than_the_size_of_both_lists() {
|
||||
int[] list1 = new int[]{3, 7, 9, 11, 55};
|
||||
int[] list2 = new int[]{1, 2, 3, 7, 13};
|
||||
int result = findKthSmallestElement(7, list1, list2);
|
||||
assertEquals(9, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_bigger_than_the_size_of_both_lists_and_list1_has_more_elements_than_list2() {
|
||||
int[] list1 = new int[]{3, 7, 9, 11, 55, 77, 100, 200};
|
||||
int[] list2 = new int[]{1, 2, 3, 7, 13};
|
||||
int result = findKthSmallestElement(11, list1, list2);
|
||||
assertEquals(77, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void max_test() {
|
||||
int[] list1 = new int[]{100, 200};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(4, list1, list2);
|
||||
assertEquals(100, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void max_test2() {
|
||||
int[] list1 = new int[]{100, 200};
|
||||
int[] list2 = new int[]{1, 2, 3};
|
||||
int result = findKthSmallestElement(5, list1, list2);
|
||||
assertEquals(200, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_smaller_than_the_size_of_both_lists_and_kth_element_in_list2() {
|
||||
int[] list1 = new int[]{1, 2, 5};
|
||||
int[] list2 = new int[]{1, 3, 4, 7};
|
||||
int result = findKthSmallestElement(4, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_smaller_than_the_size_of_both_lists_and_kth_element_is_smallest_in_list2() {
|
||||
int[] list1 = new int[]{1, 2, 5};
|
||||
int[] list2 = new int[]{3, 4, 7};
|
||||
int result = findKthSmallestElement(3, list1, list2);
|
||||
assertEquals(3, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void when_k_is_smaller_than_the_size_of_both_lists_and_kth_element_is_smallest_in_list23() {
|
||||
int[] list1 = new int[]{3, 11, 27, 53, 90};
|
||||
int[] list2 = new int[]{4, 20, 21, 100};
|
||||
int result = findKthSmallestElement(5, list1, list2);
|
||||
assertEquals(21, result);
|
||||
}
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void randomTests() {
|
||||
// IntStream.range(1, 100000).forEach(i -> random());
|
||||
// }
|
||||
|
||||
private void random() {
|
||||
|
||||
Random random = new Random();
|
||||
int length1 = (Math.abs(random.nextInt())) % 1000 + 1;
|
||||
int length2 = (Math.abs(random.nextInt())) % 1000 + 1;
|
||||
|
||||
int[] list1 = sortedRandomIntArrayOfLength(length1);
|
||||
int[] list2 = sortedRandomIntArrayOfLength(length2);
|
||||
|
||||
int k = (Math.abs(random.nextInt()) % (length1 + length2)) + 1 ;
|
||||
|
||||
int result = findKthSmallestElement(k, list1, list2);
|
||||
|
||||
int result2 = getKthElementSorted(list1, list2, k);
|
||||
|
||||
int result3 = getKthElementMerge(list1, list2, k);
|
||||
|
||||
assertEquals(result2, result);
|
||||
assertEquals(result2, result3);
|
||||
}
|
||||
|
||||
private int[] sortedRandomIntArrayOfLength(int length) {
|
||||
int[] intArray = new Random().ints(length).toArray();
|
||||
Arrays.sort(intArray);
|
||||
return intArray;
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,3 @@ This module contains articles about Apache CXF
|
||||
- [Apache CXF Support for RESTful Web Services](https://www.baeldung.com/apache-cxf-rest-api)
|
||||
- [A Guide to Apache CXF with Spring](https://www.baeldung.com/apache-cxf-with-spring)
|
||||
- [Introduction to Apache CXF](https://www.baeldung.com/introduction-to-apache-cxf)
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
@@ -3,9 +3,11 @@
|
||||
This module contains articles about Apache POI
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Microsoft Word Processing in Java with Apache POI](https://www.baeldung.com/java-microsoft-word-with-apache-poi)
|
||||
- [Working with Microsoft Excel in Java](https://www.baeldung.com/java-microsoft-excel)
|
||||
- [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow)
|
||||
- [Merge Cells in Excel Using Apache POI](https://www.baeldung.com/java-apache-poi-merge-cells)
|
||||
- [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value)
|
||||
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
|
||||
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.poi.excel.setformula;
|
||||
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ExcelFormula {
|
||||
public double setFormula(String fileLocation, XSSFWorkbook wb, String formula) throws IOException {
|
||||
XSSFSheet sheet = wb.getSheetAt(0);
|
||||
int lastCellNum = sheet.getRow(0).getLastCellNum();
|
||||
XSSFCell formulaCell = sheet.getRow(0).createCell(lastCellNum);
|
||||
formulaCell.setCellFormula(formula);
|
||||
XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||
formulaEvaluator.evaluateFormulaCell(formulaCell);
|
||||
FileOutputStream fileOut = new FileOutputStream(new File(fileLocation));
|
||||
wb.write(fileOut);
|
||||
wb.close();
|
||||
fileOut.close();
|
||||
return formulaCell.getNumericCellValue();
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.poi.excel.setformula;
|
||||
|
||||
import org.apache.poi.ss.util.CellReference;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
class ExcelFormulaUnitTest {
|
||||
private static String FILE_NAME = "com/baeldung/poi/excel/setformula/SetFormulaTest.xlsx";
|
||||
private String fileLocation;
|
||||
private ExcelFormula excelFormula;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws URISyntaxException {
|
||||
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
|
||||
excelFormula = new ExcelFormula();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenExcelData_whenSetFormula_thenSuccess() throws IOException {
|
||||
FileInputStream inputStream = new FileInputStream(new File(fileLocation));
|
||||
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
|
||||
XSSFSheet sheet = wb.getSheetAt(0);
|
||||
double resultColumnA = 0;
|
||||
double resultColumnB = 0;
|
||||
for (int row = 0; row <= sheet.getLastRowNum(); row++) {
|
||||
resultColumnA += sheet.getRow(row).getCell(0).getNumericCellValue();
|
||||
resultColumnB += sheet.getRow(row).getCell(1).getNumericCellValue();
|
||||
}
|
||||
String colNameA = CellReference.convertNumToColString(0);
|
||||
String colNameB = CellReference.convertNumToColString(1);
|
||||
String startCellA = colNameA + 1;
|
||||
String stopCellA = colNameA + (sheet.getLastRowNum() + 1);
|
||||
String sumFormulaForColumnA = String.format("SUM(%s:%s)", startCellA, stopCellA);
|
||||
String startCellB = colNameB + 1;
|
||||
String stopCellB = colNameB + (sheet.getLastRowNum() + 1);
|
||||
String sumFormulaForColumnB = String.format("SUM(%s:%s)", startCellB, stopCellB);
|
||||
|
||||
double resultValue = excelFormula.setFormula(fileLocation, wb, sumFormulaForColumnA + "-" + sumFormulaForColumnB);
|
||||
|
||||
Assert.assertEquals(resultColumnA - resultColumnB, resultValue, 0d);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -9,3 +9,4 @@ This module contains articles about Java 10 core features
|
||||
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
||||
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
|
||||
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
||||
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
||||
|
||||
@@ -1,22 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-10</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-10</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -34,6 +41,7 @@
|
||||
<properties>
|
||||
<maven.compiler.source.version>10</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>10</maven.compiler.target.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.java10.collections.conversion;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ListSetConversionUnitTest {
|
||||
|
||||
// Set -> List; List -> Set
|
||||
|
||||
@Test
|
||||
public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
|
||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||
final List<Integer> targetList = new ArrayList<>(sourceSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
|
||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||
final Set<Integer> targetSet = new HashSet<>(sourceList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava10_whenSetConvertedToList_thenCorrect() {
|
||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||
final List<Integer> targetList = List.copyOf(sourceSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingJava10_whenListConvertedToSet_thenCorrect() {
|
||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||
final Set<Integer> targetSet = Set.copyOf(sourceList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
|
||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||
final List<Integer> targetList = Lists.newArrayList(sourceSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
|
||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||
final Set<Integer> targetSet = Sets.newHashSet(sourceList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
|
||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||
|
||||
final Set<Integer> targetSet = new HashSet<>(6);
|
||||
CollectionUtils.addAll(targetSet, sourceList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
|
||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||
|
||||
final List<Integer> targetList = new ArrayList<>(6);
|
||||
CollectionUtils.addAll(targetList, sourceSet);
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
+1
-1
@@ -31,6 +31,6 @@ public class UseDateTimeFormatterUnitTest {
|
||||
public void givenALocalDate_whenFormattingWithStyleAndLocale_thenPass() {
|
||||
String result = subject.formatWithStyleAndLocale(localDateTime, FormatStyle.MEDIUM, Locale.UK);
|
||||
|
||||
assertThat(result).isEqualTo("25 Jan 2015, 06:30:00");
|
||||
assertThat(result).isEqualTo("25-Jan-2015 06:30:00");
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -3,6 +3,7 @@ package com.baeldung.datetime;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
@@ -24,10 +25,11 @@ public class UseToInstantUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenADate_whenConvertingToLocalDate_thenAsExpected() {
|
||||
Date givenDate = new Date(1465817690000L);
|
||||
LocalDateTime currentDateTime = LocalDateTime.now();
|
||||
Date givenDate = Date.from(currentDateTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
|
||||
LocalDateTime localDateTime = subject.convertDateToLocalDate(givenDate);
|
||||
|
||||
assertThat(localDateTime).isEqualTo("2016-06-13T13:34:50");
|
||||
assertThat(localDateTime).isEqualTo(currentDateTime);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class UseZonedDateTimeUnitTest {
|
||||
@Test
|
||||
public void givenAStringWithTimeZone_whenParsing_thenEqualsExpected() {
|
||||
ZonedDateTime resultFromString = zonedDateTime.getZonedDateTimeUsingParseMethod("2015-05-03T10:15:30+01:00[Europe/Paris]");
|
||||
ZonedDateTime resultFromLocalDateTime = ZonedDateTime.of(2015, 5, 3, 11, 15, 30, 0, ZoneId.of("Europe/Paris"));
|
||||
ZonedDateTime resultFromLocalDateTime = ZonedDateTime.of(2015, 5, 3, 10, 15, 30, 0, ZoneId.of("Europe/Paris"));
|
||||
|
||||
assertThat(resultFromString.getZone()).isEqualTo(ZoneId.of("Europe/Paris"));
|
||||
assertThat(resultFromLocalDateTime.getZone()).isEqualTo(ZoneId.of("Europe/Paris"));
|
||||
|
||||
@@ -13,3 +13,5 @@ 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)
|
||||
- [How to Get a Name of a Method Being Executed?](https://www.baeldung.com/java-name-of-executing-method)
|
||||
|
||||
@@ -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,7 +3,9 @@
|
||||
This module contains articles about advanced operations on arrays in Java. They assume some background knowledge with arrays in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Copy an Array in Java](https://www.baeldung.com/java-array-copy)
|
||||
- [Arrays.deepEquals](https://www.baeldung.com/java-arrays-deepequals)
|
||||
- [Find Sum and Average in a Java Array](https://www.baeldung.com/java-array-sum-average)
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Comparing Arrays in Java](https://www.baeldung.com/java-comparing-arrays)
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Plane {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String model;
|
||||
|
||||
public Plane(String name, String model) {
|
||||
|
||||
this.name = name;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Plane plane = (Plane) o;
|
||||
return Objects.equals(name, plane.name) && Objects.equals(model, plane.model);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(name, model);
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DeepEqualsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContents_whenDeepEquals_thenTrue() {
|
||||
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentsWithDifferentOrder_whenDeepEquals_thenFalse() {
|
||||
final Plane[][] planes1 = new Plane[][] { new Plane[] { new Plane("Plane 1", "A320") },
|
||||
new Plane[] { new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] { new Plane[] { new Plane("Plane 2", "B738") },
|
||||
new Plane[] { new Plane("Plane 1", "A320") } };
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isFalse();
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class EqualsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContents_whenEquals_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(Arrays.equals(planes1, planes2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentsDifferentOrder_whenEquals_thenFalse() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "B738", "A320", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(Arrays.equals(planes1, planes2)).isFalse();
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class LengthsCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameContent_whenSizeCompare_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final Integer[] quantities = new Integer[] { 10, 12, 34, 45, 12, 43, 5, 2 };
|
||||
|
||||
assertThat(planes1).hasSize(8);
|
||||
assertThat(quantities).hasSize(8);
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class OrderCompareUnitTest {
|
||||
@Test
|
||||
public void givenSameContentDifferentOrder_whenSortedAndDeepEquals_thenTrue() {
|
||||
final Plane[][] planes1 = new Plane[][] {
|
||||
new Plane[] { new Plane("Plane 1", "A320"), new Plane("Plane 2", "B738") } };
|
||||
final Plane[][] planes2 = new Plane[][] {
|
||||
new Plane[] { new Plane("Plane 2", "B738"), new Plane("Plane 1", "A320") } };
|
||||
|
||||
Comparator<Plane> planeComparator = (o1, o2) -> {
|
||||
if (o1.getName()
|
||||
.equals(o2.getName())) {
|
||||
return o2.getModel()
|
||||
.compareTo(o1.getModel());
|
||||
}
|
||||
return o2.getName()
|
||||
.compareTo(o1.getName());
|
||||
};
|
||||
Arrays.sort(planes1[0], planeComparator);
|
||||
Arrays.sort(planes2[0], planeComparator);
|
||||
|
||||
assertThat(Arrays.deepEquals(planes1, planes2)).isTrue();
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.arraycompare;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ReferenceCompareUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSameReferences_whenSame_thenTrue() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = planes1;
|
||||
|
||||
assertThat(planes1).isSameAs(planes2);
|
||||
|
||||
planes2[0] = "747";
|
||||
|
||||
assertThat(planes1).isSameAs(planes2);
|
||||
assertThat(planes2[0]).isEqualTo("747");
|
||||
assertThat(planes1[0]).isEqualTo("747");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSameContentDifferentReferences_whenSame_thenFalse() {
|
||||
final String[] planes1 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
final String[] planes2 = new String[] { "A320", "B738", "A321", "A319", "B77W", "B737", "A333", "A332" };
|
||||
|
||||
assertThat(planes1).isNotSameAs(planes2);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user