Finding duplicate values in arraylist

后端 未结 5 708
你的背包
你的背包 2020-11-28 06:41

I have an ArrayList

For Example

class Car{
   String carName;
   int carType;
}

Now, I have to find if the

5条回答
  •  被撕碎了的回忆
    2020-11-28 07:12

    If you have

    class Car{
       String carName;
       int carType;
    }
    

    and

    List list;
    

    that contains a list of cars, then you could have a method like

    public static boolean hasDuplicates(List p_cars) {
        final List usedNames = new ArrayList();
        for (Car car : p_cars) {
            final String name = car.carName;
    
            if (usedNames.contains(name)) {
                return true;
            }
    
            usedNames.add(name);
        }
    
        return false;
    }
    

    to find out whether the list of cars have cars with duplicate names.

提交回复
热议问题