Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -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,4 +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)
|
||||
- [Topological Sort of Directed Acyclic Graph](https://www.baeldung.com/cs/dag-topological-sort)
|
||||
- [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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+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"));
|
||||
|
||||
@@ -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)
|
||||
|
||||
+11
-2
@@ -5,7 +5,16 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
@@ -138,7 +147,7 @@ public class JavaSortingUnitTest {
|
||||
HashSet<Integer> descSortedIntegersSet = new LinkedHashSet<>(Arrays.asList(255, 200, 123, 89, 88, 66, 7, 5, 1));
|
||||
|
||||
ArrayList<Integer> list = new ArrayList<>(integersSet);
|
||||
list.sort((i1, i2) -> i2 - i1);
|
||||
list.sort(Comparator.reverseOrder());
|
||||
integersSet = new LinkedHashSet<>(list);
|
||||
|
||||
assertTrue(Arrays.equals(integersSet.toArray(), descSortedIntegersSet.toArray()));
|
||||
|
||||
@@ -12,3 +12,4 @@
|
||||
- [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)
|
||||
- [A Guide to BitSet in Java](https://www.baeldung.com/java-bitset)
|
||||
|
||||
@@ -10,4 +10,5 @@ This module contains articles about the Java List collection
|
||||
- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance)
|
||||
- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list)
|
||||
- [How to Count Duplicate Elements in Arraylist](https://www.baeldung.com/java-count-duplicate-elements-arraylist)
|
||||
- [Finding the Differences Between Two Lists in Java](https://www.baeldung.com/java-lists-difference)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-collections-list-2)
|
||||
|
||||
@@ -21,20 +21,15 @@
|
||||
<version>${eclipse-collections.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.trove4j</groupId>
|
||||
<artifactId>trove4j</artifactId>
|
||||
<version>${trove4j.version}</version>
|
||||
<groupId>com.carrotsearch</groupId>
|
||||
<artifactId>hppc</artifactId>
|
||||
<version>${hppc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>it.unimi.dsi</groupId>
|
||||
<artifactId>fastutil</artifactId>
|
||||
<version>${fastutil.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>colt</groupId>
|
||||
<artifactId>colt</artifactId>
|
||||
<version>${colt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@@ -69,9 +64,8 @@
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
<trove4j.version>3.0.2</trove4j.version>
|
||||
<hppc.version>0.7.2</hppc.version>
|
||||
<fastutil.version>8.1.0</fastutil.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+57
-24
@@ -1,29 +1,68 @@
|
||||
package com.baeldung.map.primitives;
|
||||
|
||||
import cern.colt.map.AbstractIntDoubleMap;
|
||||
import cern.colt.map.OpenIntDoubleHashMap;
|
||||
import gnu.trove.map.TDoubleIntMap;
|
||||
import gnu.trove.map.hash.TDoubleIntHashMap;
|
||||
import com.carrotsearch.hppc.IntLongHashMap;
|
||||
import com.carrotsearch.hppc.IntLongScatterMap;
|
||||
import com.carrotsearch.hppc.IntObjectHashMap;
|
||||
import com.carrotsearch.hppc.IntObjectMap;
|
||||
import com.carrotsearch.hppc.IntObjectScatterMap;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2BooleanMaps;
|
||||
import it.unimi.dsi.fastutil.ints.Int2BooleanOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2BooleanSortedMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2BooleanSortedMaps;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
||||
|
||||
import org.eclipse.collections.api.map.primitive.ImmutableIntIntMap;
|
||||
import org.eclipse.collections.api.map.primitive.MutableIntIntMap;
|
||||
import org.eclipse.collections.api.map.primitive.MutableObjectDoubleMap;
|
||||
import org.eclipse.collections.impl.factory.primitive.IntIntMaps;
|
||||
import org.eclipse.collections.impl.factory.primitive.ObjectDoubleMaps;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class PrimitiveMaps {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
hppcMap();
|
||||
eclipseCollectionsMap();
|
||||
troveMap();
|
||||
coltMap();
|
||||
fastutilMap();
|
||||
}
|
||||
|
||||
private static void hppcMap() {
|
||||
//Regular maps
|
||||
IntLongHashMap intLongHashMap = new IntLongHashMap();
|
||||
intLongHashMap.put(25,1L);
|
||||
intLongHashMap.put(150,Long.MAX_VALUE);
|
||||
intLongHashMap.put(1,0L);
|
||||
|
||||
intLongHashMap.get(150);
|
||||
|
||||
IntObjectMap<BigDecimal> intObjectMap = new IntObjectHashMap<BigDecimal>();
|
||||
intObjectMap.put(1, BigDecimal.valueOf(1));
|
||||
intObjectMap.put(2, BigDecimal.valueOf(2500));
|
||||
|
||||
BigDecimal value = intObjectMap.get(2);
|
||||
|
||||
//Scatter maps
|
||||
IntLongScatterMap intLongScatterMap = new IntLongScatterMap();
|
||||
intLongScatterMap.put(1, 1L);
|
||||
intLongScatterMap.put(2, -2L);
|
||||
intLongScatterMap.put(1000,0L);
|
||||
|
||||
intLongScatterMap.get(1000);
|
||||
|
||||
IntObjectScatterMap<BigDecimal> intObjectScatterMap = new IntObjectScatterMap<BigDecimal>();
|
||||
intObjectScatterMap.put(1, BigDecimal.valueOf(1));
|
||||
intObjectScatterMap.put(2, BigDecimal.valueOf(2500));
|
||||
|
||||
value = intObjectScatterMap.get(2);
|
||||
}
|
||||
|
||||
|
||||
private static void fastutilMap() {
|
||||
Int2BooleanMap int2BooleanMap = new Int2BooleanOpenHashMap();
|
||||
int2BooleanMap.put(1, true);
|
||||
@@ -32,13 +71,19 @@ public class PrimitiveMaps {
|
||||
|
||||
boolean value = int2BooleanMap.get(1);
|
||||
|
||||
Int2BooleanSortedMap int2BooleanSorted = Int2BooleanSortedMaps.EMPTY_MAP;
|
||||
}
|
||||
//Lambda style iteration
|
||||
Int2BooleanMaps.fastForEach(int2BooleanMap, entry -> {
|
||||
System.out.println(String.format("Key: %d, Value: %b",entry.getIntKey(),entry.getBooleanValue()));
|
||||
});
|
||||
|
||||
//Iterator based loop
|
||||
ObjectIterator<Int2BooleanMap.Entry> iterator = Int2BooleanMaps.fastIterator(int2BooleanMap);
|
||||
while(iterator.hasNext()) {
|
||||
Int2BooleanMap.Entry entry = iterator.next();
|
||||
System.out.println(String.format("Key: %d, Value: %b",entry.getIntKey(),entry.getBooleanValue()));
|
||||
|
||||
}
|
||||
|
||||
private static void coltMap() {
|
||||
AbstractIntDoubleMap map = new OpenIntDoubleHashMap();
|
||||
map.put(1, 4.5);
|
||||
double value = map.get(1);
|
||||
}
|
||||
|
||||
private static void eclipseCollectionsMap() {
|
||||
@@ -53,17 +98,5 @@ public class PrimitiveMaps {
|
||||
dObject.addToValue("stability", 0.8);
|
||||
}
|
||||
|
||||
private static void troveMap() {
|
||||
double[] doubles = new double[] {1.2, 4.5, 0.3};
|
||||
int[] ints = new int[] {1, 4, 0};
|
||||
|
||||
TDoubleIntMap doubleIntMap = new TDoubleIntHashMap(doubles, ints);
|
||||
|
||||
doubleIntMap.put(1.2, 22);
|
||||
doubleIntMap.put(4.5, 16);
|
||||
|
||||
doubleIntMap.adjustValue(1.2, 1);
|
||||
doubleIntMap.adjustValue(4.5, 4);
|
||||
doubleIntMap.adjustValue(0.3, 7);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#Core Java Console
|
||||
|
||||
[Read and Write User Input in Java](http://www.baeldung.com/java-console-input-output)
|
||||
[Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
|
||||
[ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
||||
### Relevant Articles:
|
||||
|
||||
- [Read and Write User Input in Java](http://www.baeldung.com/java-console-input-output)
|
||||
- [Formatting with printf() in Java](https://www.baeldung.com/java-printstream-printf)
|
||||
- [ASCII Art in Java](http://www.baeldung.com/ascii-art-in-java)
|
||||
- [System.console() vs. System.out](https://www.baeldung.com/java-system-console-vs-system-out)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [NoSuchMethodError in Java](https://www.baeldung.com/java-nosuchmethod-error)
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.exceptions</groupId>
|
||||
<artifactId>core-java-exceptions-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-exceptions-3</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.exceptions.nosuchmethoderror;
|
||||
|
||||
public class MainMenu {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Today's Specials: " + getSpecials());
|
||||
}
|
||||
|
||||
public static String getSpecials() {
|
||||
return SpecialToday.getDesert();
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.exceptions.nosuchmethoderror;
|
||||
public class SpecialToday {
|
||||
private static String desert = "Chocolate Cake";
|
||||
|
||||
public static String getDesert() {
|
||||
return desert;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.exceptions.nosuchmethoderror;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MainMenuUnitTest {
|
||||
|
||||
@Test
|
||||
void whenGetSpecials_thenNotNull() {
|
||||
assertNotNull(MainMenu.getSpecials());
|
||||
}
|
||||
}
|
||||
@@ -6,4 +6,7 @@ This module contains articles about core Java input and output (IO)
|
||||
|
||||
- [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)
|
||||
- [Check If a File or Directory Exists in Java](https://www.baeldung.com/java-file-directory-exists)
|
||||
- [Copy a Directory in Java](https://www.baeldung.com/java-copy-directory)
|
||||
- [Java Files Open Options](https://www.baeldung.com/java-file-options)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.linenumber;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LineAtGivenNumberUnitTest {
|
||||
|
||||
private static final String FILE_PATH = "src/test/resources/linesInput.txt";
|
||||
|
||||
@Test
|
||||
public void givenFile_whenUsingBufferedReader_thenExtractedLineIsCorrect() throws IOException {
|
||||
try (BufferedReader br = Files.newBufferedReader(Paths.get(FILE_PATH))) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
br.readLine();
|
||||
}
|
||||
|
||||
String extractedLine = br.readLine();
|
||||
assertEquals("Line 4", extractedLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenUsingScanner_thenExtractedLineIsCorrect() throws IOException {
|
||||
try (Scanner scanner = new Scanner(new File(FILE_PATH))) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
scanner.nextLine();
|
||||
}
|
||||
|
||||
String extractedLine = scanner.nextLine();
|
||||
assertEquals("Line 4", extractedLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSmallFile_whenUsingFilesAPI_thenExtractedLineIsCorrect() throws IOException {
|
||||
String extractedLine = Files.readAllLines(Paths.get(FILE_PATH)).get(4);
|
||||
|
||||
assertEquals("Line 5", extractedLine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLargeFile_whenUsingFilesAPI_thenExtractedLineIsCorrect() throws IOException {
|
||||
try (Stream<String> lines = Files.lines(Paths.get(FILE_PATH))) {
|
||||
String extractedLine = lines.skip(4).findFirst().get();
|
||||
|
||||
assertEquals("Line 5", extractedLine);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenUsingFileUtils_thenExtractedLineIsCorrect() throws IOException {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
File file = new File(classLoader.getResource("linesInput.txt").getFile());
|
||||
|
||||
List<String> lines = FileUtils.readLines(file, "UTF-8");
|
||||
|
||||
String extractedLine = lines.get(0);
|
||||
assertEquals("Line 1", extractedLine);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFile_whenUsingIOUtils_thenExtractedLineIsCorrect() throws IOException {
|
||||
String fileContent = IOUtils.toString(new FileInputStream(FILE_PATH), StandardCharsets.UTF_8);
|
||||
|
||||
String extractedLine = fileContent.split(System.lineSeparator())[0];
|
||||
assertEquals("Line 1", extractedLine);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
Line 1
|
||||
Line 2
|
||||
Line 3
|
||||
Line 4
|
||||
Line 5
|
||||
@@ -10,4 +10,5 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [boolean and boolean[] Memory Layout in the JVM](https://www.baeldung.com/jvm-boolean-memory-layout)
|
||||
- [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)
|
||||
- [List All Classes Loaded in a Specific Class Loader](https://www.baeldung.com/java-list-classes-class-loader)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.loadedclasslisting;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
printClassesLoadedBy("BOOTSTRAP");
|
||||
printClassesLoadedBy("SYSTEM");
|
||||
printClassesLoadedBy("EXTENSION");
|
||||
}
|
||||
|
||||
private static void printClassesLoadedBy(String classLoaderType) {
|
||||
System.out.println(classLoaderType + " ClassLoader : ");
|
||||
Class<?>[] classes = ListLoadedClassesAgent.listLoadedClasses(classLoaderType);
|
||||
Arrays.asList(classes)
|
||||
.forEach(clazz -> System.out.println(clazz.getCanonicalName()));
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.loadedclasslisting;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
public class ListLoadedClassesAgent {
|
||||
|
||||
private static Instrumentation instrumentation;
|
||||
|
||||
public static void premain(String agentArgs, Instrumentation instrumentation) {
|
||||
ListLoadedClassesAgent.instrumentation = instrumentation;
|
||||
}
|
||||
|
||||
public static Class<?>[] listLoadedClasses(String classLoaderType) {
|
||||
if (instrumentation == null) {
|
||||
throw new IllegalStateException(
|
||||
"ListLoadedClassesAgent is not initialized.");
|
||||
}
|
||||
return instrumentation.getInitiatedClasses(
|
||||
getClassLoader(classLoaderType));
|
||||
}
|
||||
|
||||
private static ClassLoader getClassLoader(String classLoaderType) {
|
||||
ClassLoader classLoader = null;
|
||||
switch (classLoaderType) {
|
||||
case "SYSTEM":
|
||||
classLoader = ClassLoader.getSystemClassLoader();
|
||||
break;
|
||||
case "EXTENSION":
|
||||
classLoader = ClassLoader.getSystemClassLoader().getParent();
|
||||
break;
|
||||
// passing a null value to the Instrumentation : getInitiatedClasses method
|
||||
// defaults to the bootstrap class loader
|
||||
case "BOOTSTRAP":
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return classLoader;
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
Premain-Class: com.baeldung.loadedclasslisting.ListLoadedClassesAgent
|
||||
+1
-1
@@ -37,7 +37,7 @@ public class Human {
|
||||
|
||||
public static int compareByNameThenAge(final Human lhs, final Human rhs) {
|
||||
if (lhs.name.equals(rhs.name)) {
|
||||
return lhs.age - rhs.age;
|
||||
return Integer.compare(lhs.age, rhs.age);
|
||||
} else {
|
||||
return lhs.name.compareTo(rhs.name);
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user