javatechie

Custom ArrayList Not_Allow_Duplicate

Sep 21st, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. package com.javatechie.solution;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class CustomeArrayListNotAllowDuplicate extends ArrayList {
  7.  
  8.     @Override
  9.     public boolean add(Object o) {
  10.         if (this.contains(o)) {
  11.             return true;
  12.         } else {
  13.             return super.add(o);
  14.         }
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.         List list=new CustomeArrayListNotAllowDuplicate();
  19.         list.add(1);
  20.         list.add(1);
  21.         System.out.println(list);
  22.     }
  23. }
Add Comment
Please, Sign In to add comment