• 利用Iterator删除List里相近或相同的对象


    package com.eg.log;

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import org.apache.log4j.Logger;

    public class TestLog
    {
     private static Logger log = Logger.getLogger(TestLog.class);

     /**
      * @param args
      */
     public static void main(String[] args)
     {
      List<User> users = new ArrayList<User>();
      User user5 = new User("yff","111111111","111111111");
      User user9 = new User("yff","111111111","111000");
      User user1 = new User("yff","111111111","111");
      User user6 = new User("lxx","222222222","222222222");
      User user2 = new User("lxx","222222222","222");
      User user3 = new User("stt","333333333","333111");
      User user7 = new User("stt","333333333","333");
      User user4 = new User("jqq","444444444","444444444");
      User user8 = new User("jqq","444444444","444");
      User user10 = new User("ltt","55555555","555");
      users.add(user5);
      users.add(user9);
      users.add(user1);
      users.add(user6);
      users.add(user2);
      users.add(user3);
      users.add(user7);
      users.add(user4);
      users.add(user8);
      users.add(user10);
      List<User> users2 = new ArrayList<User>();
      users2.addAll(users);
      
      for(User user:users){
       System.out.println(user.toString());
      }
      System.out.println("------------------------------------------");
      
      for(User user11 : users)
      {
       for(Iterator<User> userIt2 = users2.iterator();userIt2.hasNext();)
       {
        User user12 = userIt2.next();
        if(user11.getUsername().equals(user12.getUsername()))
        {
         if(user11.getGrade().length()>user12.getGrade().length())
         {
          userIt2.remove();
         }
        }
        
       }
      }
      
      for(User user:users2){
       System.out.println(user.toString());
      }
     }
     

    }
    class User{
     public User(){
      
     }
     public User(String username,String address,String grade){
      this.username=username;
      this.address=address;
      this.grade=grade;
     }
     public String username;
     public String address;
     public String grade;
     public String getUsername() {
      return username;
     }
     public String getGrade() {
      return grade;
     }
     public void setGrade(String grade) {
      this.grade = grade;
     }
     public void setUsername(String username) {
      this.username = username;
     }
     public String getAddress() {
      return address;
     }
     public void setAddress(String address) {
      this.address = address;
     }
     public String toString()
     {
      return this.username+"--"+this.address+"---"+this.grade;
     }
    }

  • 相关阅读:
    Qt 配置fakevim
    CentOS安装Ruby组件
    Linux shell 操作 postgresql,并设置crontab任务
    修改win7锁定界面背景
    Ubuntu安装Redis
    Java 向SQL Server插入文件数据
    用VMware vSphere Client客户端登陆vsphere5提示错误
    DD应用实例
    shell初学
    deepin2014.1快捷键
  • 原文地址:https://www.cnblogs.com/yanff/p/3080783.html
Copyright © 2020-2023  润新知