Skip to content

Commit 8e203dd

Browse files
authored
Merge pull request eugenp#572 from ishwardas/master
initial pull request for java - join and split arrays and collections
2 parents 66ad57e + 25a1b10 commit 8e203dd

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.baeldung.java.arrays;
2+
3+
import java.util.Arrays;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class ArraysJoinAndSplitJUnitTest {
9+
10+
private final String[] sauces = {"Marinara", "Olive Oil"};
11+
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
12+
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
13+
14+
15+
@Test
16+
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception {
17+
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
18+
19+
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
20+
int AddedSoFar = sauces.length;
21+
22+
System.arraycopy(cheeses, 0, toppings, AddedSoFar, cheeses.length);
23+
AddedSoFar += cheeses.length;
24+
25+
System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length);
26+
27+
Assert.assertArrayEquals(toppings,
28+
new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta",
29+
"Parmesan", "Olives", "Spinach", "Green Peppers"} );
30+
}
31+
32+
33+
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
34+
@Test
35+
public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_ShouldSucceed() throws Exception {
36+
int ordersHalved = (customers.length / 2) + (customers.length % 2);
37+
38+
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
39+
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
40+
41+
Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} );
42+
Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} );
43+
}
44+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.baeldung.java.collections;
2+
3+
import java.util.ArrayList;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class CollectionsJoinAndSplitJUnitTest {
9+
10+
public ArrayList<String> sauces = new ArrayList<String>();
11+
public ArrayList<String> cheeses = new ArrayList<String>();
12+
public ArrayList<String> vegetables = new ArrayList<String>();
13+
14+
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
15+
16+
public CollectionsJoinAndSplitJUnitTest() throws Exception {
17+
whenGeneratingTestData_ShouldSucceed();
18+
}
19+
20+
@Test
21+
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
22+
sauces.clear();
23+
sauces.add("Olive Oil");
24+
sauces.add("Marinara");
25+
26+
cheeses.clear();
27+
cheeses.add("Mozarella");
28+
cheeses.add("Feta");
29+
cheeses.add("Parmesan");
30+
31+
vegetables.clear();
32+
vegetables.add("Olives");
33+
vegetables.add("Spinach");
34+
vegetables.add("Green Peppers");
35+
36+
ingredients.clear();
37+
ingredients.add(sauces);
38+
ingredients.add(cheeses);
39+
ingredients.add(vegetables);
40+
41+
Assert.assertTrue(sauces.size() == 2);
42+
Assert.assertTrue(cheeses.size() == 3);
43+
Assert.assertTrue(vegetables.size() == 3);
44+
Assert.assertTrue(ingredients.size() == 3);
45+
}
46+
47+
@Test
48+
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
49+
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
50+
51+
toppings.add(sauces);
52+
toppings.add(cheeses);
53+
toppings.add(vegetables);
54+
55+
Assert.assertTrue(toppings.size() == 3);
56+
Assert.assertTrue(toppings.contains(sauces));
57+
Assert.assertTrue(toppings.contains(cheeses));
58+
Assert.assertTrue(toppings.contains(vegetables));
59+
}
60+
61+
@Test
62+
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
63+
ArrayList<ArrayList<String>> toppings = ingredients;
64+
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
65+
66+
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
67+
68+
Assert.assertTrue(removedToppings.contains(vegetables));
69+
Assert.assertTrue(removedToppings.size() == 1);
70+
Assert.assertTrue(toppings.size() == 2);
71+
Assert.assertTrue(toppings.contains(sauces));
72+
Assert.assertTrue(toppings.contains(cheeses));
73+
}
74+
}

0 commit comments

Comments
 (0)