判断List是否有重复值

想在添加之前判断一下List中是否包含要添加的这个元素,试试很简单,用contains(Object o)判断一下是否包含就行了

例如:

public List<String> getYear(String bondId){

{

String sql = "SELECT e.bgq FROM EnterpriseSJLL e WHERE e.gpdm = ?";
        List list = this.find(sql, new Object[]{bondId});
        if (list == null)
            return new ArrayList<String>();
        else {
            List<String> newResult = new ArrayList<String>();
            for (int i = 0; i < list.size(); i++) {
                String bgq = (String) list.get(i);
                String newYear = bgq.substring(0, 4);
                if (!newResult.contains(newYear)) {
                    newResult.add(newYear);
                }
            }
            return newResult;
        }

你可能感兴趣的:(List判断是否重复)