-1

I have an Employee class. It has three paramaters like name(String), age(int),address(String) I have added 10 Employee objects into an ArrayList now i have a list of names but in it, name can be partial or complete. I need to check whether any name in the above list exist in the list of emplyee object either partially or complete match.

Basiclly, I need to check contains for the name string parameter of emplyee object in the ArrayList.

What will be the best approach for it.

mayank agrawal
  • 628
  • 5
  • 20
  • Possible duplicate of [How to prevent the adding of duplicate objects to an ArrayList](http://stackoverflow.com/questions/14192532/how-to-prevent-the-adding-of-duplicate-objects-to-an-arraylist) – Mario Santini Oct 25 '16 at 09:09
  • iterate one by one using for(item:list) approach. – Azodious Oct 25 '16 at 09:10
  • I need to check contains whether added object's name parameter string contains the partial/complete name from the list – mayank agrawal Oct 25 '16 at 09:24
  • Tihis is the right approach: write the code. If having problems writing it, ask a question showing the code, where you are stuck, etc. If you succeed in writing the code, but you have questions about whether it follows best practices or has performance issues, then you can go to codereview.stackexchange.com – Erwin Bolwidt Oct 25 '16 at 10:50
  • I was not aware about the shared link next time, i will follow it from next time thanks – mayank agrawal Oct 25 '16 at 12:27

1 Answers1

1

You can override arrayList class like this :

public class PartialStringList extends ArrayList<String>
{
 public boolean contains(Object o)
 {
    if(!(o instanceof String))
    {
        return false;
    }
    String s = (String)o;
    Iterator<String> iter = iterator();
    while(iter.hasNext())
    {
        String iStr = iter.next();
        if (iStr.contain(s))
        {
            return true;
        }
    }
    return false;
  }
}

Java-8 Then filter your list name :

List<String> employeNameList=new PartialNameList<>(nameList);
employeList=employeList.stream().
   filter(e->employeNameList.contains(e.getName()).
   collect(Collectors.toList());

Java-7

List<String> employeNameList=new PartialNameList<>(nameList);
List<Employe> filteredList=new ArrayList<>();
for(Employe e: employeList){
   if(employeNameList.contains(e.getName()){
    filteredList.add(e);
   }
}
andolsi zied
  • 3,553
  • 2
  • 32
  • 43
  • Thanks for your answer. Can you explain it without Functional Interfaces and Lambda Expressions – mayank agrawal Oct 25 '16 at 09:29
  • @mayankagrawal if it works, click on this gray tick below the answer score. – andolsi zied Oct 25 '16 at 09:31
  • I will do that but first need to test it and i am using java 7 so i can not use Functional Interfaces and Lambda Expressions – mayank agrawal Oct 25 '16 at 09:33
  • I was checking this out but in case of **Iterator iter = iterator();** it's showing empty list but i have added the list into it – mayank agrawal Oct 25 '16 at 11:45
  • class PartialStringList extends ArrayList { static List nameList=new ArrayList<>(); public PartialStringList(List nameList) { PartialStringList.nameList=nameList; } public boolean contains(Object o) { if(!(o instanceof String)) { return false; } String s = (String)o; Iterator iter = iterator(); while(iter.hasNext()) { String iStr = iter.next(); if (iStr.contains(s)) { return true; } } return false; } } – mayank agrawal Oct 25 '16 at 11:48
  • Thank you. it's working fine for me – mayank agrawal Oct 25 '16 at 12:02