Java - Join and Split Arrays and Collections
This commit is contained in:
parent
5a172a792a
commit
026e5debf1
|
@ -0,0 +1,78 @@
|
|||
package org.baeldung.java.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
//import java.util.AbstractSet;
|
||||
|
||||
public class CollectionsJoinAndSplitJUnitTest {
|
||||
|
||||
public ArrayList<String> sauces = new ArrayList<String>();
|
||||
public ArrayList<String> cheeses = new ArrayList<String>();
|
||||
public ArrayList<String> vegetables = new ArrayList<String>();
|
||||
|
||||
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
|
||||
|
||||
public CollectionsJoinAndSplitJUnitTest() throws Exception {
|
||||
//generate test data
|
||||
whenGeneratingTestData_ShouldSucceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
|
||||
sauces.clear();
|
||||
sauces.add("Olive Oil");
|
||||
sauces.add("Marinara");
|
||||
|
||||
cheeses.clear();
|
||||
cheeses.add("Mozarella");
|
||||
cheeses.add("Feta");
|
||||
cheeses.add("Parmesan");
|
||||
|
||||
vegetables.clear();
|
||||
vegetables.add("Olives");
|
||||
vegetables.add("Spinach");
|
||||
vegetables.add("Green Peppers");
|
||||
|
||||
ingredients.clear();
|
||||
ingredients.add(sauces);
|
||||
ingredients.add(cheeses);
|
||||
ingredients.add(vegetables);
|
||||
|
||||
Assert.assertTrue(sauces.size() == 2);
|
||||
Assert.assertTrue(cheeses.size() == 3);
|
||||
Assert.assertTrue(vegetables.size() == 3);
|
||||
Assert.assertTrue(ingredients.size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
|
||||
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
|
||||
|
||||
toppings.add(sauces);
|
||||
toppings.add(cheeses);
|
||||
toppings.add(vegetables);
|
||||
|
||||
Assert.assertTrue(toppings.size() == 3);
|
||||
Assert.assertTrue(toppings.contains(sauces));
|
||||
Assert.assertTrue(toppings.contains(cheeses));
|
||||
Assert.assertTrue(toppings.contains(vegetables));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
|
||||
ArrayList<ArrayList<String>> toppings = ingredients;
|
||||
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
|
||||
|
||||
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
|
||||
|
||||
Assert.assertTrue(removedToppings.contains(vegetables));
|
||||
Assert.assertTrue(removedToppings.size() == 1);
|
||||
Assert.assertTrue(toppings.size() == 2);
|
||||
Assert.assertTrue(toppings.contains(sauces));
|
||||
Assert.assertTrue(toppings.contains(cheeses));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package org.baeldung.java.arrays;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ArraysJoinAndSplitJUnitTest {
|
||||
|
||||
//pizza toppings
|
||||
private final String[] sauces = {"Marinara", "Olive Oil"};
|
||||
private final String[] cheeses = {"Mozzarella", "Feta", "Parmesan"};
|
||||
private final String[] vegetables = {"Olives", "Spinach", "Green Peppers"};
|
||||
|
||||
|
||||
@Test
|
||||
public void givenThreeStringArrays_whenJoiningIntoOneStringArray_ShouldSucceed() throws Exception {
|
||||
//create the destination array
|
||||
String[] toppings = new String[sauces.length + cheeses.length + vegetables.length];
|
||||
|
||||
//add the sauces
|
||||
System.arraycopy(sauces, 0, toppings, 0, sauces.length);
|
||||
int AddedSoFarCount = sauces.length;
|
||||
|
||||
//add the cheeses
|
||||
System.arraycopy(cheeses, 0, toppings, AddedSoFarCount, cheeses.length);
|
||||
AddedSoFarCount += cheeses.length;
|
||||
|
||||
//add the vegetables
|
||||
System.arraycopy(vegetables, 0, toppings, AddedSoFarCount, vegetables.length);
|
||||
|
||||
//check the result
|
||||
Assert.assertArrayEquals(toppings,
|
||||
new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta",
|
||||
"Parmesan", "Olives", "Spinach", "Green Peppers"} );
|
||||
}
|
||||
|
||||
|
||||
private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"};
|
||||
@Test
|
||||
public void givenOneStringArray_whenSplittingInHalfBetweenTwoStringArrays_ShouldSucceed() throws Exception {
|
||||
//split the orders in half, rounding up
|
||||
int ordersHalved = (customers.length / 2) + (customers.length % 2);
|
||||
|
||||
//divide the orders between two drivers
|
||||
String[] driverOne = Arrays.copyOf(customers, ordersHalved);
|
||||
String[] driverTwo = Arrays.copyOfRange(customers, ordersHalved, customers.length);
|
||||
|
||||
//check ther result
|
||||
Assert.assertArrayEquals(driverOne, new String[] {"Jay", "Harry", "Ronnie"} );
|
||||
Assert.assertArrayEquals(driverTwo, new String[] {"Gary", "Ross"} );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package org.baeldung.java.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
//import java.util.AbstractSet;
|
||||
|
||||
public class CollectionsJoinAndSplitJUnitTest {
|
||||
|
||||
public ArrayList<String> sauces = new ArrayList<String>();
|
||||
public ArrayList<String> cheeses = new ArrayList<String>();
|
||||
public ArrayList<String> vegetables = new ArrayList<String>();
|
||||
|
||||
public ArrayList<ArrayList<String>> ingredients = new ArrayList<ArrayList<String>>();
|
||||
|
||||
public CollectionsJoinAndSplitJUnitTest() throws Exception {
|
||||
//generate test data
|
||||
whenGeneratingTestData_ShouldSucceed();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGeneratingTestData_ShouldSucceed() throws Exception {
|
||||
sauces.clear();
|
||||
sauces.add("Olive Oil");
|
||||
sauces.add("Marinara");
|
||||
|
||||
cheeses.clear();
|
||||
cheeses.add("Mozarella");
|
||||
cheeses.add("Feta");
|
||||
cheeses.add("Parmesan");
|
||||
|
||||
vegetables.clear();
|
||||
vegetables.add("Olives");
|
||||
vegetables.add("Spinach");
|
||||
vegetables.add("Green Peppers");
|
||||
|
||||
ingredients.clear();
|
||||
ingredients.add(sauces);
|
||||
ingredients.add(cheeses);
|
||||
ingredients.add(vegetables);
|
||||
|
||||
Assert.assertTrue(sauces.size() == 2);
|
||||
Assert.assertTrue(cheeses.size() == 3);
|
||||
Assert.assertTrue(vegetables.size() == 3);
|
||||
Assert.assertTrue(ingredients.size() == 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeArrayLists_whenJoiningIntoOneArrayList_ShouldSucceed() throws Exception {
|
||||
ArrayList<ArrayList<String>> toppings = new ArrayList<ArrayList<String>>();
|
||||
|
||||
toppings.add(sauces);
|
||||
toppings.add(cheeses);
|
||||
toppings.add(vegetables);
|
||||
|
||||
Assert.assertTrue(toppings.size() == 3);
|
||||
Assert.assertTrue(toppings.contains(sauces));
|
||||
Assert.assertTrue(toppings.contains(cheeses));
|
||||
Assert.assertTrue(toppings.contains(vegetables));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneArrayList_whenSplittingIntoTwoArrayLists_shouldSucceed() throws Exception {
|
||||
ArrayList<ArrayList<String>> toppings = ingredients;
|
||||
ArrayList<ArrayList<String>> removedToppings = new ArrayList<ArrayList<String>>();
|
||||
|
||||
removedToppings.add(toppings.remove(toppings.indexOf(vegetables)));
|
||||
|
||||
Assert.assertTrue(removedToppings.contains(vegetables));
|
||||
Assert.assertTrue(removedToppings.size() == 1);
|
||||
Assert.assertTrue(toppings.size() == 2);
|
||||
Assert.assertTrue(toppings.contains(sauces));
|
||||
Assert.assertTrue(toppings.contains(cheeses));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue