forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create MethodsToInitializeLinkedList.java (eugenp#17924)
* Create MethodsToInitializeLinkedList.java * Create LinkedListJUnitTest.java * Update LinkedListJUnitTest.java * Update and rename LinkedListJUnitTest.java to LinkedListJUnitTest.java * Update and rename core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/java/list/MethodsToInitializeLinkedList.java to core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/linkedlist/MethodsToInitializeLinkedList.java
- Loading branch information
1 parent
a7c2366
commit 68cd562
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...llections-list-2/src/main/java/com/baeldung/linkedlist/MethodsToInitializeLinkedList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.baeldung.linkedlist; | ||
|
||
import java.util.LinkedList; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Demonstrates the different methods to | ||
* initialize a LinkedList. | ||
*/ | ||
public class MethodsToInitializeLinkedList { | ||
|
||
|
||
/** | ||
* Initialize an Empty List | ||
*/ | ||
public void initializeEmptyList() { | ||
LinkedList<String> linkedList=new LinkedList<String>(); | ||
|
||
linkedList.addFirst("one"); | ||
linkedList.add("two"); | ||
linkedList.add("three"); | ||
|
||
System.out.println(linkedList); | ||
} | ||
|
||
/** | ||
* Initialize a List from a Collection | ||
*/ | ||
public void initializeListFromCollection() { | ||
ArrayList<Integer> arrayList=new ArrayList<Integer>(3); | ||
|
||
arrayList.add(Integer.valueOf(1)); | ||
arrayList.add(Integer.valueOf(2)); | ||
arrayList.add(Integer.valueOf(3)); | ||
|
||
LinkedList<Integer> linkedList=new LinkedList<Integer>(arrayList); | ||
|
||
System.out.println(linkedList); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...re-java-collections-list-2/src/test/java/com/baeldung/linkedlist/LinkedListJUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.baeldung.linkedlist; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
|
||
public class LinkedListJUnitTest { | ||
|
||
@Test | ||
public void whenInitializingLinkedList_ShouldReturnEmptyList() throws Exception { | ||
LinkedList<String> linkedList=new LinkedList<String>(); | ||
|
||
Assertions.assertTrue(linkedList.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void whenInitializingListFromCollection_ShouldReturnCollectionsElements() throws Exception { | ||
ArrayList<Integer> arrayList=new ArrayList<Integer>(3); | ||
|
||
arrayList.add(Integer.valueOf(1)); | ||
arrayList.add(Integer.valueOf(2)); | ||
arrayList.add(Integer.valueOf(3)); | ||
|
||
LinkedList<Integer> linkedList=new LinkedList<Integer>(arrayList); | ||
|
||
Object[] linkedListElements = linkedList.toArray(); | ||
Object[] collectionElements = arrayList.toArray(); | ||
|
||
Assertions.assertArrayEquals(linkedListElements,collectionElements); | ||
} | ||
} |