@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
+5
-5
@@ -26,7 +26,7 @@ public class Product {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public Product addTagsOfOtherProdcut(Product product) {
|
||||
public Product addTagsOfOtherProduct(Product product) {
|
||||
this.tags.addAll(product.getTags());
|
||||
return this;
|
||||
}
|
||||
@@ -100,11 +100,11 @@ public class Product {
|
||||
HashMap<String, Product> productsByName = new HashMap<>();
|
||||
Product eBike2 = new Product("E-Bike", "A bike with a battery");
|
||||
eBike2.getTags().add("sport");
|
||||
productsByName.merge("E-Bike", eBike2, Product::addTagsOfOtherProdcut);
|
||||
productsByName.merge("E-Bike", eBike2, Product::addTagsOfOtherProduct);
|
||||
|
||||
//Prior to Java 8:
|
||||
if(productsByName.containsKey("E-Bike")) {
|
||||
productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2);
|
||||
productsByName.get("E-Bike").addTagsOfOtherProduct(eBike2);
|
||||
} else {
|
||||
productsByName.put("E-Bike", eBike2);
|
||||
}
|
||||
@@ -117,7 +117,7 @@ public class Product {
|
||||
|
||||
productsByName.compute("E-Bike", (k,v) -> {
|
||||
if(v != null) {
|
||||
return v.addTagsOfOtherProdcut(eBike2);
|
||||
return v.addTagsOfOtherProduct(eBike2);
|
||||
} else {
|
||||
return eBike2;
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public class Product {
|
||||
|
||||
//Prior to Java 8:
|
||||
if(productsByName.containsKey("E-Bike")) {
|
||||
productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2);
|
||||
productsByName.get("E-Bike").addTagsOfOtherProduct(eBike2);
|
||||
} else {
|
||||
productsByName.put("E-Bike", eBike2);
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.map.hashmap.loadfactor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class HashMapLoadFactorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCreateMapWithDefaultParam_thenSucces() {
|
||||
Map<String, String> mapWithDefaultParams = new HashMap<>();
|
||||
mapWithDefaultParams.put("1", "one");
|
||||
mapWithDefaultParams.put("2", "two");
|
||||
mapWithDefaultParams.put("3", "three");
|
||||
mapWithDefaultParams.put("4", "four");
|
||||
mapWithDefaultParams.put("5", "five");
|
||||
|
||||
Assert.assertEquals(5, mapWithDefaultParams.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateMapWithInitialCapacity_thenSucces() {
|
||||
Map<String, String> mapWithInitialCapacity = new HashMap<>(5);
|
||||
mapWithInitialCapacity.put("1", "one");
|
||||
mapWithInitialCapacity.put("2", "two");
|
||||
mapWithInitialCapacity.put("3", "three");
|
||||
|
||||
Assert.assertEquals(3, mapWithInitialCapacity.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateMapWithInitialCapacityAndLF_thenSucces() {
|
||||
Map<String, String> mapWithInitialCapacityAndLF = new HashMap<>(5, 0.5f);
|
||||
mapWithInitialCapacityAndLF.put("1", "one");
|
||||
mapWithInitialCapacityAndLF.put("2", "two");
|
||||
mapWithInitialCapacityAndLF.put("3", "three");
|
||||
mapWithInitialCapacityAndLF.put("4", "four");
|
||||
mapWithInitialCapacityAndLF.put("5", "five");
|
||||
mapWithInitialCapacityAndLF.put("6", "six");
|
||||
mapWithInitialCapacityAndLF.put("7", "seven");
|
||||
mapWithInitialCapacityAndLF.put("8", "eight");
|
||||
mapWithInitialCapacityAndLF.put("9", "nine");
|
||||
mapWithInitialCapacityAndLF.put("10", "ten");
|
||||
|
||||
Assert.assertEquals(10, mapWithInitialCapacityAndLF.size());
|
||||
}
|
||||
}
|
||||
+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=
|
||||
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Binary Semaphore vs Reentrant Lock](https://www.baeldung.com/java-binary-semaphore-vs-reentrant-lock)
|
||||
- [Bad Practices With Synchronization](https://www.baeldung.com/java-synchronization-bad-practices)
|
||||
|
||||
@@ -11,4 +11,5 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [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)
|
||||
- [An Introduction to the Constant Pool in the JVM](https://www.baeldung.com/jvm-constant-pool)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
|
||||
@@ -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)
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
public class BankAccount implements Comparable<BankAccount> {
|
||||
|
||||
private final int balance;
|
||||
|
||||
public BankAccount(int balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BankAccount anotherAccount) {
|
||||
return this.balance - anotherAccount.balance;
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
public class BankAccountFix implements Comparable<BankAccountFix> {
|
||||
|
||||
private final int balance;
|
||||
|
||||
public BankAccountFix(int balance) {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(BankAccountFix anotherAccount) {
|
||||
return Integer.compare(this.balance, anotherAccount.balance);
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
public class FootballPlayer implements Comparable<FootballPlayer> {
|
||||
|
||||
private final String name;
|
||||
private final int goalsScored;
|
||||
|
||||
public FootballPlayer(String name, int goalsScored) {
|
||||
this.name = name;
|
||||
this.goalsScored = goalsScored;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(FootballPlayer anotherPlayer) {
|
||||
return Integer.compare(this.goalsScored, anotherPlayer.goalsScored);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object object) {
|
||||
if (this == object)
|
||||
return true;
|
||||
if (object == null || getClass() != object.getClass())
|
||||
return false;
|
||||
FootballPlayer player = (FootballPlayer) object;
|
||||
return name.equals(player.name);
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
public class HandballPlayer {
|
||||
|
||||
private final String name;
|
||||
private final int height;
|
||||
|
||||
public HandballPlayer(String name, int height) {
|
||||
this.name = name;
|
||||
this.height = height;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ArraysSortingUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenArrayOfNumbers_whenSortingArray_thenNumbersAreSortedAscending() {
|
||||
int[] numbers = new int[] {5, 3, 9, 11, 1, 7};
|
||||
Arrays.sort(numbers);
|
||||
assertThat(numbers).containsExactly(1, 3, 5, 7, 9, 11);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayOfStrings_whenSortingArray_thenStringsAreSortedAlphabetically() {
|
||||
String[] players = new String[] {"ronaldo", "modric", "ramos", "messi"};
|
||||
Arrays.sort(players);
|
||||
assertThat(players).containsExactly("messi", "modric", "ramos", "ronaldo");
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BankAccountFixUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenComparisonBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
|
||||
BankAccountFix accountOne = new BankAccountFix(5000);
|
||||
BankAccountFix accountTwo = new BankAccountFix(1000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComparisonBasedImpl_whenUsingLargeIntegers_thenComparisonWorks() {
|
||||
BankAccountFix accountOne = new BankAccountFix(1900000000);
|
||||
BankAccountFix accountTwo = new BankAccountFix(-2000000000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class BankAccountUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSubtractionBasedImpl_whenUsingSmallIntegers_thenComparisonWorks() {
|
||||
BankAccount accountOne = new BankAccount(5000);
|
||||
BankAccount accountTwo = new BankAccount(1000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isPositive();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSubtractionBasedImpl_whenUsingLargeIntegers_thenComparisonBreaks() {
|
||||
BankAccount accountOne = new BankAccount(1900000000);
|
||||
BankAccount accountTwo = new BankAccount(-2000000000);
|
||||
int comparison = accountOne.compareTo(accountTwo);
|
||||
assertThat(comparison).isNegative();
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class FootballPlayerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInconsistentCompareToAndEqualsImpl_whenUsingSortedSet_thenSomeElementsAreNotAdded() {
|
||||
FootballPlayer messi = new FootballPlayer("Messi", 800);
|
||||
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 800);
|
||||
|
||||
TreeSet<FootballPlayer> set = new TreeSet<>();
|
||||
set.add(messi);
|
||||
set.add(ronaldo);
|
||||
|
||||
assertThat(set).hasSize(1);
|
||||
assertThat(set).doesNotContain(ronaldo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompareToImpl_whenUsingCustomComparator_thenComparatorLogicIsApplied() {
|
||||
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
|
||||
FootballPlayer messi = new FootballPlayer("Messi", 800);
|
||||
FootballPlayer modric = new FootballPlayer("Modric", 100);
|
||||
|
||||
List<FootballPlayer> players = Arrays.asList(ronaldo, messi, modric);
|
||||
Comparator<FootballPlayer> nameComparator = Comparator.comparing(FootballPlayer::getName);
|
||||
Collections.sort(players, nameComparator);
|
||||
|
||||
assertThat(players).containsExactly(messi, modric, ronaldo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompareToImpl_whenSavingElementsInTreeMap_thenKeysAreSortedUsingCompareTo() {
|
||||
FootballPlayer ronaldo = new FootballPlayer("Ronaldo", 900);
|
||||
FootballPlayer messi = new FootballPlayer("Messi", 800);
|
||||
FootballPlayer modric = new FootballPlayer("Modric", 100);
|
||||
|
||||
Map<FootballPlayer, String> players = new TreeMap<>();
|
||||
players.put(ronaldo, "forward");
|
||||
players.put(messi, "forward");
|
||||
players.put(modric, "midfielder");
|
||||
|
||||
assertThat(players.keySet()).containsExactly(modric, messi, ronaldo);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.compareto;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
public class HandballPlayerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenComparableIsNotImplemented_whenSortingArray_thenExceptionIsThrown() {
|
||||
HandballPlayer duvnjak = new HandballPlayer("Duvnjak", 197);
|
||||
HandballPlayer hansen = new HandballPlayer("Hansen", 196);
|
||||
|
||||
HandballPlayer[] players = new HandballPlayer[] {duvnjak, hansen};
|
||||
|
||||
assertThatExceptionOfType(ClassCastException.class).isThrownBy(() -> Arrays.sort(players));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -7,3 +7,4 @@ This module contains articles about generics in Java
|
||||
- [Type Erasure in Java Explained](https://www.baeldung.com/java-type-erasure)
|
||||
- [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)
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.reflection.check.abstractclass;
|
||||
|
||||
public interface InterfaceExample {
|
||||
}
|
||||
+23
-3
@@ -1,16 +1,36 @@
|
||||
package com.baeldung.reflection.check.abstractclass;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Date;
|
||||
|
||||
class AbstractExampleUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAbstractClass_whenCheckModifierIsAbstract_thenTrue() throws Exception {
|
||||
void givenAbstractClass_whenCheckModifierIsAbstract_thenTrue() {
|
||||
Class<AbstractExample> clazz = AbstractExample.class;
|
||||
Assertions.assertTrue(Modifier.isAbstract(clazz.getModifiers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInterface_whenCheckModifierIsAbstract_thenTrue() {
|
||||
Class<InterfaceExample> clazz = InterfaceExample.class;
|
||||
Assertions.assertTrue(Modifier.isAbstract(clazz.getModifiers()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAbstractClass_whenCheckIsAbstractClass_thenTrue() {
|
||||
Class<AbstractExample> clazz = AbstractExample.class;
|
||||
int mod = clazz.getModifiers();
|
||||
Assertions.assertTrue(Modifier.isAbstract(mod) && !Modifier.isInterface(mod));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConcreteClass_whenCheckIsAbstractClass_thenFalse() {
|
||||
Class<Date> clazz = Date.class;
|
||||
int mod = clazz.getModifiers();
|
||||
Assertions.assertFalse(Modifier.isAbstract(mod) && !Modifier.isInterface(mod));
|
||||
}
|
||||
}
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.egd;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* JavaSecurityEgdTester - run this with JVM parameter java.security.egd, e.g.:
|
||||
* java -Djava.security.egd=file:/dev/urandom -cp . com.baeldung.egd.JavaSecurityEgdTester
|
||||
*/
|
||||
public class JavaSecurityEgdTester {
|
||||
public static final double NANOSECS = 1000000000.0;
|
||||
public static final String JAVA_SECURITY_EGD = "java.security.egd";
|
||||
|
||||
public static void main(String[] args) {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
long start = System.nanoTime();
|
||||
byte[] randomBytes = new byte[256];
|
||||
secureRandom.nextBytes(randomBytes);
|
||||
double duration = (System.nanoTime() - start) / NANOSECS;
|
||||
|
||||
String message = String.format("java.security.egd=%s took %.3f seconds and used the %s algorithm", System.getProperty(JAVA_SECURITY_EGD), duration, secureRandom.getAlgorithm());
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Kotlin vs Java](https://www.baeldung.com/kotlin/kotlin-vs-java)
|
||||
- [Kotlin vs Java](https://www.baeldung.com/kotlin/vs-java)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user