Skip to content

Commit c8bf184

Browse files
authored
Merge pull request eugenp#4 from pandachris/BAEL-2184-clean
BAEL-2184 Java Convert Collection to ArrayList
2 parents 46c7340 + 7538a4b commit c8bf184

3 files changed

Lines changed: 203 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ jmeter/src/main/resources/*-JMeter.csv
6262
**/dist
6363
**/tmp
6464
**/out-tsc
65+
**/nbproject/
66+
**/nb-configuration.xml
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(
49+
this.id, this.name, this.parent != null ? this.parent.deepCopy() : null);
50+
}
51+
52+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
// make sure the collection isn't sorted accidentally
38+
assertFalse("Oops: source collection is already sorted!", isSorted(srcCollection));
39+
}
40+
41+
/**
42+
* Section 3. Using the ArrayList Constructor
43+
*/
44+
@Test
45+
public void whenUsingConstructor_thenVerifyShallowCopy() {
46+
ArrayList<Foo> newList = new ArrayList<>(srcCollection);
47+
verifyShallowCopy(srcCollection, newList);
48+
}
49+
50+
/**
51+
* Section 4. Using the Streams API
52+
*/
53+
@Test
54+
public void whenUsingStream_thenVerifyShallowCopy() {
55+
ArrayList<Foo> newList = srcCollection.stream().collect(toCollection(ArrayList::new));
56+
57+
verifyShallowCopy(srcCollection, newList);
58+
}
59+
60+
/**
61+
* Section 5. Deep Copy
62+
*/
63+
@Test
64+
public void whenUsingDeepCopy_thenVerifyDeepCopy() {
65+
ArrayList<Foo> newList = srcCollection.stream()
66+
.map(foo -> foo.deepCopy())
67+
.collect(toCollection(ArrayList::new));
68+
69+
verifyDeepCopy(srcCollection, newList);
70+
}
71+
72+
/**
73+
* Section 6. Controlling the List Order
74+
*/
75+
@Test
76+
public void whenUsingSortedStream_thenVerifySortOrder() {
77+
ArrayList<Foo> newList = srcCollection.stream()
78+
.map(foo -> foo.deepCopy())
79+
.sorted(Comparator.comparing(Foo::getName))
80+
.collect(toCollection(ArrayList::new));
81+
82+
assertTrue("ArrayList is not sorted by name", isSorted(newList));
83+
}
84+
85+
/**
86+
* Verify that the contents of the two collections are the same
87+
* @param a
88+
* @param b
89+
*/
90+
private void verifyShallowCopy(Collection a, Collection b) {
91+
assertEquals("Collections have different lengths", a.size(), b.size());
92+
Iterator<Foo> iterA = a.iterator();
93+
Iterator<Foo> iterB = b.iterator();
94+
while (iterA.hasNext()) {
95+
// use '==' to test instance identity
96+
assertTrue("Foo instances differ!", iterA.next() == iterB.next());
97+
}
98+
}
99+
100+
/**
101+
* Verify that the contents of the two collections are the same
102+
* @param a
103+
* @param b
104+
*/
105+
private void verifyDeepCopy(Collection a, Collection b) {
106+
assertEquals("Collections have different lengths", a.size(), b.size());
107+
Iterator<Foo> iterA = a.iterator();
108+
Iterator<Foo> iterB = b.iterator();
109+
while (iterA.hasNext()) {
110+
Foo nextA = iterA.next();
111+
Foo nextB = iterB.next();
112+
// should not be same instance
113+
assertFalse("Foo instances are the same!", nextA == nextB);
114+
// but should have same content
115+
assertFalse("Foo instances have different content!", fooDiff(nextA, nextB));
116+
}
117+
}
118+
119+
/**
120+
* Return true if the contents of a and b differ. Test parent recursively
121+
* @param a
122+
* @param b
123+
* @return False if the two items are the same
124+
*/
125+
private boolean fooDiff(Foo a, Foo b) {
126+
if (a != null && b != null) {
127+
return a.getId() != b.getId()
128+
|| !a.getName().equals(b.getName())
129+
|| fooDiff(a.getParent(), b.getParent());
130+
}
131+
return !(a == null && b == null);
132+
}
133+
134+
/**
135+
* @param c collection of Foo
136+
* @return true if the collection is sorted by name
137+
*/
138+
private static boolean isSorted(Collection<Foo> c) {
139+
String prevName = null;
140+
for (Foo foo : c) {
141+
if (prevName == null || foo.getName().compareTo(prevName) > 0) {
142+
prevName = foo.getName();
143+
} else {
144+
return false;
145+
}
146+
}
147+
return true;
148+
}
149+
}

0 commit comments

Comments
 (0)