Java Program to Identify Two Addresses are Same or Not When Two Address Details are Separated by Comma and in Jumbled Manner

In this article you will see how you can identify two addresses are same or not even it they are in jumbled manner by using Java programming language.

Java Program to Identify Two Addresses are Same or Not When Two Address Details are Separated by Comma and in Jumbled Manner

As per the problem statement you have to identify two addresses are same or different where both the addresses are in jumbled manner.

Let’s understand it with an example.

Suppose you have 2 addresses.
Address-1: "PLOT-345, SAI NAGAR , MADHAPUR , HYDERABAD"
Address-2: "PLOT-345, MADHAPUR , SAI NAGAR , HYDERABAD"
If you will look both the address then both the addresses are same only difference is the addresses is jumbled.

Let’s see another example.

Address-1: "PLOT-245, SAI NAGAR , MADHAPUR , HYDERABAD"
Address-2: "PLOT-345, MADHAPUR , SAI NAGAR , HYDERABAD"
If you will look both the address then both the addresses are not same. 
Here, PLOT number of both the addresses differs.

Let’s understand it more clearly with an program.

Approach:

  • Declared two String variables and assign two addresses as values.
  • Declared two array of String and and split both the addresses based on space and store the elements in both the array respectively.
  • Compared both the array by using containsAll() method.
  • If one array contains all the elements of another array then it is sure both the addresses are same else both the addresses are not same.

Program-1: (With Same Address & in Jumbled Manner)

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        //declared two String variables and assigned two addresses as values
        String address1= new String("PLOT-345, SAI NAGAR , MADHAPUR , HYDERABAD"); 
        String address2= new String("PLOT-345, MADHAPUR , SAI NAGAR , HYDERABAD");
        //declared two array of String
        //and splited both the addresses based on space 
        //and stored the elements in both the array respectively
        String a1[] = address1.split(" ");
        String a2[] = address2.split(" ");
        //compared both the array elements by using containsAll() method
        //if one array contains all the elements of another array
        //then it is sure both the addresses are same
        if(Arrays.asList(a1).containsAll(Arrays.asList(a2))) 
        {
            System.out.print("BOTH ADDRESSES ARE SAME");
        } 
        //Else both the addresses are not same
        else 
        {
            System.out.print("BOTH ADDRESSES ARE NOT SAME");
        }
        
    }
}

Output:

BOTH ADDRESSES ARE SAME

Program-2: (With Different Address & in Jumbled Manner)

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        //declared two String variables and assigned two addresses as values
        String address1= new String("PLOT-245, SAI NAGAR , MADHAPUR , HYDERABAD"); 
        String address2= new String("PLOT-345, MADHAPUR , SAI NAGAR , HYDERABAD");
        //declared two array of String
        //and splited both the addresses based on space \
        //and stored the elements in both the array respectively
        String a1[] = address1.split(" ");
        String a2[] = address2.split(" ");
        //compared both the array by using containsAll() method
        //if one array contains all the elemnts of another array
        //then it is sure both the addresses are same
        if(Arrays.asList(a1).containsAll(Arrays.asList(a2))) 
        {
            System.out.print("BOTH ADDRESSES ARE SAME");
        } 
        //Else both the addresses are not same
        else 
        {
            System.out.print("BOTH ADDRESSES ARE NOT SAME");
        }
        
    }
}

Output:

BOTH ADDRESSES ARE NOT SAME

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.