@@ -5,7 +5,7 @@ This module contains articles about Java 10 core features
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java 10 LocalVariable Type-Inference](http://www.baeldung.com/java-10-local-variable-type-inference)
|
||||
- [Guide to Java 10](http://www.baeldung.com/java-10-overview)
|
||||
- [New Features in Java 10](https://www.baeldung.com/java-10-overview)
|
||||
- [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)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
## Relevant Articles:
|
||||
|
||||
|
||||
- [String API Updates in Java 12](https://www.baeldung.com/java12-string-api)
|
||||
- [Java 12 New Features](https://www.baeldung.com/java-12-new-features)
|
||||
- [New Features in Java 12](https://www.baeldung.com/java-12-new-features)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
### Relevant articles:
|
||||
|
||||
- [Java Switch Statement](https://www.baeldung.com/java-switch)
|
||||
- [New Java 13 Features](https://www.baeldung.com/java-13-new-features)
|
||||
- [New Features in Java 13](https://www.baeldung.com/java-13-new-features)
|
||||
|
||||
@@ -10,4 +10,4 @@ This module contains articles about Java 14.
|
||||
- [Helpful NullPointerExceptions in Java 14](https://www.baeldung.com/java-14-nullpointerexception)
|
||||
- [Foreign Memory Access API in Java 14](https://www.baeldung.com/java-foreign-memory-access)
|
||||
- [Java 14 Record Keyword](https://www.baeldung.com/java-record-keyword)
|
||||
- [Java 14 – New Features](https://www.baeldung.com/java-14-new-features)
|
||||
- [New Features in Java 14](https://www.baeldung.com/java-14-new-features)
|
||||
|
||||
@@ -4,7 +4,7 @@ This module contains articles about core Java features that have been introduced
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
|
||||
- [New Features in Java 9](https://www.baeldung.com/new-java-9)
|
||||
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
|
||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import javax.annotation.Generated;
|
||||
|
||||
@RetentionAnnotation
|
||||
@Generated("Available only on source code")
|
||||
public class AnnotatedClass {
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
|
||||
@Target(TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RetentionAnnotation {
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.annotations;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
import static org.hamcrest.core.Is.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class AnnotatedClassUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAnnotationRetentionPolicyRuntime_shouldAccess() {
|
||||
AnnotatedClass anAnnotatedClass = new AnnotatedClass();
|
||||
Annotation[] annotations = anAnnotatedClass.getClass().getAnnotations();
|
||||
assertThat(annotations.length, is(1));
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,4 @@
|
||||
This module contains articles about Java Character Class
|
||||
|
||||
### Relevant Articles:
|
||||
- [Character#isAlphabetic vs Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
||||
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.18.0</assertj.version>
|
||||
<assertj.version>3.19.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ArrayLifoStack<E> implements LifoStack<E> {
|
||||
private final Deque<E> deque = new ArrayDeque<>();
|
||||
|
||||
@Override
|
||||
public void push(E item) {
|
||||
deque.addFirst(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() {
|
||||
return deque.removeFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peek() {
|
||||
return deque.peekFirst();
|
||||
}
|
||||
|
||||
// implementing methods from the Collection interface
|
||||
@Override
|
||||
public int size() {
|
||||
return deque.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return deque.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return deque.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return deque.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return deque.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return deque.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return deque.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return deque.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return deque.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
return deque.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
return deque.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
return deque.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
deque.clear();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface LifoStack<E> extends Collection<E> {
|
||||
|
||||
E peek();
|
||||
|
||||
E pop();
|
||||
|
||||
void push(E item);
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class StackVsDequeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAStack_whenAccessByIndex_thenElementCanBeRead() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am the 1st element."); //index 0
|
||||
myStack.push("I am the 2nd element."); //index 1
|
||||
myStack.push("I am the 3rd element."); //index 2
|
||||
//access by index
|
||||
assertThat(myStack.get(0)).isEqualTo("I am the 1st element.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStack_whenIterate_thenFromBottomToTop() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am at the bottom.");
|
||||
myStack.push("I am in the middle.");
|
||||
myStack.push("I am at the top.");
|
||||
|
||||
Iterator<String> it = myStack.iterator();
|
||||
|
||||
assertThat(it).toIterable().containsExactly(
|
||||
"I am at the bottom.",
|
||||
"I am in the middle.",
|
||||
"I am at the top.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStack_whenAddOrRemoveByIndex_thenElementCanBeAddedOrRemoved() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am the 1st element.");
|
||||
myStack.push("I am the 3rd element.");
|
||||
|
||||
assertThat(myStack.size()).isEqualTo(2);
|
||||
|
||||
//insert by index
|
||||
myStack.add(1, "I am the 2nd element.");
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
assertThat(myStack.get(1)).isEqualTo("I am the 2nd element.");
|
||||
//remove by index
|
||||
myStack.remove(1);
|
||||
assertThat(myStack.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenADeque_whenAddOrRemoveLastElement_thenTheLastElementCanBeAddedOrRemoved() {
|
||||
Deque<String> myStack = new ArrayDeque<>();
|
||||
myStack.push("I am the 1st element.");
|
||||
myStack.push("I am the 2nd element.");
|
||||
myStack.push("I am the 3rd element.");
|
||||
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
|
||||
//insert element to the bottom of the stack
|
||||
myStack.addLast("I am the NEW element.");
|
||||
assertThat(myStack.size()).isEqualTo(4);
|
||||
assertThat(myStack.peek()).isEqualTo("I am the 3rd element.");
|
||||
|
||||
//remove element from the bottom of the stack
|
||||
String removedStr = myStack.removeLast();
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
assertThat(removedStr).isEqualTo("I am the NEW element.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenADeque_whenIterate_thenFromTopToBottom() {
|
||||
Deque<String> myStack = new ArrayDeque<>();
|
||||
myStack.push("I am at the bottom.");
|
||||
myStack.push("I am in the middle.");
|
||||
myStack.push("I am at the top.");
|
||||
|
||||
Iterator<String> it = myStack.iterator();
|
||||
|
||||
assertThat(it).toIterable().containsExactly(
|
||||
"I am at the top.",
|
||||
"I am in the middle.",
|
||||
"I am at the bottom.");
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,5 @@ This module contains articles about Map data structures in Java.
|
||||
- [Comparing Two HashMaps in Java](https://www.baeldung.com/java-compare-hashmaps)
|
||||
- [The Map.computeIfAbsent() Method](https://www.baeldung.com/java-map-computeifabsent)
|
||||
- [Collections.synchronizedMap vs. ConcurrentHashMap](https://www.baeldung.com/java-synchronizedmap-vs-concurrenthashmap)
|
||||
- [Java HashMap Load Factor](https://www.baeldung.com/java-hashmap-load-factor)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-maps-2)
|
||||
|
||||
@@ -20,6 +20,11 @@
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.map.propertieshashmap;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class PropertiesToHashMapConverter {
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public static HashMap<String, String> typeCastConvert(Properties prop) {
|
||||
Map step1 = prop;
|
||||
Map<String, String> step2 = (Map<String, String>) step1;
|
||||
return new HashMap<>(step2);
|
||||
}
|
||||
|
||||
public static HashMap<String, String> loopConvert(Properties prop) {
|
||||
HashMap<String, String> retMap = new HashMap<>();
|
||||
for (Map.Entry<Object, Object> entry : prop.entrySet()) {
|
||||
retMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
|
||||
}
|
||||
return retMap;
|
||||
}
|
||||
|
||||
public static HashMap<String, String> streamConvert(Properties prop) {
|
||||
return prop.entrySet().stream().collect(
|
||||
Collectors.toMap(
|
||||
e -> String.valueOf(e.getKey()),
|
||||
e -> String.valueOf(e.getValue()),
|
||||
(prev, next) -> next, HashMap::new
|
||||
));
|
||||
}
|
||||
|
||||
public static HashMap<String, String> guavaConvert(Properties prop) {
|
||||
return Maps.newHashMap(Maps.fromProperties(prop));
|
||||
}
|
||||
}
|
||||
+192
@@ -0,0 +1,192 @@
|
||||
package com.baeldung.map.propertieshashmap;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PropertiesToHashMapConverterUnitTest {
|
||||
|
||||
private Properties properties;
|
||||
|
||||
private final static String propertyFileName = "toHashMap.properties";
|
||||
|
||||
@BeforeEach
|
||||
public void setup() throws IOException {
|
||||
properties = new Properties();
|
||||
try (InputStream is = getClass().getClassLoader().getResourceAsStream(propertyFileName)) {
|
||||
if (is != null) {
|
||||
properties.load(is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingPropertiesLoaded_whenCheck_thenEquals() {
|
||||
assertEquals(3, properties.size());
|
||||
assertEquals("str_value", properties.get("property1"));
|
||||
assertEquals("123", properties.get("property2"));
|
||||
assertEquals("", properties.get("property3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPropertiesModified_thenTypeSafeIssues() {
|
||||
compromiseProperties(properties);
|
||||
|
||||
assertEquals(5, properties.size());
|
||||
|
||||
assertNull(properties.getProperty("property4"));
|
||||
assertNotEquals(String.class, properties.get("property4").getClass());
|
||||
assertEquals(456, properties.get("property4"));
|
||||
|
||||
|
||||
assertNull(properties.getProperty("5"));
|
||||
assertNotEquals(String.class, properties.get(5).getClass());
|
||||
assertEquals(10.11, properties.get(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingNonModifiedProperties_whenTypeCastConvert_thenNoTypeSafeIssues() {
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.typeCastConvert(properties);
|
||||
|
||||
assertEquals(3, hMap.size());
|
||||
assertEquals(String.class, hMap.get("property1").getClass());
|
||||
assertEquals(properties.get("property1"), hMap.get("property1"));
|
||||
assertEquals(String.class, hMap.get("property2").getClass());
|
||||
assertEquals(properties.get("property2"), hMap.get("property2"));
|
||||
assertEquals(String.class, hMap.get("property3").getClass());
|
||||
assertEquals(properties.get("property3"), hMap.get("property3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedProperties_whenTypeCastConvert_thenClassCastException() {
|
||||
compromiseProperties(properties);
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.typeCastConvert(properties);
|
||||
assertEquals(5, hMap.size());
|
||||
|
||||
assertThrows(ClassCastException.class, () -> {
|
||||
String s = hMap.get("property4");
|
||||
});
|
||||
assertEquals(Integer.class, ((Object) hMap.get("property4")).getClass());
|
||||
|
||||
assertNull(hMap.get("5"));
|
||||
assertNotNull(hMap.get(5));
|
||||
assertThrows(ClassCastException.class, () -> {
|
||||
String s = hMap.get(5);
|
||||
});
|
||||
assertEquals(Double.class, ((Object) hMap.get(5)).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingNonModifiedProperties_whenLoopConvert_thenNoTypeSafeIssues() {
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.loopConvert(properties);
|
||||
|
||||
assertEquals(3, hMap.size());
|
||||
assertEquals(String.class, hMap.get("property1").getClass());
|
||||
assertEquals(properties.get("property1"), hMap.get("property1"));
|
||||
assertEquals(String.class, hMap.get("property2").getClass());
|
||||
assertEquals(properties.get("property2"), hMap.get("property2"));
|
||||
assertEquals(String.class, hMap.get("property3").getClass());
|
||||
assertEquals(properties.get("property3"), hMap.get("property3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedProperties_whenLoopConvert_thenNoClassCastException() {
|
||||
compromiseProperties(properties);
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.loopConvert(properties);
|
||||
assertEquals(5, hMap.size());
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
String s = hMap.get("property4");
|
||||
});
|
||||
assertEquals(String.class, hMap.get("property4").getClass());
|
||||
assertEquals("456", hMap.get("property4"));
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
String s = hMap.get("5");
|
||||
});
|
||||
assertEquals("10.11", hMap.get("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingNonModifiedProperties_whenStreamConvert_thenNoTypeSafeIssues() {
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.streamConvert(properties);
|
||||
|
||||
assertEquals(3, hMap.size());
|
||||
assertEquals(String.class, hMap.get("property1").getClass());
|
||||
assertEquals(properties.get("property1"), hMap.get("property1"));
|
||||
assertEquals(String.class, hMap.get("property2").getClass());
|
||||
assertEquals(properties.get("property2"), hMap.get("property2"));
|
||||
assertEquals(String.class, hMap.get("property3").getClass());
|
||||
assertEquals(properties.get("property3"), hMap.get("property3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedProperties_whenStreamConvert_thenNoClassCastException() {
|
||||
compromiseProperties(properties);
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.streamConvert(properties);
|
||||
assertEquals(5, hMap.size());
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
String s = hMap.get("property4");
|
||||
});
|
||||
assertEquals(String.class, hMap.get("property4").getClass());
|
||||
assertEquals("456", hMap.get("property4"));
|
||||
|
||||
assertDoesNotThrow(() -> {
|
||||
String s = hMap.get("5");
|
||||
});
|
||||
assertEquals("10.11", hMap.get("5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedProperties_whenLoopConvertAndStreamConvert_thenHashMapsSame() {
|
||||
compromiseProperties(properties);
|
||||
HashMap<String, String> hMap1 = PropertiesToHashMapConverter.loopConvert(properties);
|
||||
HashMap<String, String> hMap2 = PropertiesToHashMapConverter.streamConvert(properties);
|
||||
|
||||
assertEquals(hMap2, hMap1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingNonModifiedProperties_whenGuavaConvert_thenNoTypeSafeIssues() {
|
||||
HashMap<String, String> hMap = PropertiesToHashMapConverter.guavaConvert(properties);
|
||||
|
||||
assertEquals(3, hMap.size());
|
||||
assertEquals(String.class, hMap.get("property1").getClass());
|
||||
assertEquals(properties.get("property1"), hMap.get("property1"));
|
||||
assertEquals(String.class, hMap.get("property2").getClass());
|
||||
assertEquals(properties.get("property2"), hMap.get("property2"));
|
||||
assertEquals(String.class, hMap.get("property3").getClass());
|
||||
assertEquals(properties.get("property3"), hMap.get("property3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedProperties_whenGuavaConvert_thenUnableToConvertAndThrowException() {
|
||||
compromiseProperties(properties);
|
||||
assertThrows(Exception.class, () -> PropertiesToHashMapConverter.guavaConvert(properties));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedPropertiesWithNoIntegerValue_whenGuavaConvert_thenNullPointerException() {
|
||||
properties.put("property4", 456);
|
||||
assertThrows(NullPointerException.class, () -> PropertiesToHashMapConverter.guavaConvert(properties));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void havingModifiedPropertiesWithNoIntegerKey_whenGuavaConvert_thenClassCastException() {
|
||||
properties.put(5, 10.11);
|
||||
assertThrows(ClassCastException.class, () -> PropertiesToHashMapConverter.guavaConvert(properties));
|
||||
}
|
||||
|
||||
|
||||
private void compromiseProperties(Properties prop) {
|
||||
prop.put("property4", 456);
|
||||
prop.put(5, 10.11);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
property1=str_value
|
||||
property2=123
|
||||
property3=
|
||||
@@ -17,7 +17,7 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.servicemodule</groupId>
|
||||
<artifactId>servicemodule</artifactId>
|
||||
<artifactId>servicemodule2</artifactId>
|
||||
<version>${servicemodule.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -41,4 +41,4 @@
|
||||
<providermodule.version>1.0</providermodule.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -11,4 +11,5 @@ This module contains articles about core features in the Java language
|
||||
- [The transient Keyword in Java](https://www.baeldung.com/java-transient-keyword)
|
||||
- [How to Access an Iteration Counter in a For Each Loop](https://www.baeldung.com/java-foreach-counter)
|
||||
- [Comparing Doubles in Java](https://www.baeldung.com/java-comparing-doubles)
|
||||
- [Guide to Implementing the compareTo Method](https://www.baeldung.com/java-compareto)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-2)
|
||||
|
||||
@@ -4,6 +4,5 @@
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Calculate Factorial in Java](https://www.baeldung.com/java-calculate-factorial)
|
||||
- [Evaluating a Math Expression in Java](https://www.baeldung.com/java-evaluate-math-expression-string)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-math-2)
|
||||
|
||||
@@ -8,3 +8,4 @@ This module contains articles about generics in Java
|
||||
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
|
||||
- [Super Type Tokens in Java Generics](https://www.baeldung.com/java-super-type-tokens)
|
||||
- [Java Warning “unchecked conversion”](https://www.baeldung.com/java-unchecked-conversion)
|
||||
- [Java Warning “Unchecked Cast”](https://www.baeldung.com/java-warning-unchecked-cast)
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.uncheckedcast;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class UncheckedCast {
|
||||
public static Map getRawMap() {
|
||||
Map rawMap = new HashMap();
|
||||
rawMap.put("date 1", LocalDate.of(2021, Month.FEBRUARY, 10));
|
||||
rawMap.put("date 2", LocalDate.of(1992, Month.AUGUST, 8));
|
||||
rawMap.put("date 3", LocalDate.of(1976, Month.NOVEMBER, 18));
|
||||
return rawMap;
|
||||
}
|
||||
|
||||
public static Map getRawMapWithMixedTypes() {
|
||||
Map rawMap = new HashMap();
|
||||
rawMap.put("date 1", LocalDate.of(2021, Month.FEBRUARY, 10));
|
||||
rawMap.put("date 2", LocalDate.of(1992, Month.AUGUST, 8));
|
||||
rawMap.put("date 3", LocalDate.of(1976, Month.NOVEMBER, 18));
|
||||
rawMap.put("date 4", new Date());
|
||||
return rawMap;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.uncheckedcast;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.Map;
|
||||
|
||||
public class UncheckedCastUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenRawMap_whenCastToTypedMap_shouldHaveCompilerWarning() {
|
||||
Map<String, LocalDate> castFromRawMap = (Map<String, LocalDate>) UncheckedCast.getRawMap();
|
||||
Assert.assertEquals(3, castFromRawMap.size());
|
||||
Assert.assertEquals(castFromRawMap.get("date 2"), LocalDate.of(1992, Month.AUGUST, 8));
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenMixTypedRawMap_whenCastToTypedMap_shouldThrowClassCastException() {
|
||||
Map<String, LocalDate> castFromRawMap = (Map<String, LocalDate>) UncheckedCast.getRawMapWithMixedTypes();
|
||||
Assert.assertEquals(4, castFromRawMap.size());
|
||||
Assert.assertTrue(castFromRawMap.get("date 4").isAfter(castFromRawMap.get("date 3")));
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,12 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.6.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.selector;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.Pipe;
|
||||
import java.nio.channels.SelectableChannel;
|
||||
import java.nio.channels.Selector;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import static java.nio.channels.SelectionKey.OP_READ;
|
||||
|
||||
public class SelectorManualTest {
|
||||
|
||||
@Test
|
||||
public void whenWakeUpCalledOnSelector_thenBlockedThreadReturns() throws IOException, InterruptedException {
|
||||
Pipe pipe = Pipe.open();
|
||||
|
||||
Selector selector = Selector.open();
|
||||
SelectableChannel channel = pipe.source();
|
||||
channel.configureBlocking(false);
|
||||
channel.register(selector, OP_READ);
|
||||
|
||||
List<String> invocationStepsTracker = Collections.synchronizedList(new ArrayList<>());
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
Thread thread = new Thread(() -> {
|
||||
invocationStepsTracker.add(">> Count down");
|
||||
latch.countDown();
|
||||
try {
|
||||
invocationStepsTracker.add(">> Start select");
|
||||
selector.select();
|
||||
invocationStepsTracker.add(">> End select");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
invocationStepsTracker.add(">> Start await");
|
||||
thread.start();
|
||||
latch.await();
|
||||
invocationStepsTracker.add(">> End await");
|
||||
|
||||
invocationStepsTracker.add(">> Wakeup thread");
|
||||
|
||||
selector.wakeup();
|
||||
channel.close();
|
||||
|
||||
assertThat(invocationStepsTracker)
|
||||
.containsExactly(
|
||||
">> Start await",
|
||||
">> Count down",
|
||||
">> Start select",
|
||||
">> End await",
|
||||
">> Wakeup thread",
|
||||
">> End select"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
- [Introduction to Docker Compose](https://www.baeldung.com/docker-compose)
|
||||
- [Reusing Docker Layers with Spring Boot](https://www.baeldung.com/docker-layers-spring-boot)
|
||||
- [Running Spring Boot with PostgreSQL in Docker Compose](https://www.baeldung.com/spring-boot-postgresql-docker)
|
||||
- [How To Configure Java Heap Size Inside a Docker Container](https://www.baeldung.com/ops/docker-jvm-heap-size)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
FROM openjdk:8u92-jdk-alpine
|
||||
COPY /src /src/
|
||||
RUN mkdir /app && ls /src && javac /src/main/java/com/baeldung/docker/heapsizing/PrintXmxXms.java -d /app
|
||||
CMD java -version && java $JAVA_OPTS -cp /app com.baeldung.docker.heapsizing.PrintXmxXms
|
||||
@@ -0,0 +1,61 @@
|
||||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.2</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.baeldung.docker</groupId>
|
||||
<artifactId>heap-sizing</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>heap-sizing</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<image>
|
||||
<name>heapsizing-demo</name>
|
||||
</image>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.google.cloud.tools</groupId>
|
||||
<artifactId>jib-maven-plugin</artifactId>
|
||||
<version>2.7.1</version>
|
||||
|
||||
<configuration>
|
||||
<to>
|
||||
<image>heapsizing-demo-jib</image>
|
||||
</to>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.docker.heapsizing;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static com.baeldung.docker.heapsizing.PrintXmxXms.logMemory;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HeapSizingApplication {
|
||||
private static final Logger logger = Logger.getLogger(HeapSizingApplication.class.getName());
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HeapSizingApplication.class, args);
|
||||
logMemory(logger);
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user