Skip to content

Commit 45da250

Browse files
author
clininger
committed
BAEL-2184 Converting a Collection to ArrayList
1 parent 46c7340 commit 45da250

2 files changed

Lines changed: 196 additions & 0 deletions

File tree

  • core-java-collections/src
    • main/java/com/baeldung/convertcollectiontoarraylist
    • test/java/com/baeldung/convertcollectiontoarraylist
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.convertcollectiontoarraylist;
2+
3+
/**
4+
* This POJO is the element type of our collection. It has a deepCopy() method.
5+
*
6+
* @author chris
7+
*/
8+
public class Foo {
9+
10+
private int id;
11+
private String name;
12+
private Foo parent;
13+
14+
public Foo() {
15+
}
16+
17+
public Foo(int id, String name, Foo parent) {
18+
this.id = id;
19+
this.name = name;
20+
this.parent = parent;
21+
}
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public void setId(int id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public Foo getParent() {
40+
return parent;
41+
}
42+
43+
public void setParent(Foo parent) {
44+
this.parent = parent;
45+
}
46+
47+
public Foo deepCopy() {
48+
return new Foo(this.id, this.name,
49+
this.parent != null ? this.parent.deepCopy() : null);
50+
}
51+
52+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.baeldung.convertcollectiontoarraylist;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Comparator;
6+
import java.util.HashSet;
7+
import java.util.Iterator;
8+
import static java.util.stream.Collectors.toCollection;
9+
import org.junit.BeforeClass;
10+
import org.junit.Test;
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
*
15+
* @author chris
16+
*/
17+
public class FooUnitTest {
18+
private static Collection<Foo> srcCollection = new HashSet<>();
19+
20+
public FooUnitTest() {
21+
}
22+
23+
@BeforeClass
24+
public static void setUpClass() {
25+
int i = 0;
26+
Foo john = new Foo(i++, "John", null);
27+
Foo mary = new Foo(i++, "Mary", null);
28+
Foo sam = new Foo(i++, "Sam", john);
29+
Foo alice = new Foo(i++, "Alice", john);
30+
Foo buffy = new Foo(i++, "Buffy", sam);
31+
srcCollection.add(john);
32+
srcCollection.add(mary);
33+
srcCollection.add(sam);
34+
srcCollection.add(alice);
35+
srcCollection.add(buffy);
36+
}
37+
38+
/**
39+
* Section 3. Using the ArrayList Constructor
40+
*/
41+
@Test
42+
public void testConstructor() {
43+
ArrayList<Foo> newList = new ArrayList<>(srcCollection);
44+
verifyShallowCopy(srcCollection, newList);
45+
}
46+
47+
/**
48+
* Section 4. Using the Streams API
49+
*/
50+
@Test
51+
public void testStream() {
52+
ArrayList<Foo> newList = srcCollection.stream().collect(toCollection(ArrayList::new));
53+
verifyShallowCopy(srcCollection, newList);
54+
}
55+
56+
/**
57+
* Section 5. Deep Copy
58+
*/
59+
@Test
60+
public void testStreamDeepCopy() {
61+
ArrayList<Foo> newList = srcCollection.stream()
62+
.map(foo -> foo.deepCopy())
63+
.collect(toCollection(ArrayList::new));
64+
verifyDeepCopy(srcCollection, newList);
65+
}
66+
67+
/**
68+
* Section 6. Controlling the List Order
69+
*/
70+
@Test
71+
public void testSortOrder() {
72+
assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection));
73+
ArrayList<Foo> newList = srcCollection.stream()
74+
.map(foo -> foo.deepCopy())
75+
.sorted(Comparator.comparing(Foo::getName))
76+
.collect(toCollection(ArrayList::new));
77+
assertTrue("ArrayList is not sorted by name", isSorted(newList));
78+
}
79+
80+
/**
81+
* Verify that the contents of the two collections are the same
82+
* @param a
83+
* @param b
84+
*/
85+
private void verifyShallowCopy(Collection a, Collection b) {
86+
assertEquals("Collections have different lengths", a.size(), b.size());
87+
Iterator<Foo> iterA = a.iterator();
88+
Iterator<Foo> iterB = b.iterator();
89+
while (iterA.hasNext()) {
90+
// use '==' to test instance identity
91+
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
92+
}
93+
}
94+
95+
/**
96+
* Verify that the contents of the two collections are the same
97+
* @param a
98+
* @param b
99+
*/
100+
private void verifyDeepCopy(Collection a, Collection b) {
101+
assertEquals("Collections have different lengths", a.size(), b.size());
102+
Iterator<Foo> iterA = a.iterator();
103+
Iterator<Foo> iterB = b.iterator();
104+
while (iterA.hasNext()) {
105+
Foo nextA = iterA.next();
106+
Foo nextB = iterB.next();
107+
// should not be same instance
108+
assertFalse("Foo instances are the same!", nextA == nextB);
109+
// but should have same content
110+
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
111+
}
112+
}
113+
114+
/**
115+
* Return true if the contents of a and b differ. Test parent recursively
116+
* @param a
117+
* @param b
118+
* @return False if the two items are the same
119+
*/
120+
private boolean fooDiff(Foo a, Foo b) {
121+
if (a != null && b != null) {
122+
return a.getId() != b.getId()
123+
|| !a.getName().equals(b.getName())
124+
|| fooDiff(a.getParent(), b.getParent());
125+
}
126+
return !(a == null && b == null);
127+
}
128+
129+
/**
130+
* @param c collection of Foo
131+
* @return true if the collection is sorted by name
132+
*/
133+
private boolean isSorted(Collection<Foo> c) {
134+
String prevName = null;
135+
for (Foo foo : c) {
136+
if (prevName == null || foo.getName().compareTo(prevName) > 0) {
137+
prevName = foo.getName();
138+
} else {
139+
return false;
140+
}
141+
}
142+
return true;
143+
}
144+
}

0 commit comments

Comments
 (0)