姓名 学号 年龄
A     2    20
A     2    30
A     2    20现在要检查记录的唯一性,有什么好的方法实现。
认为1和3是不唯一的。(姓名 学号 年龄 三个一起作为主键)
要求要好得方法,不是直接循环去比!

解决方案 »

  1.   

    sql:select 姓名,学号,年龄 from table group by 姓名,学号,年龄
      

  2.   

    楼上说的是二分法!不是sql,不好意思 我理解错了
      

  3.   

    需要从什么样的数据结构中 检查?
    arraylist hashmap 数组 ?
      

  4.   

    看能不能全部放入一个Set中.....
      

  5.   


    import java.util.*;class t1{
    ArrayList arr = new ArrayList();

    public t1(){
    HashMap temp1 = new HashMap();
    temp1.put("name","a");
    temp1.put("id","001");
    temp1.put("age","12");

    HashMap temp2 = new HashMap();
    temp2.put("name","b");
    temp2.put("id","002");
    temp2.put("age","12");

    HashMap temp3 = new HashMap();
    temp3.put("name","c");
    temp3.put("id","003");
    temp3.put("age","13");

    arr.add(temp1);
    arr.add(temp2);
    arr.add(temp3);
    }


    public boolean checkValues(HashMap map){
    boolean b = false;

    String name = map.get("name").toString();
    String id = map.get("id").toString();
    String age = map.get("age").toString();

    HashMap temp;
    String name_temp,id_temp,age_temp;
    int count = 0;
    for(int i=0;i<arr.size();i++){
    temp = (HashMap)arr.get(i);
    name_temp = temp.get("name").toString();
    id_temp = temp.get("id").toString();
    age_temp = temp.get("age").toString();

    if(name_temp.equals(name)&&id_temp.equals(id)&&age_temp.equals(age))
    count++;
    }
    if(count==1)b=true;
    return b;
    }

    public static void main(String args[]){
    HashMap map = new HashMap();
    map.put("name","c");
    map.put("id","003");
    map.put("age","13");

    boolean b = new t1().checkValues(map);
    System.out.println (b);

    }
    }
    ----------------------------------------------------------------有更好的方法,希望大家写出来!
      

  6.   

    写一个查询sql好了
    select count(*) from table1 a,table 2 where a.name=b.name and a.id=b.id and a.age=b.age 
    如果查出来记录书不为一,那么它肯定是有重复数据了,