Spring -Setter Dependency Injection Demo

Project Snapshot

upload(18)

Program


package com.wordpress.sreeharshasite.model;
public class Employee {
private int empId;
private String empName;
private String city;
private String designation;
public Employee()
{
}
public Employee(int empId,String empName,String city,String designation)
{
this.empId=empId;
this.empName=empName;
this.city=city;
this.designation=designation;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package com.wordpress.sreeharshasite.services;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public interface EmployeeService {
List<Employee> listAllEmployees();
}


package com.wordpress.sreeharshasite.services;
import java.util.ArrayList;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public class EmployeeServiceImpl implements EmployeeService {
public List<Employee> listAllEmployees() {
Employee emp1=new Employee(100,"John","Delhi","Associate");
Employee emp2=new Employee(101,"Ravi","Hyderabad","SeniorAssociate");
Employee emp3=new Employee(102,"Rajesh","Delhi","JuniorAssociate");
Employee emp4=new Employee(103,"Ramesh","Delhi","AssociateDirector");
List<Employee> employeeList=new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
return employeeList;
}
}


package com.wordpress.sreeharshasite.controller;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
import java.util.List;
import com.wordpress.sreeharshasite.model.*;
public class MyController {
private EmployeeServiceImpl employeeServiceImpl;
public EmployeeServiceImpl getEmployeeServiceImpl() {
return employeeServiceImpl;
}
public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
this.employeeServiceImpl = employeeServiceImpl;
}
public List<Employee> listAllEmployees()
{
return employeeServiceImpl.listAllEmployees();
}
}


package com.wordpress.sreeharshasite.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.model.Employee;
public class DIDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
MyController controller = (MyController) ctx.getBean("myController");
List<Employee> employees = controller.getEmployeeServiceImpl().listAllEmployees();
for(Employee employee : employees){
System.out.println("Employee Details");
System.out.print(employee.getEmpId()+" ");
System.out.print(employee.getEmpName()+" ");
System.out.print(employee.getCity()+" ");
System.out.print(employee.getDesignation()+" ");
System.out.println();
}
}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="employeeServiceImpl" class="com.wordpress.sreeharshasite.services.EmployeeServiceImpl" />
<bean id="myController" class="com.wordpress.sreeharshasite.controller.MyController">
<property name="employeeServiceImpl" ref="employeeServiceImpl" />
</bean>
</beans>


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wordpress.sreeharshasite.spring</groupId>
<artifactId>SpringDIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDIDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Output

Jul 20, 2017 5:02:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Jul 20 17:02:41 IST 2017]; root of context hierarchy
Jul 20, 2017 5:02:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Employee Details
100 John Delhi Associate 
Employee Details
101 Ravi Hyderabad SeniorAssociate 
Employee Details
102 Rajesh Delhi JuniorAssociate 
Employee Details
103 Ramesh Delhi AssociateDirector
Advertisements
Tagged with: ,
Posted in Frameworks, Spring

RESTful Webservices Demo

Project Snapshot

upload(6)

Program


package com.wordpress.sreeharshasite.webservices.services.entity;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name="employee")
@XmlAccessorType (XmlAccessType.FIELD)
public class Employee {
private int empId;
private String empName;
private String city;
private String designation;
public Employee()
{
}
public Employee(int empId,String empName,String city,String designation)
{
this.empId=empId;
this.empName=empName;
this.city=city;
this.designation=designation;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package com.wordpress.sreeharshasite.webservices.services.dao;
import java.util.List;
import com.wordpress.sreeharshasite.webservices.services.entity.Employee;
public interface EmployeeDAO {
Employee addEmployee(Employee emp);
void deleteEmployee(int empId);
Employee updateEmployee(Employee emp);
Employee findEmployee(int empId);
List<Employee> getAllEmployees();;
}


package com.wordpress.sreeharshasite.webservices.services.dao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wordpress.sreeharshasite.webservices.services.entity.Employee;
public class EmployeeDAOImpl implements EmployeeDAO {
private static final Map&amp;lt;Integer,Employee&amp;gt; employeeMap=new HashMap&amp;lt;Integer,Employee&amp;gt;();
static
{
initEmps();
}
private static void initEmps()
{
Employee emp1=new Employee(100,"Abhi","Chennai","Associate");
Employee emp2=new Employee(101,"Ravi","Delhi","SeniorAssociate");
Employee emp3=new Employee(102,"Harsha","Bengaluru","Associate");
employeeMap.put(emp1.getEmpId(), emp1);
employeeMap.put(emp2.getEmpId(), emp2);
employeeMap.put(emp3.getEmpId(), emp3);
}
public Employee addEmployee(Employee emp) {
employeeMap.put(emp.getEmpId(), emp);
return emp;
}
public void deleteEmployee(int empId) {
employeeMap.remove(empId);
}
public Employee findEmployee(int empId)
{
Employee emp=employeeMap.get(empId);
return emp;
}
public Employee updateEmployee(Employee emp) {
employeeMap.put(emp.getEmpId(), emp);
return emp;
}
@Override
@GET
@Produces ({MediaType.APPLICATION_XML})
public List<Employee> getAllEmployees()
{
return employeedao.getAllEmployees();
}
}


package com.wordpress.sreeharshasite.webservices.services;
import java.util.List;
import javax.ws.rs.core.Response;
import com.wordpress.sreeharshasite.webservices.services.entity.Employee;
public interface EmployeeService {
List<Employee> getAllEmployees();
Employee getEmployee(int empId);
Response addEmployee(Employee emp);
Response deleteEmployee(int empId);
Response updateEmployee(Employee emp);
}


package com.wordpress.sreeharshasite.webservices.services;
import java.util.List;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.wordpress.sreeharshasite.webservices.services.dao.EmployeeDAOImpl;
import com.wordpress.sreeharshasite.webservices.services.entity.Employee;
@Path ("/employees")
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDAOImpl employeedao=new EmployeeDAOImpl();
@GET
@Produces ({MediaType.APPLICATION_XML})
@Path ("/{empId}")
public Employee getEmployee(@PathParam ("empId") int empId) {
return employeedao.findEmployee(empId);
}
@POST
@Produces ({MediaType.APPLICATION_XML})
public Response addEmployee(Employee emp) {
employeedao.addEmployee(emp);
return Response.status(200).build();
}
@DELETE
@Path ("/{empId}")
@Produces ({MediaType.APPLICATION_XML})
public Response deleteEmployee(@PathParam ("empId") int empId) {
employeedao.deleteEmployee(empId);
return Response.status(200).build();
}
@PUT
@Produces ({MediaType.APPLICATION_XML})
public Response updateEmployee(Employee emp) {
employeedao.updateEmployee(emp);
return Response.status(200).build();
}
@Override
@GET
@Produces ({MediaType.APPLICATION_XML})
public List&amp;lt;Employee&amp;gt; getAllEmployees() {
return employeedao.getAllEmployees();
}

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>restwebservices</groupId>
<artifactId>restwebservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>restwebservices</display-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Output

GET

upload(7)

GET Particular empId

upload(8)

PUT–>Update (Advanced REST Client Chrome Plugin)

upload(10)

upload(11)

POST–>Insert (Advanced REST Client Chrome Plugin)

upload(12)

upload(13)

DELETE

upload(15)upload(16)upload(17)

Tagged with:
Posted in RESTFul Webservice, Webservices

Queue Implementation using Arrays

Program


package com.wordpress.sreeharshasite;
import java.util.Arrays;
public class Queue<T> {
private int size=0;
private Object[] elements;
private final static int INITIAL_CAPACITY=10;
public Queue()
{
this.elements=new Object[INITIAL_CAPACITY];
}

/*Element to be added at last*/
public void insert(Object obj)
{
if(elements.length==size)
{
increaseCapacity();
}
elements[size]=obj;
size++;
}
public void increaseCapacity()
{
elements=Arrays.copyOf(elements, elements.length*2);
System.out.println("Capacity size after increase-"+elements.length);
}

/*First element to be removed and rest elements should be moved one index backward*/
public void remove()
{
if(size<=0)
{
throw new ArrayIndexOutOfBoundsException();
}
else
{
int index=0;
while(index<size)
{
elements[index]=elements[index+1];
elements[index+1]=null;
index++;
}
size--;
}
}
public void print()
{
for(int i=0;i<size;i++)
{
System.out.print(elements[i]+" ");
}
System.out.println();
}
}


package com.wordpress.sreeharshasite;
public class QueueDemo {
public static void main(String[] args) {
Queue<Integer> que=new Queue<Integer>();
que.insert(10);
que.insert(11);
que.insert(12);
que.insert(13);
que.insert(14);
que.insert(15);
que.insert(16);
que.insert(17);
que.insert(18);
que.insert(19);
que.insert(20);
System.out.println("After insertion");
que.print();
que.remove();
System.out.println("After Deletion");
que.print();
que.remove();
System.out.println("After Deletion");
que.print();
que.remove();
System.out.println("After Deletion");
que.print();
que.insert(21);
System.out.println("After insertion");
que.print();
}
}

Output

Capacity size after increase-20
After insertion
10 11 12 13 14 15 16 17 18 19 20 
After Deletion
11 12 13 14 15 16 17 18 19 20 
After Deletion
12 13 14 15 16 17 18 19 20 
After Deletion
13 14 15 16 17 18 19 20 
After insertion
13 14 15 16 17 18 19 20 21
Tagged with: , ,
Posted in Collections, Core Java, Data Structures

Stack Implementation Using Arrays

Program


package com.wordpress.sreeharshasite;
import java.util.Arrays;
public class Stack<T> {
private int size=0;
private Object[] array;
private static final int INITIAL_CAPACITY=10;
public Stack()
{
this.array=new Object[INITIAL_CAPACITY];
}
public void push(Object obj)
{
/*To check if the capacity of the array is reached ,
* if yes then length of the array is doubled
*/
if(array.length==size)
{
ensureCapacity();
}
/*if there are no objects add the element at the first index */
if(size==0)
{
array[0]=obj;
size++;
}
/*add the element at the zero index of the array*/
else
{
int index=size-1;
while(index>=0)
{
Object temp=array[index];
array[index+1]=temp;
index--;
}
array[index+1]=obj;
size++;
}
}
/*Removing the first element in the list */
public void pop()
{
if(size<=0)
{
throw new ArrayIndexOutOfBoundsException();
}
else
{
int index=0;
while(index<size-1)
{
array[index]=array[index+1];
index++;
}
size--;
}
}
/*Creating a new array while copying the old elements
* in the array and increasing the length twice*/
public void ensureCapacity()
{
array=Arrays.copyOf(array, array.length*2);
System.out.println("Size capacity after increase-"+array.length);
}
public void print()
{
for(int i=0;i<size;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
public Object peek()
{
int index=0;
if(size>0)
return array[index];
else
return null;
}
}


package com.wordpress.sreeharshasite;
public class StackDemo {
public static void main(String[] args) {
Stack<Integer> stack=new Stack<Integer>();
stack.push(10);
stack.push(9);
stack.push(99);
stack.push(70);
stack.push(65);
stack.push(47);
stack.push(32);
stack.push(23);
stack.push(9);
stack.push(76);
stack.push(54);
stack.push(45);
stack.push(3);
System.out.println("Elements in the Stack After Push");
stack.print();
stack.pop();
System.out.println("Elements in the Stack After Pop");
stack.print();
stack.pop();
System.out.println("Elements in the Stack After Pop");
stack.print();
Integer firstElement=(Integer) stack.peek();
System.out.println("Element at the first position in the stack "+firstElement);
stack.push(130);
System.out.println("Elements in the Stack After Push");
stack.print();
}
}

Output

Size capacity after increase-20
Elements in the Stack After Push
3 45 54 76 9 23 32 47 65 70 99 9 10 
Elements in the Stack After Pop
45 54 76 9 23 32 47 65 70 99 9 10 
Elements in the Stack After Pop
54 76 9 23 32 47 65 70 99 9 10 
Element at the first position in the stack 54
Elements in the Stack After Push
130 54 76 9 23 32 47 65 70 99 9 10

 

Tagged with: , ,
Posted in Core Java, Data Structures

Custom Iterator

Program


package com.wordpress.sreeharshasite;
public interface Iterable<T> {
public abstract Iterator<T> iterator();
}


package com.wordpress.sreeharshasite;
public interface Iterator<T> {
public abstract boolean hasNext();
public abstract Object next();
public abstract void remove(Object obj);
}


package com.wordpress.sreeharshasite;
import java.util.Arrays;
public class CustomIterator<T> implements Iterable<T> {
private int size=0;
private Object[] array;
private int currPos=-1;
public int getSize() {
return size;
}
/*initializing the object array with size 10*/
public CustomIterator()
{
this.array=new Object[10];
}
public void add(Object obj)
{
/*if the existing array size is less than 5 then a new array copying
all the existing elements with the double length of that array is created*/
if(array.length-size<=5)
{
increaseSize();
}
array[size++]=obj;
}
/*if the existing array size is less than 5 then a new array copying
all the existing elements with the double length of that array is created*/
public void increaseSize()
{
array=Arrays.copyOf(array,array.length*2);
System.out.println("Object array length after size increase "+array.length);
}
public Object get(int index)
{
/*if index is beyond the array size exception is thrown*/
if(size<index)
{
throw new ArrayIndexOutOfBoundsException();
}
/*if index value is negative exception is thrown*/
else if (index<0)
{
throw new ArrayIndexOutOfBoundsException();
}
else
{
return array[index];
}
}
public void remove(int index)
{
/*if index is beyond the array size exception is thrown*/
if(index>size)
{
throw new ArrayIndexOutOfBoundsException();
}
/*if index value is negative exception is thrown*/
else if(index<0)
{
throw new ArrayIndexOutOfBoundsException();
}
else
{
array[index]=null;
int tmp=index;
/*moving all the elements position one index up*/
while(tmp<size)
{
array[tmp]=array[tmp+1];
array[tmp+1]=null;
tmp++;
}
size--;
}
}
public void print(CustomIterator<T> list)
{
for(int i=0;i	<list.getSize();i++)
{
System.out.print(list.get(i)+" ");
}
System.out.println();
}
@Override
public Iterator<T> iterator() {
return new MyIterator<T>();
}
private class MyIterator<T> implements Iterator<T>
{
@Override
public boolean hasNext() {
if(size > 0 && currPos!=size-1)
{
currPos++;
return true;
}
else
{
currPos=-1;
return false;
}
}
@Override
public Object next() {
return array[currPos];
}
@Override
public void remove(Object obj) {
remove(obj);
}
}
}


package com.wordpress.sreeharshasite;
public class CustomIteratorDemo {
public static void main(String[] args) {
CustomIterator<Integer> list=new CustomIterator<Integer>();
list.add(5);
list.add(15);
list.add(25);
list.add(53);
list.add(35);
list.add(50);
Iterator<Integer> itr=list.iterator();
while(itr.hasNext())
{
Integer element=(Integer) itr.next();
System.out.print(element+" ");
}
System.out.println();
System.out.println("After removal");
list.remove(3);
itr=list.iterator();
while(itr.hasNext())
{
Integer element=(Integer) itr.next();
System.out.print(element+" ");
}
System.out.println();
list.add(23);
list.add(33);
list.add(5);
list.add(15);
list.add(25);
list.add(53);
list.add(35);
list.add(50);
list.add(5);
list.add(15);
list.add(25);
list.add(53);
list.add(35);
list.add(50);
itr=list.iterator();
while(itr.hasNext())
{
Integer element=(Integer) itr.next();
System.out.print(element+" ");
}
}
}

Output

Object array length after size increase 20
5 15 25 53 35 50 
After removal
5 15 25 35 50 
Object array length after size increase 40
5 15 25 35 50 23 33 5 15 25 53 35 50 5 15 25 53 35 50

 

Tagged with: ,
Posted in Collections, Core Java

HashSet Implementation

Program

package com.wordpress.sreeharshasite;
import java.util.HashMap;
public class HashSet<T>
{
private transient HashMap<T,Object> map;
/*a dummy object is stored as value in the hashmap with the value stored as the key */ private final static Object PRESENT=new Object();
/*Hashset internally creates an object for hashmap*/
public HashSet ()
{
map=new HashMap<T,Object>();
}
/* returns true if the object to be added is not present in the hashmap   false if object is present*/
public boolean add(T obj)
{
return (map.put(obj, PRESENT)==null);
}
/* returns true if the object to be removed is  present in the hashmap if object is not present*/
public boolean remove(T obj)
{
return map.remove(obj)==PRESENT;
}
}

package com.wordpress.sreeharshasite;
public class HashSetDemo
{
public static void main(String[] args)
{
HashSet<String> hashset=new HashSet<String>();
boolean value=hashset.add("John");
System.out.println("Element add status "+value);
value=hashset.add("Ravi");
System.out.println("Element add status "+value);
value=hashset.add("Alex");
System.out.println("Element add status "+value);
value=hashset.add("Jacob");
System.out.println("Element add status "+value);
value=hashset.add("John");
System.out.println("Element add status "+value);
value=hashset.remove("John");
System.out.println("Is element available to remove "+value); value=hashset.remove("John");
System.out.println("Is element available to remove "+value);
}
}

Output

Element add status true
Element add status true
Element add status true
Element add status true
Element add status false
Is element available to remove true
Is element available to remove false
Tagged with: , ,
Posted in Collections, Core Java, Data Structures

Duplicate element check in Array

Program


package com.wordpress.sreeharshasite;
public class DuplicateCheckArray

 {
 public static void main(String[] args) 

{ 

DuplicateCheckArray dc=new DuplicateCheckArray(); 

int a[]={5,55,43,49,78,52,55,99}; 

dc.duplicateIntCheck(a); 

String[] names={"Ravi","Rajesh","Harsha","Abhi","latha","geetha","Ravi","Rani"}; dc.duplicateStringCheck(names);

 } 

public void duplicateStringCheck(String[] names) 

{

 for(int i=0;i<names.length;i++) 

{ 

for(int j=i+1;j<names.length;j++) 

{ 

if(names[i].equals(names[j])) 

{ 

System.out.println("Duplicate found at the indexes: "+i+" &nbsp;"+j); System.out.println("Duplicate Data is "+names[i]); 

} 

}

 } 

} 

public void duplicateIntCheck(int[] arr) 

{ 

for(int i=0;i<arr.length;i++) 

{ 

for(int j=i+1;j<arr.length;j++)

 { 

if(arr[i]==arr[j]) 

{ 

System.out.println("Duplicate found at the indexes: "+i+" &nbsp;"+j);
System.out.println("Duplicate Data is "+arr[i]);

 } 

} 

} 

}
}

Output

Duplicate found at the indexes: 1 6
Duplicate Data is 55
Duplicate found at the indexes: 0 6
Duplicate Data is Ravi
Tagged with: , ,
Posted in Collections, Core Java
Follow My Diary on WordPress.com
Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

%d bloggers like this: