下面是重写ArrayList,并保证ChooseCars里面alpha字段不重复的例子
public class DistinctList extends ArrayList<ChooseCars>{ private static Map<String,Integer> distinct_map = new HashMap<String,Integer>(); private Integer distinct_int_value = null; public void show(){ for(String map_key:distinct_map.keySet()){ System.out.println(map_key + " " + distinct_map.get(map_key)); } } public boolean add(ChooseCars distinct){ if(distinct_map.containsKey(distinct.getAlpha())){ Integer distinct_int_value = distinct_map.get(distinct.getAlpha()); distinct_int_value = distinct_int_value+1; return true; }else{ distinct_map.put(distinct.getAlpha(), 1); return super.add(distinct); } } public boolean normalAdd(ChooseCars distinct){ return super.add(distinct); } }