Skip to content

Commit 026e5de

Browse files
author
govinda-radhe
committed
Java - Join and Split Arrays and Collections
1 parent 5a172a7 commit 026e5de

3 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
9+
//import java.util.AbstractSet;
10+
11+
public class CollectionsJoinAndSplitJUnitTest {
12+
13+
public ArrayList<String> sauces = new ArrayList<String>();
14+
public ArrayList<String> cheeses = new ArrayList<String>();
15+
public ArrayList<String> vegetables = new ArrayList<String>();
16+
17+
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
18+
19+
public CollectionsJoinAndSplitJUnitTest() throws Exception {
20+
//generate test data
21+
whenGeneratingTestData_ShouldSucceed();
22+
}
23+
24+
@Test
25+
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
26+
sauces.clear();
27+
sauces.add("Olive Oil");
28+
sauces.add("Marinara");
29+
30+
cheeses.clear();
31+
cheeses.add("Mozarella");
32+
cheeses.add("Feta");
33+
cheeses.add("Parmesan");
34+
35+
vegetables.clear();
36+
vegetables.add("Olives");
37+
vegetables.add("Spinach");
38+
vegetables.add("Green Peppers");
39+
40+
ingredients.clear();
41+
ingredients.add(sauces);
42+
ingredients.add(cheeses);
43+
ingredients.add(vegetables);
44+
45+
Assert.assertTrue(sauces.size() == 2);
46+
Assert.assertTrue(cheeses.size() == 3);
47+
Assert.assertTrue(vegetables.size() == 3);
48+
Assert.assertTrue(ingredients.size() == 3);
49+
}
50+
51+
@Test
52+
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
53+
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
54+
55+
toppings.add(sauces);
56+
toppings.add(cheeses);
57+
toppings.add(vegetables);
58+
59+
Assert.assertTrue(toppings.size() == 3);
60+
Assert.assertTrue(toppings.contains(sauces));
61+
Assert.assertTrue(toppings.contains(cheeses));
62+
Assert.assertTrue(toppings.contains(vegetables));
63+
}
64+
65+
@Test
66+
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
67+
ArrayList<ArrayList<String>> toppings = ingredients;
68+
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
69+
70+
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
71+
72+
Assert.assertTrue(removedToppings.contains(vegetables));
73+
Assert.assertTrue(removedToppings.size() == 1);
74+
Assert.assertTrue(toppings.size() == 2);
75+
Assert.assertTrue(toppings.contains(sauces));
76+
Assert.assertTrue(toppings.contains(cheeses));
77+
}
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
//pizza toppings
11+
private final String[] sauces = {"Marinara", "Olive Oil"};
12+
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
13+
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
14+
15+
16+
@Test
17+
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception {
18+
//create the destination array
19+
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
20+
21+
//add the sauces
22+
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
23+
int AddedSoFarCount = sauces.length;
24+
25+
//add the cheeses
26+
System.arraycopy(cheeses, 0, toppings, AddedSoFarCount, cheeses.length);
27+
AddedSoFarCount += cheeses.length;
28+
29+
//add the vegetables
30+
System.arraycopy(vegetables, 0, toppings, AddedSoFarCount, vegetables.length);
31+
32+
//check the result
33+
Assert.assertArrayEquals(toppings,
34+
new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta",
35+
"Parmesan", "Olives", "Spinach", "Green Peppers"} );
36+
}
37+
38+
39+
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
40+
@Test
41+
public void givenOneStringArray_whenSplittingInHalfBetweenTwoStringArrays_ShouldSucceed() throws Exception {
42+
//split the orders in half, rounding up
43+
int ordersHalved = (customers.length / 2) + (customers.length % 2);
44+
45+
//divide the orders between two drivers
46+
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
47+
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
48+
49+
//check ther result
50+
Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} );
51+
Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} );
52+
}
53+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
9+
//import java.util.AbstractSet;
10+
11+
public class CollectionsJoinAndSplitJUnitTest {
12+
13+
public ArrayList<String> sauces = new ArrayList<String>();
14+
public ArrayList<String> cheeses = new ArrayList<String>();
15+
public ArrayList<String> vegetables = new ArrayList<String>();
16+
17+
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
18+
19+
public CollectionsJoinAndSplitJUnitTest() throws Exception {
20+
//generate test data
21+
whenGeneratingTestData_ShouldSucceed();
22+
}
23+
24+
@Test
25+
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
26+
sauces.clear();
27+
sauces.add("Olive Oil");
28+
sauces.add("Marinara");
29+
30+
cheeses.clear();
31+
cheeses.add("Mozarella");
32+
cheeses.add("Feta");
33+
cheeses.add("Parmesan");
34+
35+
vegetables.clear();
36+
vegetables.add("Olives");
37+
vegetables.add("Spinach");
38+
vegetables.add("Green Peppers");
39+
40+
ingredients.clear();
41+
ingredients.add(sauces);
42+
ingredients.add(cheeses);
43+
ingredients.add(vegetables);
44+
45+
Assert.assertTrue(sauces.size() == 2);
46+
Assert.assertTrue(cheeses.size() == 3);
47+
Assert.assertTrue(vegetables.size() == 3);
48+
Assert.assertTrue(ingredients.size() == 3);
49+
}
50+
51+
@Test
52+
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
53+
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
54+
55+
toppings.add(sauces);
56+
toppings.add(cheeses);
57+
toppings.add(vegetables);
58+
59+
Assert.assertTrue(toppings.size() == 3);
60+
Assert.assertTrue(toppings.contains(sauces));
61+
Assert.assertTrue(toppings.contains(cheeses));
62+
Assert.assertTrue(toppings.contains(vegetables));
63+
}
64+
65+
@Test
66+
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
67+
ArrayList<ArrayList<String>> toppings = ingredients;
68+
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
69+
70+
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
71+
72+
Assert.assertTrue(removedToppings.contains(vegetables));
73+
Assert.assertTrue(removedToppings.size() == 1);
74+
Assert.assertTrue(toppings.size() == 2);
75+
Assert.assertTrue(toppings.contains(sauces));
76+
Assert.assertTrue(toppings.contains(cheeses));
77+
}
78+
}

0 commit comments

Comments
 (0)