从文件导入时,TreeSet 仅复制第一项

我实现了一个名为 EventSet 的类,其中包含一个带有自定义比较器的 TreeSet。comparator 应该与 equals 一致,因为在将元素添加到集合之前,TreeSet 似乎使用 compare 或 compareTo 进行所有需要的比较。我的应用程序需要读取包含一系列命令的文本文件,一个可能的命令是导入指定事件的文本文件。因此,一个假设的 event.txt 文件包含几行,例如“IN LOGIN 18082019 ab001 45.457, 9,181 A”,应用程序调用一个方法来解析字符串并将其转换为一个 Event 对象,该对象被添加到 EventSet 实例中。这里的问题很奇怪:一切正常,除非在命令文件中我尝试导入相同的事件。txt 文件两次,文件的第一行被转换为一个事件,并作为重复项插入到集合中,即使 equals 和 compare 说它是重复项。无论我如何更改它,这只会发生在文件的第一行。到目前为止,这是我的一些代码:


类事件集:


private static EventSet instance;

private TreeSet<Event> eventTree;


//costruttore

private EventSet() {

    EventComparator comp = new EventComparator();

    this.eventTree = new TreeSet<Event>(comp);

}


public static EventSet getInstance() {

    if (instance == null) {

        instance = new EventSet();

    }

    return instance;

}


public TreeSet<Event> getEventTree() {

    return eventTree;

}


public void setEventTree(TreeSet<Event> eventTree) {

    this.eventTree = eventTree;

}



public boolean add(Event e) {

    return this.eventTree.add(e);

}


public boolean add(Set<Event> set) {

    return this.eventTree.addAll(set);

}

类事件比较器:


public EventComparator() {

    super();

}


@Override

public int compare(Event e1, Event e2) {

    if(e1.equals(e2)) {

        return 0;

    } else if(e1.getTimestamp().compareTo(e2.getTimestamp())>=0) {

        return 1;

    } else {

        return -1;

    }

}


烙印99
浏览 93回答 1
1回答

红糖糍粑

已解决:显然,问题是由调用一个方法引起的,该方法读取文件并在另一个从文件读取的方法中对树执行 add() 。我编辑了 import_events() 和 import_cmd() 方法,现在一切正常。private HashSet<Event> import_events(String path) throws FileNotFoundException, IOException, ParseException {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; FileReader fr1;&nbsp; &nbsp; BufferedReader br1;&nbsp;&nbsp; &nbsp; String current;&nbsp;&nbsp; &nbsp; String[] tokens;&nbsp; &nbsp; HashSet<Event> set = new HashSet<Event>();&nbsp; &nbsp; fr1 = new FileReader(path);&nbsp; &nbsp; br1 = new BufferedReader(fr1);&nbsp;&nbsp; &nbsp; current = br1.readLine();&nbsp;&nbsp; &nbsp; if (current == null) {&nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Warning: event file is empty, no events to import");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br1.close();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fr1.close();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return set;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; InputLine il;&nbsp; &nbsp; while (current != null) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; current = current.trim();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; il = new InputLine(current, controller);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; il = new InputLine(current);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (il.line_ok()) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tokens = current.split(Pattern.quote(" "));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RegState reg_state = RegState.valueOf(tokens[0]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String user_id = tokens[3];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LogState log_state = LogState.valueOf(tokens[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputLine.d_format.setLenient(false);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date timestamp = InputLine.d_format.parse(tokens[2]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] latlong = tokens[4].split(",");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double lat = Double.parseDouble(latlong[0]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double longi = Double.parseDouble(latlong[1]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Position pos = Position.create(lat, longi);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pos == null) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Error: invalid coordinates at "+current+", event ignored");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Emotion emotion = Emotion.valueOf(tokens[5]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Event event = new Event(reg_state,log_state, timestamp, user_id, pos, emotion);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set.add(event);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = br1.readLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br1.close();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; fr1.close();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return set;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }现在 import_events() 返回文件中包含的事件的 HashSet。import_cmd() 使用 HashSet 作为在树上调用 addAll() 的参数。void import_cmd() throws NumberFormatException, ParseException {&nbsp; &nbsp; FileReader fr;&nbsp; &nbsp; BufferedReader br;&nbsp; &nbsp; String current;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; fr = new FileReader(cmd_path);&nbsp; &nbsp; &nbsp; &nbsp; br = new BufferedReader(fr);&nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Error: file not found at this location "+cmd_path.getAbsolutePath());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; InputLine line;&nbsp; &nbsp; Set<Event> toAdd=new HashSet<Event>();&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; current = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; while (current != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = new InputLine(current, controller);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = new InputLine(current);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.cmd_check() == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String extracted = line.getIn_line().substring(line.getIn_line().indexOf("(")+1, line.getIn_line().indexOf(")"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String path = this.event_path.getAbsolutePath()+File.separator+extracted;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toAdd=import_events(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("File "+ extracted + " successfully imported ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Error: file not found at "+path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ioe) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Error: unable to read from file at "+path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean addOk=EventSet.getInstance().add(toAdd);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller!=null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (addOk) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Events added");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("No events to add");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (line.cmd_check() == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean valid = line.validate_date_iterval(line.getIn_line());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (valid) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Date[] dates = line.convertIntervaltoDates(line.intervalExtract(line.getIn_line()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; createmap(dates[0], dates[1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Map correctly created for "+ line.getIn_line());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (this.controller != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; controller.get_message("Invalid date at "+ line.getIn_line()+": unable to create map");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = br.readLine();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; br.close();&nbsp; &nbsp; &nbsp; &nbsp; fr.close();&nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java