【问题标题】:Compare one Array List object with the next one将一个 Array List 对象与下一个对象进行比较
【发布时间】:2018-01-12 00:49:32
【问题描述】:

大家好,这里是我的代码

public class Main {
    public static List<String> globalList;

    public static void main(String[] args) {
        globalList = new ArrayList<String>();
        File folder = new File("\\Documents\\Folder);
                File[] listOfFiles = folder.listFiles();
                for(File file : listOfFiles) {
                    dosomething(file);
                }
                for( String t : globalList)
                {
                    System.out.println(t);
                }
    }

    public static void dosomething(File file) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                globalList.add(str);
            }
            in.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

在我的文件夹中,我有一些文件。每个文件都包含一行信息,所以我取出每一行并将这些行保存到我的 Arraylist globalList。现在我想将 ArrayList 中的一个对象与下一个对象进行比较,这可能吗? 就像: if (firstobj == nextobj) 删除它。

编辑

对不起各位,我忘记写点什么了...... 问题是,我需要删除的行并不完全相同,因为它们以生成的数字开头,这个数字对那些文件很重要,所以我不能删除它...... 所以我需要比较我的行的一部分,如果它相同我需要删除它。

一个例子:
10133;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
10134;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
10135;1;XXXX 110;4100;Autotour 4M100;15;0;K;0;;;0;2;3;XXXX

所以现在您看到前 2 行除了起始编号几乎相同,我需要找到这些行并删除它们。由于变化,我不能做像if(list.contains("10135;1;XXXX 110;4100;Autotour 4M100;15;0;K;0;;;0;2;3;XXXX") ) 这样的事情。

【问题讨论】:

  • 当然这是可能的globalList.get(0).equals(globalList.get(1))。但你真正想要实现什么?
  • @MoritzPetersen 我需要在列表 100files 中读取(当我的程序完成时)。在其中一些文件中(不是全部)是我需要删除的行。
  • 你想避免重复行吗?是这个吗?
  • 您要删除仅生成的 id 不同的条目?
  • 你如何定义“足够相似”?

标签: java file arraylist compare


【解决方案1】:

是的,这是可能的。您可以使用equals 方法来比较行。但是,您应该在读取文件时比较行,而不是将文件内容存储在list 中并进行比较。例如:

try(BufferedReader reader1 = new BufferedReader(new FileReader(file1));
BufferedReader reader2 = new BufferedReader(new FileReader(file2));){
    String line1 = reader1.readLine();
    String line2 = reader2.readLine();
    if(line1 != null && line1.equals(line2)){
       //Do something
    }
}catch(Exception e){
    //Exception handling
}

【讨论】:

    【解决方案2】:

    编辑:

    如果生成相同的数字后跟分号等转义字符,则可以获取第一个分号的索引并获取从下一个字符开始的子字符串:

    public static void dosomething(File file) {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                globalList.add(str);
    
                String subString = str.substring(str.indexOf(";") + 1, str.length());
    
                for(Iterator<String> iter = globalList.iterator(); iter.hasNext();)
                {
                    String elem = (String) iter.next();
    
                    if(elem.contains(subString))
                       iter.remove();
                    }
                }
            }
            in.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
    

    另一个版本:

    public static void dosomething(File file) {
        try {
                BufferedReader in = new BufferedReader(new FileReader(file));
                String str;
                while ((str = in.readLine()) != null) {
    
                    // remove spaces from beginning and end of the input string
                    str = str.trim();
                    String subString = str.substring(str.indexOf(";") + 1, str.length());
    
                    boolean match = false;
                    // search in globalList for a match
                    for(String elem : globalList)
                    {
                        if(elem.contains(subString)){
                           match = true;
                           break;
                        }
                    }
                    // if it doesn't contain the string add it
                    if(!match)
                       globalList.add(str);
                }
                in.close();
            } catch (IOException e) {
                System.out.println(e);
            }
        }
    

    【讨论】:

    • 谢谢你的回答,我很抱歉老兄,我忘了说重要的事情。你能读懂“编辑”吗?非常感谢!
    • 已编辑,但我假设您在生成的数字后总是有一个分号。它是否正确?还是会改变?
    • 是的,我需要这样的东西,但我的问题是我需要比较每一行,比如下一行是否包含相同的文本 ";1;XXXX 110;4100;Autotour 4M100;30;0 ;K;0;;;0;2;3;XXXX" 删除我。最后我需要检查大约 100 个文件,如果下一行同样删除我,我需要检查 oke ...
    • 哦该死的儿子 xD 我非常爱你,“另一个版本”正是想要的。它运作良好,它删除了我必须删除的行。但现在的一个问题是,一条线仍然存在。例如:line1 (same) line2 (same) line3 (same) line4(different) 现在第 2,3 行已删除,但第 1 行仍然存在
    • 很高兴它有效!哪条线?它在某些方面与其他人不同吗?也许是一个空间?
    【解决方案3】:

    我认为您只是想确保最终得到一个列表,其中包含每个文件中的每个“唯一”行。

    如果是这种情况,您可以使用 Set (https://docs.oracle.com/javase/7/docs/api/java/util/Set.html) 的实现来代替您的 globaList,例如 HashSet https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

    set 实现将确保不添加重复项。

    【讨论】:

      【解决方案4】:

      我想你正在寻找这样的东西

        Iterator<String> iterator =  globalList.iterator();
        String lastObj = null;
        String nextObj = null;
        while (iterator.hasNext()){
          nextObj = iterator.next();
          if(lastObj == null ){
            lastObj = nextObj;
          }
          else {
            if(lastObj.equals(nextObj)){
              iterator.remove();
            }
          }
        }
      

      【讨论】:

      • 对不起,我忘记了重要的事情,请您阅读编辑部分,但感谢您的回答,是的,我需要这样的东西。
      【解决方案5】:

      在您的情况下,如果您不想每次都检查部分字符串是否包含在列表中,那么如果您有一个巨大的文件,则需要几个小时,您可以使用不允许重复的 Set。 set 的独特之处在于它不允许重复,因此不是采用字符串列表,而是将字符串包装在自定义类中,然后覆盖该类的 equals 和 hashcode 实现,以便在第一个之前不会考虑字符串的部分字符串中出现分号。

      终于找到工作代码了..

      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.HashSet;
      import java.util.Set;
      
      public class MyString 
      {
          public static Set<MyString> globalSet; 
          private String line;
          public String getLine(){
              return this.line;
          }
      
          public void  setLine(String newLine){
              this.line = newLine;  
          }
      
      
      
          public static void main(String[] args) {
              File folder = new File("C:\\Users\\hbm5cf7\\Documents\\NewF");
              globalSet = new HashSet<MyString>(); 
              File[] listOfFiles = folder.listFiles();
      
              for(File file : listOfFiles) {
                  dosomething(file);
              }
      
              for(MyString t : globalSet)
              {
                  System.out.println(t.getLine());
              }
      
          }
          public static void dosomething(File file)
          {
      
              try {
      
                  BufferedReader in = new BufferedReader(new FileReader(file));
                  MyString str;
                  String line;
                  while ((line = in.readLine()) != null) {
                      str = new MyString();
                      str.setLine(line);
                      globalSet.add(str);
                  }
                  in.close();
      
      
      
              }catch(IOException e)
              {
                  System.out.println(e);
              }
          }
      
          @Override
          public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + ((line == null) ? 0 : line.split(";", 2)[1].hashCode());
              return result;
          }
      
           @Override
              public boolean equals(Object obj)
              { 
      
                  if (this == obj)
                  {
                      return true;
                  }
                  if (getClass() != obj.getClass())
                  {
                      return false;
                  }
                  if (this.line.equals(((MyString) obj).getLine())) 
                  {
                      return true;
                  }
                  String splitString = ((MyString) obj).getLine().split(";", 2)[1];   
                  if(this.line.endsWith(splitString))
                  {
                      return true;  
                  }
                  return false; 
               }
      
      }
      

      通过覆盖 equals 和 hashcode 方法,您可以决定哪个是重复的,哪个不取决于您的要求

      【讨论】:

      • 我试图测试那个,但我认为我愚蠢地在我的代码中实现这个权利......我在这一行遇到错误:if (this.line.equals(obj.line))
      • @k.bo 是的,我的错是因为行是私有对象。现在我修改了我的代码检查它。
      • @K.bo 我没有在 Eclipse 中执行该代码。我刚刚在这里输入了。因此,只需查看实现并尝试使用这种方式。
      • 我不明白 =(
      • 我想说的是我没有自己编译代码,而是给出了实现方式
      【解决方案6】:

      @Harish Barma

      文件内容: 文件1:

      2;XXXX;XXXX;;P;XX;XXX;1N410;20170719;1;;;0;0;1;999;1;1;;
      10000;1;XXXX 096;1410;Autotour 1N410;15;0;K;0;;;0;2;;XXXX
      10001;2;XXXX 097;1410;Autotour 1N410;10;0;K;0;;;0;2;;XXXX
      10002;3;XXXX 098;1410;Autotour 1N410;13;0;K;0;;;0;2;;XXXX
      10003;4;XXXX 099;1410;Autotour 1N410;19;0;K;0;;;0;2;;XXXX
      10004;5;XXXX 100;1410;Autotour 1N410;15;0;K;0;;;0;2;;XXXX
      

      文件2:

      2;XXXX;XXXX;;P;XX;XXXX;3A680;20170726;1;;;0;0;1;999;1;1;;
      10082;1;XXX S146;3680;Autotour 3A680;5;0;K;0;;;0;2;;XXXx
      10083;2;XXX S147;3680;Autotour 3A680;8;0;K;0;;;0;2;;XXXX
      10084;3;XXX 095;3680;Autotour 3A680;6;0;K;0;;;0;2;3;XXXX
      

      文件3:

      2;XXXX;XXXX;;P;XX;XXX;4M100;20170719;1;;;0;0;1;999;1;1;;
      10129;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10130;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10131;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10132;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10133;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10134;1;XXXX 110;4100;Autotour 4M100;30;0;K;0;;;0;2;3;XXXX
      10135;1;XXXX 110;4100;Autotour 4M100;15;0;K;0;;;0;2;3;XXXX
      

      (文件 5-100 可以包含与文件 3 相同的行或与文件 1 或 2 相同的不同行。) 代码:

      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      import java.util.*;
      
      public class MyString 
      {
          public static Set<MyString> globalSet; 
          private String line;
          public String getLine(){
              return this.line;
          }
          public void  setLine(String newLine){
              this.line = newLine;  
          }
          @Override
          public boolean equals(Object obj)
          {
          if (this == obj)
          {
              return true;
          }
          if (getClass() != obj.getClass())
          {
              return false;
          }
          if (this.line.equals(((MyString) obj).getLine())) //compiler said i need to do this so...
          {
              return true;
          }
          String splitString = ((MyString) obj).getLine().split(";", 2)[1];   //compiler said i need to do this so...
           if(this.line.endsWith(splitString))
           {
               return true;  
           }
          return false; 
          }
      
      
          public static void main(String[] args) {
                  File folder = new File("C:\\Users\\V90xxxxPCN\\Documents\\Neuer Ordner");
                  File[] listOfFiles = folder.listFiles();
      
      
      
                 for(File file : listOfFiles) {
                     dosomething(file);
                 }
      
                for(MyString t : globalSet)
              {
                  System.out.println(t.getLine());
              }
      
      
          }
          public static void dosomething(File file)
          {
      
              try {
                  globalSet = new HashSet<MyString>();    
                  BufferedReader in = new BufferedReader(new FileReader(file));
                  MyString str = new MyString();
                  String line;
                  while ((line = in.readLine()) != null) {
                       str.setLine(line);
                      globalSet.add(str);
                  }
      
      
      
                  }catch(IOException e)
                  {
                      System.out.println(e);
                  }
      
          }
      }
      

      预期输出:

      2;XXXX;XXXX;;P;XX;XXX;1N410;20170719;1;;;0;0;1;999;1;1;;
          10000;1;XXXX 096;1410;Autotour 1N410;15;0;K;0;;;0;2;;XXXX
          10001;2;XXXX 097;1410;Autotour 1N410;10;0;K;0;;;0;2;;XXXX
          10002;3;XXXX 098;1410;Autotour 1N410;13;0;K;0;;;0;2;;XXXX
          10003;4;XXXX 099;1410;Autotour 1N410;19;0;K;0;;;0;2;;XXXX
          10004;5;XXXX 100;1410;Autotour 1N410;15;0;K;0;;;0;2;;XXXX
      2;XXXX;XXXX;;P;XX;XXXX;3A680;20170726;1;;;0;0;1;999;1;1;;
          10082;1;XXX S146;3680;Autotour 3A680;5;0;K;0;;;0;2;;XXXx
          10083;2;XXX S147;3680;Autotour 3A680;8;0;K;0;;;0;2;;XXXX
          10084;3;XXX 095;3680;Autotour 3A680;6;0;K;0;;;0;2;3;XXXX
      2;XXXX;XXXX;;P;XX;XXX;4M100;20170719;1;;;0;0;1;999;1;1;;
      10135;1;XXXX 110;4100;Autotour 4M100;15;0;K;0;;;0;2;3;XXXX
      

      输出:

      10135;1;XXXX 110;4100;Autotour 4M100;15;0;K;0;;;0;2;3;XXXX
      

      【讨论】:

        猜你喜欢
        • 2018-08-04
        • 2019-07-26
        • 2013-11-15
        • 2023-03-12
        • 2020-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多