From 026e5debf1ad089610dc9460817bc9cc3ff6bec9 Mon Sep 17 00:00:00 2001 From: govinda-radhe Date: Mon, 25 Jul 2016 15:00:28 -0700 Subject: [PATCH 1/6] Java - Join and Split Arrays and Collections --- .../CollectionsJoinAndSplitJUnitTest.java | 78 +++++++++++++++++++ .../arrays/ArraysJoinAndSplitJUnitTest.java | 53 +++++++++++++ .../CollectionsJoinAndSplitJUnitTest.java | 78 +++++++++++++++++++ 3 files changed, 209 insertions(+) create mode 100644 core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java create mode 100644 core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java create mode 100644 core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java diff --git a/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java new file mode 100644 index 0000000000..9d7b306c1f --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java @@ -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 sauces = new ArrayList(); + public ArrayList cheeses = new ArrayList(); + public ArrayList vegetables = new ArrayList(); + + public ArrayList> ingredients = new ArrayList>(); + + 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> toppings = new ArrayList>(); + + 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> toppings = ingredients; + ArrayList> removedToppings = new ArrayList>(); + + 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)); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java new file mode 100644 index 0000000000..6b36301915 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java @@ -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"} ); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java new file mode 100644 index 0000000000..9d7b306c1f --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java @@ -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 sauces = new ArrayList(); + public ArrayList cheeses = new ArrayList(); + public ArrayList vegetables = new ArrayList(); + + public ArrayList> ingredients = new ArrayList>(); + + 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> toppings = new ArrayList>(); + + 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> toppings = ingredients; + ArrayList> removedToppings = new ArrayList>(); + + 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)); + } +} From a1a43e64ead82e97b2886497db8ee47e029b190e Mon Sep 17 00:00:00 2001 From: ishwardas Date: Tue, 2 Aug 2016 09:01:02 -0500 Subject: [PATCH 2/6] removed comments --- .../java/arrays/ArraysJoinAndSplitJUnitTest.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java index 6b36301915..bbbef46933 100644 --- a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java @@ -7,7 +7,6 @@ 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"}; @@ -15,21 +14,16 @@ public class ArraysJoinAndSplitJUnitTest { @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"} ); @@ -39,14 +33,11 @@ public class ArraysJoinAndSplitJUnitTest { 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"} ); } From 77c84e9e79cdb24b84958113f8ca2e471a69f5fb Mon Sep 17 00:00:00 2001 From: ishwardas Date: Tue, 2 Aug 2016 09:01:47 -0500 Subject: [PATCH 3/6] removed comments --- .../java/collections/CollectionsJoinAndSplitJUnitTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java index 9d7b306c1f..3a7a527656 100644 --- a/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/collections/CollectionsJoinAndSplitJUnitTest.java @@ -5,9 +5,6 @@ import java.util.ArrayList; import org.junit.Assert; import org.junit.Test; - -//import java.util.AbstractSet; - public class CollectionsJoinAndSplitJUnitTest { public ArrayList sauces = new ArrayList(); @@ -17,7 +14,6 @@ public class CollectionsJoinAndSplitJUnitTest { public ArrayList> ingredients = new ArrayList>(); public CollectionsJoinAndSplitJUnitTest() throws Exception { - //generate test data whenGeneratingTestData_ShouldSucceed(); } From 88c360a3362ce53cd9c5bda3b768cbc065ddb117 Mon Sep 17 00:00:00 2001 From: ishwardas Date: Tue, 2 Aug 2016 09:23:53 -0500 Subject: [PATCH 4/6] Update ArraysJoinAndSplitJUnitTest.java --- .../org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java index bbbef46933..442cdabb1d 100644 --- a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java @@ -32,7 +32,7 @@ public class ArraysJoinAndSplitJUnitTest { private final String[] customers = {"Jay", "Harry", "Ronnie", "Gary", "Ross"}; @Test - public void givenOneStringArray_whenSplittingInHalfBetweenTwoStringArrays_ShouldSucceed() throws Exception { + public void givenOneStringArray_whenSplittingInHalfTwoStringArrays_ShouldSucceed() throws Exception { int ordersHalved = (customers.length / 2) + (customers.length % 2); String[] driverOne = Arrays.copyOf(customers, ordersHalved); From ac018698466492c086524c5c0ef92866f0c1c15d Mon Sep 17 00:00:00 2001 From: ishwardas Date: Tue, 2 Aug 2016 09:26:58 -0500 Subject: [PATCH 5/6] Update ArraysJoinAndSplitJUnitTest.java --- .../baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java index 442cdabb1d..ec59d08525 100644 --- a/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/arrays/ArraysJoinAndSplitJUnitTest.java @@ -17,12 +17,12 @@ public class ArraysJoinAndSplitJUnitTest { String[] toppings = new String[sauces.length + cheeses.length + vegetables.length]; System.arraycopy(sauces, 0, toppings, 0, sauces.length); - int AddedSoFarCount = sauces.length; + int AddedSoFar = sauces.length; - System.arraycopy(cheeses, 0, toppings, AddedSoFarCount, cheeses.length); - AddedSoFarCount += cheeses.length; + System.arraycopy(cheeses, 0, toppings, AddedSoFar, cheeses.length); + AddedSoFar += cheeses.length; - System.arraycopy(vegetables, 0, toppings, AddedSoFarCount, vegetables.length); + System.arraycopy(vegetables, 0, toppings, AddedSoFar, vegetables.length); Assert.assertArrayEquals(toppings, new String[] {"Marinara", "Olive Oil", "Mozzarella", "Feta", From 25a1b10010009230c6d47efb60911cf9cd63539f Mon Sep 17 00:00:00 2001 From: ishwardas Date: Tue, 2 Aug 2016 09:38:57 -0500 Subject: [PATCH 6/6] Delete CollectionsJoinAndSplitJUnitTest.java --- .../CollectionsJoinAndSplitJUnitTest.java | 78 ------------------- 1 file changed, 78 deletions(-) delete mode 100644 core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java diff --git a/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java b/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java deleted file mode 100644 index 9d7b306c1f..0000000000 --- a/core-java/src/test/java/org/baeldung/java/CollectionsJoinAndSplitJUnitTest.java +++ /dev/null @@ -1,78 +0,0 @@ -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 sauces = new ArrayList(); - public ArrayList cheeses = new ArrayList(); - public ArrayList vegetables = new ArrayList(); - - public ArrayList> ingredients = new ArrayList>(); - - 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> toppings = new ArrayList>(); - - 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> toppings = ingredients; - ArrayList> removedToppings = new ArrayList>(); - - 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)); - } -}