- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我实现了一个名为 EventSet 的类,其中包含一个带有自定义比较器的 TreeSet。比较器应该与 equals 一致,因为看起来 TreeSet 在将元素添加到集合之前使用compare 或compareTo 来完成所有需要的比较。我的应用程序需要读取包含一系列命令的文本文件,一个可能的命令是导入指定事件的文本文件。因此,假设的 event.txt 文件包含几行,例如“IN LOGIN 18082019 ab001 45.457, 9,181 A”,应用程序调用一个方法来解析该字符串并将其转换为 Event 对象,该对象将添加到 EventSet 实例中。这里的问题非常奇怪:一切正常,除非在命令文件中我尝试导入相同的 event.txt 文件两次,文件的第一行被转换为事件并作为重复插入到集合中,即使等于和比较说它是重复的。无论我如何更改它,这只发生在文件的第一行。这是到目前为止我的一些代码:
事件集类:
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;
}
}
类(class) Activity :
RegState reg_state;
String user_id;
LogState log_state;
Position position;
Date timestamp;
Emotion emotion;
//costruttore
public Event(RegState reg_state, LogState log_state, Date timestamp, String user_id, Position pos, Emotion emo) {
this.reg_state = reg_state;
this.user_id = user_id;
this.log_state = log_state;
this.position = pos;
this.timestamp = timestamp;
this.emotion = emo;
}
public RegState getReg_state() {
return reg_state;
}
public void setReg_state(RegState reg_state) {
this.reg_state = reg_state;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public LogState getLog_state() {
return log_state;
}
public void setLog_state(LogState log_state) {
this.log_state = log_state;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Date getTimestamp() {
return timestamp;
}
public void setTimestamp(Date timestamp) {
this.timestamp = timestamp;
}
public Emotion getEmotion() {
return emotion;
}
public void setEmotion(Emotion emotion) {
this.emotion = emotion;
}
@Override
public String toString() {
String s = reg_state.toString()+" | "+log_state.toString()+" | "+InputLine.d_format.format(timestamp)+" | "+user_id+" | "+position.toString()+" | "+emotion.toString();
return s;
}
@Override
public int hashCode() {
return Objects.hash(user_id, reg_state, log_state, position, timestamp, emotion);
}
@Override
public boolean equals(Object obj) {
if (obj==null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Event) {
Event e = (Event)obj;
if (this.user_id.equals(e.getUser_id()) & this.log_state==e.getLog_state() &
this.position.equals(e.getPosition()) & this.timestamp.equals(e.getTimestamp()) & this.emotion==e.getEmotion() & this.reg_state==e.getReg_state()) {
return true;
}
}
return false;
}
导入cmd.txt文件的方法:
void import_cmd() throws NumberFormatException, ParseException {
FileReader fr;
BufferedReader br;
String current;
try {
fr = new FileReader(cmd_path);
br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at this location "+cmd_path.getAbsolutePath());
}
return;
}
InputLine line;
try {
current = br.readLine();
while (current != null) {
if (this.controller != null) {
line = new InputLine(current, controller);
} else {
line = new InputLine(current);
}
if (line.cmd_check() == 1) {
String extracted = line.getIn_line().substring(line.getIn_line().indexOf("(")+1, line.getIn_line().indexOf(")"));
String path = this.event_path.getAbsolutePath()+File.separator+extracted;
try {
import_events(path);
if (this.controller != null) {
controller.get_message("File "+ extracted + " successfully imported ");
}
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at "+path);
}
} catch (IOException ioe) {
if (this.controller != null) {
controller.get_message("Error: unable to read from file at "+path);
}
}
} else if (line.cmd_check() == 2) {
boolean valid = line.validate_date_iterval(line.getIn_line());
if (valid) {
//call create map
if (this.controller != null) {
controller.get_message("Map correctly created for "+ line.getIn_line());
}
} else if (this.controller != null) {
controller.get_message("Invalid date at "+ line.getIn_line()+": unable to create map");
}
}
current = br.readLine();
}
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
导入event.txt文件的方法:
private void import_events(String path) throws FileNotFoundException, IOException, ParseException {
FileReader fr;
BufferedReader br;
String current;
String[] tokens;
fr = new FileReader(path);
br = new BufferedReader(fr);
current = br.readLine();
if (current == null) {
if (this.controller != null) {
controller.get_message("Warning: event file is empty, no events to import");
}
br.close();
fr.close();
return;
}
InputLine il;
while (current != null) {
current = current.trim();
if (this.controller != null) {
il = new InputLine(current, controller);
} else {
il = new InputLine(current);
}
if (il.line_ok()) {
tokens = current.split(Pattern.quote(" "));
RegState reg_state = RegState.valueOf(tokens[0]);
String user_id = tokens[3];
LogState log_state = LogState.valueOf(tokens[1]);
InputLine.d_format.setLenient(false);
Date timestamp = InputLine.d_format.parse(tokens[2]);
String[] latlong = tokens[4].split(",");
double lat = Double.parseDouble(latlong[0]);
double longi = Double.parseDouble(latlong[1]);
Position pos = Position.create(lat, longi);
if (pos == null) {
if (this.controller != null) {
controller.get_message("Error: invalid coordinates at "+current+", event ignored");
}
break;
}
Emotion emotion = Emotion.valueOf(tokens[5]);
Event event = new Event(reg_state,log_state, timestamp, user_id, pos, emotion);
boolean added=EventSet.getInstance().add(event);
if (this.controller != null) {
if (added) {
controller.get_message("Event successfully created with "+ current);
} else if (!added) {
controller.get_message("Duplicated event was ignored at "+ current + ": event already present in the set");
}
}
}
current = br.readLine();
}
br.close();
fr.close();
return;
}
我用于测试的文本文件-cmd.txt
import(eventi1.txt)
import(eventi2.txt)
create_map(10062019-12062019)
import(eventi2.txt)
eventi2.txt
IN LOGIN 10062019 ab00x 45.464,9.191 A
IN LOGIN 10062019 ab002 45.463,9.192 F
IN LOGOUT 10062019 ab001 45.458,9.181 S
OUT LOGOUT 11062019 ab002 45.473,9.173 N
IN LOGIN 11062019 ac003 45.461,9.187 T
IN LOGIN 12062019 ad004 45.464,9.188 T
OUT LOGOUT 12062019 ab001 45.473,9.173 N
最佳答案
已解决:显然,该问题是由于调用一个读取文件的方法并在另一个从文件读取的方法内对树执行 add() 引起的。我编辑了 import_events() 和 import_cmd() 方法,现在一切正常。
private HashSet<Event> import_events(String path) throws FileNotFoundException, IOException, ParseException {
FileReader fr1;
BufferedReader br1;
String current;
String[] tokens;
HashSet<Event> set = new HashSet<Event>();
fr1 = new FileReader(path);
br1 = new BufferedReader(fr1);
current = br1.readLine();
if (current == null) {
if (this.controller != null) {
controller.get_message("Warning: event file is empty, no events to import");
}
br1.close();
fr1.close();
return set;
}
InputLine il;
while (current != null) {
current = current.trim();
if (this.controller != null) {
il = new InputLine(current, controller);
} else {
il = new InputLine(current);
}
if (il.line_ok()) {
tokens = current.split(Pattern.quote(" "));
RegState reg_state = RegState.valueOf(tokens[0]);
String user_id = tokens[3];
LogState log_state = LogState.valueOf(tokens[1]);
InputLine.d_format.setLenient(false);
Date timestamp = InputLine.d_format.parse(tokens[2]);
String[] latlong = tokens[4].split(",");
double lat = Double.parseDouble(latlong[0]);
double longi = Double.parseDouble(latlong[1]);
Position pos = Position.create(lat, longi);
if (pos == null) {
if (this.controller != null) {
controller.get_message("Error: invalid coordinates at "+current+", event ignored");
}
break;
}
Emotion emotion = Emotion.valueOf(tokens[5]);
Event event = new Event(reg_state,log_state, timestamp, user_id, pos, emotion);
set.add(event);
}
current = br1.readLine();
}
br1.close();
fr1.close();
return set;
}
现在 import_events() 返回文件中包含的事件的哈希集。
import_cmd() 使用 HashSet 作为在树上调用 addAll() 的参数。
void import_cmd() throws NumberFormatException, ParseException {
FileReader fr;
BufferedReader br;
String current;
try {
fr = new FileReader(cmd_path);
br = new BufferedReader(fr);
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at this location "+cmd_path.getAbsolutePath());
}
return;
}
InputLine line;
Set<Event> toAdd=new HashSet<Event>();
try {
current = br.readLine();
while (current != null) {
if (this.controller != null) {
line = new InputLine(current, controller);
} else {
line = new InputLine(current);
}
if (line.cmd_check() == 1) {
String extracted = line.getIn_line().substring(line.getIn_line().indexOf("(")+1, line.getIn_line().indexOf(")"));
String path = this.event_path.getAbsolutePath()+File.separator+extracted;
try {
toAdd=import_events(path);
if (this.controller != null) {
controller.get_message("File "+ extracted + " successfully imported ");
}
} catch (FileNotFoundException e) {
if (this.controller != null) {
controller.get_message("Error: file not found at "+path);
}
} catch (IOException ioe) {
if (this.controller != null) {
controller.get_message("Error: unable to read from file at "+path);
}
}
boolean addOk=EventSet.getInstance().add(toAdd);
if (this.controller!=null) {
if (addOk) {
controller.get_message("Events added");
} else {
controller.get_message("No events to add");
}
}
} else if (line.cmd_check() == 2) {
boolean valid = line.validate_date_iterval(line.getIn_line());
if (valid) {
Date[] dates = line.convertIntervaltoDates(line.intervalExtract(line.getIn_line()));
createmap(dates[0], dates[1]);
if (this.controller != null) {
controller.get_message("Map correctly created for "+ line.getIn_line());
}
} else if (this.controller != null) {
controller.get_message("Invalid date at "+ line.getIn_line()+": unable to create map");
}
}
current = br.readLine();
}
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
关于java - 从文件导入时,TreeSet 仅复制第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543368/
如何判断两个TreeSet对象是否相等?我使用open-jdk-10。 ModifiebleObject class ModifiebleObject implements Comparable{
我正在使用 TreeSet 并在调用 TreeSet#add() 方法时发现了 ClassCastException。 代码: public class Testing { public st
假设我有一个自然排序的 TreeSet。我可以使用什么接口(interface)和方法来使新的 TreeSet 保持与第一个列表相同的顺序。 最佳答案 使用相同的Comparator(如果您的元素实现
是否有与 java.util.TreeSet 等效的 VB.NET? 最佳答案 您会发现最接近的是 SortedSet(T) class . 关于treeset - VB.NET 相当于 java.u
我有一个 TreeMap,其中的值是 TreeSet。现在我需要遍历键,对于 TreeSet 的每个元素,我必须删除该元素(然后继续做某事),然后删除该 TreeSet 的第二个元素等。 我试过: f
我今天接受了采访,接受我采访的人对他的陈述感到困惑,询问是否有可能 TreeSet等于 HashSet但不是 HashSet等于 TreeSet .我说“不”,但据他说,答案是"is"。 怎么可能?
这对我来说是一个很深的谜。 看看这个: TreeSet s = new TreeSet(); s.add(Long.valueOf(1)); s.add(Long.valueOf(4)); s.add
如果我想在 Java 的 TreeSet 中删除 log(n) 时间内的最高条目,我使用 treeSet.pollFirst() - Scala 的 mutable.TreeSet 类的等价物是什么?
例如,有一个二叉搜索树,其中包含一系列值。在添加新值之前,我需要检查它是否已经包含它“几乎重复”。我有 Java 解决方案,它只是执行地板和天花板以及完成这项工作的进一步条件。 JAVA : 给定一个
作为最佳实践, float 的集合类型实例不应超过一个。例如,Nil 是 scala 库中的一个 case 对象。 但是, TreeMap 和 TreeSet 在每次 empty() 调用时都会创建一
1、TreeSet 概述 1、TreeSet是 SortedSet 接口的实现类, TreeSet 可以确保集合元素处于排序状态。 2、TreeSet顾名思义他内部维护的是一个TreeMap,
我正在尝试创建一个 TreeSet 来对插入的字符串进行升序排序。我正在使用以下代码在 TreeSet 中输入值。 TreeSet ts = new TreeSet(); ts.add("@Test0
对于此作业,我需要将每个包含 2 个字符串的自定义数据类(称为 User)的实例保存到 TreeSet 中。然后,我必须在我创建的 TreeSet 中搜索从另一个文件的每一行中提取的字符串。第一个文件
好的,我有这个问题要解决: 创建一个名为 VirtualLibrary 的泛型类具有单个属性 totalNumberOfEntries , 以及使用户能够设置和返回条目的方法。条目类型为 Book ,
我不知道如何解释/理解以下有关 TreeSet 和 map 函数的行为。 我想我遗漏了一 block 拼图。任何有关此事的线索都将受到欢迎。 scala> class Person(val name:
我有一个间隔的TreeSet(带有开始和结束的案例类)。如果对此树集进行过滤,例如 treeSet.filter(x => input = x.start) 这预计会在 logN 时间内运行吗? 最佳
我需要一种方法来非常快速地计算整数 TreeSet 中小于 X 的元素数量。 我可以使用 子集() headSet() tailSet() 方法,但它们真的很慢(我只需要计数,而不是数字本身)。有办法
我是java新手,我正在尝试访问与其他类不同的类中的ArrayList和TreeSet。我知道这种从一个类访问它的方式。 这是我拥有 TreeSet 的类(class): public class
这个问题已经有答案了: Java : Comparable vs Comparator [duplicate] (2 个回答) 已关闭 3 年前。 这是我的代码 public class SetTes
我需要保留一个排序的节点列表,从第一个开始,然后获取所有相邻节点。第一个节点和所有其他节点都带有一个种子值,用于根据最低种子值确定接下来将使用哪个节点,一旦一个节点被用于获取相邻节点,它就会被标记为已
我是一名优秀的程序员,十分优秀!