How to Find Duplicate String in Java

Since we all known HashSet is a part of Collection that will store unique value always 
>  if we are trying to add any duplicate value add method return  false.
> add Method check existing Object Hashcode and compare with  newly added Object
if hashCode match then add method Return False else Return True .
>here we created HashSet<String> Object with generic type data type
>since we Added <String> After HashSet then it only allows to add String value
>generic type is only use to overcome data type casting problem 
..
Please Check below programmer 




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javainterview;
import java.util.*;

/**
 *
 * @author shiboo
 */
public class Duplicate {
    
    public static void main(String[] args) {
        
        String [] arr= {"Anand","Ajeet","Antrish","Anand"};
        HashSet<String>hset= new HashSet<String>();
        for(String value:arr){
        if(!hset.add(value)){
            System.out.println("Duplicate Value=>"+value);
        }
        }
        
    }
    
}


Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Java Design pattern

Map class field to map