Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -5,6 +5,33 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-collections-array-list</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${maven-compiler-plugin.source}</source>
|
||||
<target>${maven-compiler-plugin.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
--add-opens java.base/java.util=ALL-UNNAMED
|
||||
</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven-compiler-plugin.source>16</maven-compiler-plugin.source>
|
||||
<maven-compiler-plugin.target>16</maven-compiler-plugin.target>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
<name>core-java-collections-array-list</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
@@ -20,6 +47,12 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.1-jre</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class Node {
|
||||
|
||||
private final int x;
|
||||
private final int y;
|
||||
|
||||
public Node(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Node (" + "x=" + x + ", y=" + y + ')';
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
public class User {
|
||||
private final String fullName;
|
||||
|
||||
public User(String fullName) {
|
||||
this.fullName = fullName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User (" + "full name='" + fullName + ')';
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package com.baeldung.listofobjectstolistofstring;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConvertObjectListToStringListUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenForEachUsedToConvert_thenReturnSuccess() {
|
||||
List<String> outputList = new ArrayList<>(objectListWithNull().size());
|
||||
for (Object obj : objectListWithNull()) {
|
||||
outputList.add(Objects.toString(obj, null));
|
||||
}
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toList());
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingStreamsUnmodifiableListToConvert_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithNull().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.collect(Collectors.toUnmodifiableList());
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectList_whenUsingGuavaTransform_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = Lists.transform(objectListWithNull(), obj -> Objects.toString(obj, null));
|
||||
Assert.assertEquals(expectedStringListWithNull(), outputList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectListWithNoNull_whenUsingToList_thenReturnSuccess() {
|
||||
List<String> outputList;
|
||||
outputList = objectListWithoutNull().stream()
|
||||
.map((obj) -> Objects.toString(obj, null))
|
||||
.toList();
|
||||
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithNull() {
|
||||
List<String> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add("1");
|
||||
listOfStrings.add("true");
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.toString(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4).toString());
|
||||
listOfStrings.add(new User("John Doe").toString());
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<Object> objectListWithNull() {
|
||||
List<Object> listOfStrings = new ArrayList<>();
|
||||
listOfStrings.add(1);
|
||||
listOfStrings.add(true);
|
||||
listOfStrings.add("hello");
|
||||
listOfStrings.add(Double.valueOf(273773.98));
|
||||
listOfStrings.add(null);
|
||||
listOfStrings.add(new Node(2, 4));
|
||||
listOfStrings.add(new User("John Doe"));
|
||||
return listOfStrings;
|
||||
}
|
||||
|
||||
private List<String> expectedStringListWithoutNull() {
|
||||
return List.of("1", "true", "hello", Double.toString(273773.98), new Node(2, 4).toString(), new User("John Doe").toString());
|
||||
}
|
||||
|
||||
private List<Object> objectListWithoutNull() {
|
||||
return List.of(1, true, "hello", Double.valueOf(273773.98), new Node(2, 4), new User("John Doe"));
|
||||
}
|
||||
}
|
||||
+30
-22
@@ -2,68 +2,76 @@ package com.baeldung.convertnumberbases;
|
||||
|
||||
public class ConvertNumberBases {
|
||||
|
||||
public static String convertNumberToNewBase(String number, int base, int newBase){
|
||||
public static String convertNumberToNewBase(String number, int base, int newBase) {
|
||||
return Integer.toString(Integer.parseInt(number, base), newBase);
|
||||
}
|
||||
|
||||
public static String convertNumberToNewBaseCustom(String num, int base, int newBase) {
|
||||
int decimalNumber = convertFromAnyBaseToDecimal(num, base);
|
||||
return convertFromDecimalToBaseX(decimalNumber, newBase);
|
||||
String targetBase = "";
|
||||
try {
|
||||
targetBase = convertFromDecimalToBaseX(decimalNumber, newBase);
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return targetBase;
|
||||
}
|
||||
|
||||
public static String convertFromDecimalToBaseX(int num, int newBase) {
|
||||
public static String convertFromDecimalToBaseX(int num, int newBase) throws IllegalArgumentException {
|
||||
if ((newBase < 2 || newBase > 10) && newBase != 16) {
|
||||
throw new IllegalArgumentException("New base must be from 2 - 10 or 16");
|
||||
}
|
||||
|
||||
String result = "";
|
||||
int remainder;
|
||||
while (num > 0) {
|
||||
remainder = num % newBase;
|
||||
if (newBase == 16) {
|
||||
if (remainder == 10)
|
||||
if (remainder == 10) {
|
||||
result += 'A';
|
||||
else if (remainder == 11)
|
||||
} else if (remainder == 11) {
|
||||
result += 'B';
|
||||
else if (remainder == 12)
|
||||
} else if (remainder == 12) {
|
||||
result += 'C';
|
||||
else if (remainder == 13)
|
||||
} else if (remainder == 13) {
|
||||
result += 'D';
|
||||
else if (remainder == 14)
|
||||
} else if (remainder == 14) {
|
||||
result += 'E';
|
||||
else if (remainder == 15)
|
||||
} else if (remainder == 15) {
|
||||
result += 'F';
|
||||
else
|
||||
} else {
|
||||
result += remainder;
|
||||
} else
|
||||
}
|
||||
} else {
|
||||
result += remainder;
|
||||
|
||||
}
|
||||
num /= newBase;
|
||||
}
|
||||
return new StringBuffer(result).reverse().toString();
|
||||
}
|
||||
|
||||
public static int convertFromAnyBaseToDecimal(String num, int base) {
|
||||
|
||||
if (base < 2 || (base > 10 && base != 16))
|
||||
if (base < 2 || (base > 10 && base != 16)) {
|
||||
return -1;
|
||||
|
||||
}
|
||||
int val = 0;
|
||||
int power = 1;
|
||||
|
||||
for (int i = num.length() - 1; i >= 0; i--) {
|
||||
int digit = charToDecimal(num.charAt(i));
|
||||
|
||||
if (digit < 0 || digit >= base)
|
||||
if (digit < 0 || digit >= base) {
|
||||
return -1;
|
||||
|
||||
}
|
||||
val += digit * power;
|
||||
power = power * base;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public static int charToDecimal(char c) {
|
||||
if (c >= '0' && c <= '9')
|
||||
if (c >= '0' && c <= '9') {
|
||||
return (int) c - '0';
|
||||
else
|
||||
} else {
|
||||
return (int) c - 'A' + 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -1,5 +1,6 @@
|
||||
package com.baeldung.convertnumberbases;
|
||||
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertFromDecimalToBaseX;
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBase;
|
||||
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBaseCustom;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
@@ -17,4 +18,15 @@ class ConvertNumberBasesUnitTest {
|
||||
assertEquals(convertNumberToNewBaseCustom("11001000", 2, 8), "310");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenInputIsOutOfRange_thenIllegalArgumentExceptionIsThrown() {
|
||||
Exception exception = assertThrows(IllegalArgumentException.class, ()-> {
|
||||
convertFromDecimalToBaseX(100, 12);
|
||||
});
|
||||
String expectedMessage = "New base must be from 2 - 10 or 16";
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
assertTrue(actualMessage.contains(expectedMessage));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,6 @@
|
||||
<module>core-java-collections-2</module>
|
||||
<module>core-java-collections-3</module>
|
||||
<module>core-java-collections-4</module>
|
||||
<module>core-java-collections-array-list</module>
|
||||
<module>core-java-collections-conversions</module>
|
||||
<module>core-java-collections-conversions-2</module>
|
||||
<module>core-java-collections-set-2</module>
|
||||
|
||||
Reference in New Issue
Block a user