Friday, October 9, 2020

Java Program To Remove Duplicates From Array (Without Using Set)

1. Overview

In this article, you'll learn how to remove the duplicate values from array in different ways using java programming.

Let us learn how to work with the sorted and unsorted array for this scenario.

In the previous article, we've explained the different ways to remove duplicates from List in Java 8?

Java Program To Remove Duplicates From Array (Without Using Set)


Wednesday, October 7, 2020

Java Program To Get Intersection Of Two Arrays

1. Overview

In this tutorial, you'll learn how to get the intersection of two arrays in java.  An Intersection Set is a common values among all collections. 

In our case, you need to get the values that are common in both arrays.

For example, Look at the below two arrays.

Array 1: { 1, 2, 3, 4, 5}

Array 2: {2,  4,  6,  8}

In these two arrays, 2 and 4 are the common values so these 2,4 set is an intersection of Array 1 and Array 2.

Java Program To Get Intersection Of Two Arrays


We are going to write the java program using HashSet and retainAll() method.

retainAll() Example to get unique and duplicate values from the list

We've seen in the previous article, how to get a union of two arrays of integers or string values.

Monday, October 5, 2020

Java Program To Get Union Of Two Arrays

1. Overview

In this article, you'll learn how to get the union of two arrays in java.  A union set is all the values of two sets or from all collection.

We can do the union function in java using HashSet with arrays. Use the addAll() method to add all the values of each array into HashSet.

This is a simple solution. As well as this solution will work with both numbers and string values.

Java Program To Get Union Of Two Arrays


2. Union of two Integer arrays with numbers

Let us write the java program to print the union of two integer arrays.

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class UnionTwoArraysNumbers {

	public static void main(String[] args) {

		// Integer array 1
		Integer[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8 };
		System.out.println("Array 1 : " + Arrays.toString(array1));

		// Integer array 2
		Integer[] array2 = { 2, 4, 6, 8, 10, 12, 14 };
		System.out.println("Array 2 : " + Arrays.toString(array2));

		// creating a new Set
		Set<Integer> unionOfArrays = new HashSet<>();

		// adding the first array to set
		unionOfArrays.addAll(Arrays.asList(array1));

		// adding the second array to set
		unionOfArrays.addAll(Arrays.asList(array2));

		// converting set to array.
		Integer[] unionArray = {};
		unionArray = unionOfArrays.toArray(unionArray);

		// printing the union of two arrays.
		System.out.println("Union of two arrays: " + Arrays.toString(unionArray));

	}

}

Output:

Array 1 : [1, 2, 3, 4, 5, 6, 7, 8]
Array 2 : [2, 4, 6, 8, 10, 12, 14]
Union of two arrays: [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14]

3. Union of two String arrays

Let us write the java program to print the union of two String arrays.


public class UnionTwoArraysStrings {

	public static void main(String[] args) {

		// Integer array 1
		String[] array1 = { "A", "B", "C", "D" };
		System.out.println("String Array 1 : " + Arrays.toString(array1));

		// Integer array 2
		String[] array2 = { "C", "D", "E", "F" };
		System.out.println("String  Array 2 : " + Arrays.toString(array2));

		// creating a new Set
		Set<String> unionOfArrays = new HashSet<>();

		// adding the first array to set
		unionOfArrays.addAll(Arrays.asList(array1));

		// adding the second array to set
		unionOfArrays.addAll(Arrays.asList(array2));

		// converting set to array.
		String[] unionArray = {};
		unionArray = unionOfArrays.toArray(unionArray);

		// printing the union of two arrays.
		System.out.println("Union of two String arrays: " + Arrays.toString(unionArray));

	}

}

Output:

String Array 1 : [A, B, C, D]
String  Array 2 : [C, D, E, F]
Union of two String arrays: [A, B, C, D, E, F]

4. Conclusion

In this article, we've seen how to find the union of two arrays in java using HashSet.

As usual, all examples. are over Github.

How to compare two strings?

How to add integers to ArrayList?

HashSet

String API Methods

Integer API

Saturday, October 3, 2020

Java String to String Array Conversion Examples

1. Overview

In this article, you'll learn how to convert String to String Array in java with example programs.

Let us learn the different ways to do the conversion into a string array. from a string value.

Java String to String Array Conversion Examples