Skip to content

Commit

Permalink
Queue to list conversion (#16499)
Browse files Browse the repository at this point in the history
* Queue to list conversion

* Queue to list conversion

* Queue to list conversion

* Queue to list conversion

* Queue to list conversion

* Queue to list conversion

---------

Co-authored-by: Neetika Khandelwal <kwal.neetika2398@gmail.com>
  • Loading branch information
Neetika23 and Neetika Khandelwal authored May 8, 2024
1 parent 2b0cd01 commit 05fc667
Showing 1 changed file with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.baeldung.queuetolist;

import com.google.common.collect.Lists;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

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

public class QueueToListConvertUnitTest {

private Queue<String> queue;

@BeforeEach
public void beforeEach() {
queue = new LinkedList<>();
queue.add("AA");
queue.add("BB");
queue.add("CC");
}

@Test
public void givenAQueue_whenConvertUsingConstructor_thenReturnArrayList() {
List<String> list = new ArrayList<>(queue);

assertNotNull(list);
assertEquals(queue.size(), list.size());
assertTrue(list.containsAll(queue));
}

@Test
public void givenAQueue_whenConvertUsingAddAllMethod_thenReturnList() {
List<String> list = new ArrayList<>();
list.addAll(queue);

assertNotNull(list);
assertEquals(queue.size(), list.size());
assertTrue(list.containsAll(queue));
}

@Test
public void givenAQueue_whenConvertUsingConstructor_thenReturnLinkedList() {
LinkedList<String> list = new LinkedList<>(queue);

assertNotNull(list);
assertEquals(queue.size(), list.size());
assertTrue(list.containsAll(queue));
}

@Test
public void givenAQueue_whenConvertUsingStream_thenReturnList() {
List<String> list = queue.stream()
.collect(Collectors.toList());

assertNotNull(list);
assertEquals(queue.size(), list.size());
assertTrue(list.containsAll(queue));
}

@Test
public void givenAQueue_whenConvertUsingGuava_thenReturnList() {
List<String> list = Lists.newArrayList(queue);

assertNotNull(list);
assertEquals(queue.size(), list.size());
assertTrue(list.containsAll(queue));
}
}

0 comments on commit 05fc667

Please sign in to comment.