Write a program in java to prevent duplication from list?

import java.util.*;

public class List

{

      public static void main(String arg[])

      {

            Scanner in=new Scanner(System.in);

            ArrayList ls=new ArrayList();

            int i=0;

            while(i<5)

            {

                  System.out.print("Enter Name - ");

                  String s=in.nextLine();

                  if(true==ls.contains(s))

                  {

                        System.out.println("\n\tThis Name is Abailable in Data File, Try Another Name\n");

                        continue;

                  }

                  else

                  {

                        ls.add(s);

                  }

                  i++;

            }

            System.out.println("\nTotal Number of elements is "+ls.size());

            Iterator itr=ls.iterator();

            while(itr.hasNext())

            {

                  System.out.println("\t"+itr.next());

            }

      }

}

List Iterator (Prevent Duplication From List)

, , , , , ,

Leave a Reply