Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.entitycollection;

import java.util.*;
import java.util.stream.Collectors;

public class IdExtractor {

public static List<Long> extractIdsClassic(List<User> users) {
List<Long> ids = new ArrayList<>();
for (User user : users) {
ids.add(user.getId());
}
return ids;
}

public static List<Long> extractIdsStream(List<User> users) {
return users.stream()
.map(User::getId)
.collect(Collectors.toList());
}

public static Set<Long> extractUniqueIds(List<User> users) {
return users.stream()
.map(User::getId)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.entitycollection;

public class User {
private final Long id;
private final String name;

public User(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.baeldung.entitycollection;

import org.junit.jupiter.api.Test;

import java.util.*;

import static org.junit.jupiter.api.Assertions.*;

class IdExtractionUnitTest {

@Test
void givenListOfUsers_whenUsingClassicLoop_thenReturnListOfIds() {
List<User> users = Arrays.asList(
new User(1L, "A"),
new User(2L, "B"),
new User(3L, "C")
);

List<Long> ids = IdExtractor.extractIdsClassic(users);

assertEquals(Arrays.asList(1L, 2L, 3L), ids);
}

@Test
void givenEmptyUsersList_whenUsingClassicLoop_thenReturnEmptyList() {
List<User> users = List.of();
List<Long> ids = IdExtractor.extractIdsClassic(users);
assertTrue(ids.isEmpty());
}

@Test
void givenUsersList_whenUsingStream_thenReturnListOfIds() {
List<User> users = Arrays.asList(
new User(10L, "A"),
new User(20L, "B")
);

List<Long> ids = IdExtractor.extractIdsStream(users);
assertEquals(Arrays.asList(10L, 20L), ids);
}

@Test
void givenEmptyUsersList_whenUsingStream_thenReturnEmptyList() {
List<User> users = List.of();
List<Long> ids = IdExtractor.extractIdsStream(users);
assertTrue(ids.isEmpty());
}

@Test
void givenUsersWithNullIds_whenUsingStream_thenAllowNullValuesInList() {
List<User> users = Arrays.asList(
new User(null, "A"),
new User(5L, "B")
);

List<Long> ids = IdExtractor.extractIdsStream(users);
assertEquals(Arrays.asList(null, 5L), ids);
}

@Test
void givenUsersWithDuplicateIds_whenUsingUniqueIdExtractor_thenReturnUniqueSet() {
List<User> users = Arrays.asList(
new User(1L, "A"),
new User(1L, "B"),
new User(2L, "C")
);

Set<Long> ids = IdExtractor.extractUniqueIds(users);
assertEquals(Set.of(1L, 2L), ids);
}

@Test
void givenEmptyUsersList_whenUsingUniqueIdExtractor_thenReturnEmptySet() {
List<User> users = List.of();
Set<Long> ids = IdExtractor.extractUniqueIds(users);
assertTrue(ids.isEmpty());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.baeldung.jsonarray;

import static org.junit.jupiter.api.Assertions.*;

import org.json.JSONArray;
import org.junit.jupiter.api.Test;

import java.util.stream.IntStream;


class JsonArrayToIntArrayUnitTest {

@Test
void givenJsonArray_whenUsingLoop_thenIntArrayIsReturned() {
JSONArray jsonArray = new JSONArray("[1, 2, 3]");
int[] result = new int[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
result[i] = jsonArray.getInt(i);
}

assertArrayEquals(new int[]{1, 2, 3}, result);
}

@Test
void givenJsonArray_whenUsingStreams_thenIntArrayIsReturned() {
JSONArray jsonArray = new JSONArray("[10, 20, 30]");
int[] result = IntStream.range(0, jsonArray.length())
.map(jsonArray::getInt)
.toArray();

assertArrayEquals(new int[]{10, 20, 30}, result);
}

@Test
void givenNullJsonArray_whenConvertingSafely_thenEmptyArrayIsReturned() {
int[] result = toIntArraySafely(null);
assertEquals(0, result.length);
}

@Test
void givenEmptyJsonArray_whenConvertingSafely_thenEmptyArrayIsReturned() {
JSONArray jsonArray = new JSONArray();
int[] result = toIntArraySafely(jsonArray);
assertEquals(0, result.length);
}

private int[] toIntArraySafely(JSONArray jsonArray) {
if (jsonArray == null || jsonArray.isEmpty()) {
return new int[0];
}

int[] result = new int[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
result[i] = jsonArray.getInt(i);
}
return result;
}
}