Remove all objects in an arraylist that exist in another arraylist

前端 未结 4 1479
梦谈多话
梦谈多话 2020-12-17 07:25

I\'m trying to read in from two files and store them in two separate arraylists. The files consist of words which are either alone on a line or multiple words separated by c

4条回答
  •  一整个雨季
    2020-12-17 08:07

    First you need to override equal method in your custom class and define the matching criteria of removing list

    public class CustomClass{
    
     @Override
        public boolean equals(Object obj) {
    
            try {
                CustomClass licenceDetail  = (CustomClass) obj;
                return name.equals(licenceDetail.getName());
            }
            catch (Exception e)
            {
                return false;
            }
    
        }
    }
    

    Second you call the removeAll() method

    list1.removeAll(list2);

提交回复
热议问题