list集合對象去重


今天突然遇到list集合對象去重的問題,在這里記錄一下解決方法,自己覺得還不錯。

在list集合里去掉重復對象,只要把它強轉成set集合就可以了,

List<Student> stu = new ArrayList<Student>();  
        stu.add(new Student("1","yi"));  
        stu.add(new Student("3","san"));  
        stu.add(new Student("1","yi"));  
        stu.add(new Student("3","san"));  
        stu.add(new Student("2","er"));  
        stu.add(new Student("1","yi"));  
        stu.add(new Student("3","san"));  
        stu.add(new Student("2","er"));  
        //set集合保存的是引用不同地址的對象  
        Set<Student> ts = new HashSet<Student>();  
        ts.addAll(stu);  
          
        for (Student student : ts) {  
            System.out.println(student.getId()+"-"+student.getName());  
        }  
		

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM