Java数据结构,用于保存一组项、字符串和;可以迭代的日期
我目前正在哈希集中存储字符串,但我想将每个字符串与日期对象配对。该日期将用作衰减计时器(为字符串设置一个日期,以便在该日期过后可以删除该字符串)。当程序启动时,我希望遍历列表并检查所有字符串的日期,删除那些已过期的字符串 是否有一个2元组的数据结构允许我遍历列表,并且有一个contains方法 另外,如果我太过切换到列表数据结构而不是集合,那么与HashSet相比,迭代数据结构对性能的影响有多大?HashSet在尝试查找字符串是否存在时有一个Java数据结构,用于保存一组项、字符串和;可以迭代的日期,java,data-structures,hashset,Java,Data Structures,Hashset,我目前正在哈希集中存储字符串,但我想将每个字符串与日期对象配对。该日期将用作衰减计时器(为字符串设置一个日期,以便在该日期过后可以删除该字符串)。当程序启动时,我希望遍历列表并检查所有字符串的日期,删除那些已过期的字符串 是否有一个2元组的数据结构允许我遍历列表,并且有一个contains方法 另外,如果我太过切换到列表数据结构而不是集合,那么与HashSet相比,迭代数据结构对性能的影响有多大?HashSet在尝试查找字符串是否存在时有一个.contains(string)方法?检查需要相对快
.contains(string)
方法?检查需要相对快速,最好在一秒钟内完成 使用
Map<String,Date> stringDateMap = new HashMap<String,Date>();
您可以通过以下方式删除相同的数据:
stringDateMap.put("Key1", new Date());
stringDateMap.remove("Key1");
在这里查看其他功能
使用
Map<String,Date> stringDateMap = new HashMap<String,Date>();
您可以通过以下方式删除相同的数据:
stringDateMap.put("Key1", new Date());
stringDateMap.remove("Key1");
在这里查看其他功能
您可以尝试使用以下方法:
Map<Date,List<String>> map = new HashMap<Date,List<String>>();
我建议您使用List,而不是Set,因为它总是进行函数调用来检查元素是否已经存在于集合中。因此,如果您不担心重复项,请选择列表。您可以尝试使用以下方法:
Map<Date,List<String>> map = new HashMap<Date,List<String>>();
我建议您使用List,而不是Set,因为它总是进行函数调用来检查元素是否已经存在于集合中。因此,若您不担心重复,那个么就选择List。java中并没有元组,但您可以很容易地自己创建一个元组
public class StringDatePair {
private final String str;
private final Date date;
public StringDatePair(String str, Date date) {
this.str = str;
this.date = date;
}
public getString() {
return str;
}
public getDate() {
return date;
}
}
如果要在列表中对该元组进行排序,可以实现可比较
和按日期比较
检查包含时,集合比列表快,并且随着集合/列表的内容变大,差异也会变大。但是必须有大量的内容才能使运行时间大于1秒,以检查列表中是否存在项目
此外,如果您最终使用的列表按日期对项目进行了排序,您可以从头开始迭代列表,当您找到第一个日期未过期的项目时,您可以丢弃所有以前的项目。java中没有元组,但您可以非常轻松地创建一个元组
public class StringDatePair {
private final String str;
private final Date date;
public StringDatePair(String str, Date date) {
this.str = str;
this.date = date;
}
public getString() {
return str;
}
public getDate() {
return date;
}
}
如果要在列表中对该元组进行排序,可以实现可比较
和按日期比较
检查包含时,集合比列表快,并且随着集合/列表的内容变大,差异也会变大。但是必须有大量的内容才能使运行时间大于1秒,以检查列表中是否存在项目
此外,如果您最终使用的列表按日期对项目进行了排序,则可以从头开始迭代列表,当您找到第一个日期未过期的项目时,可以丢弃所有以前的项目。我发现一个通用的
对
类非常有用:
public class Pair<T, R> {
T first;
R second;
public Pair(T first, R second) {
this.first = first;
this.second = second;
}
public T getFirst() {
return first;
}
public R getSecond() {
return second;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
if (second != null ? !second.equals(pair.second) : pair.second != null) return false;
return true;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
}
公共类对{
T第一;
R秒;
公共对(T第一,R第二){
this.first=first;
这个秒=秒;
}
公共T getFirst(){
先返回;
}
公共R getSecond(){
返回第二;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
配对=(配对)o;
如果(first!=null?!first.equals(pair.first):pair.first!=null)返回false;
if(second!=null?!second.equals(pair.second):pair.second!=null)返回false;
返回true;
}
@凌驾
公共int hashCode(){
int result=first!=null?first.hashCode():0;
result=31*result+(秒!=null?秒。hashCode():0);
返回结果;
}
}
我发现一个泛型对
类非常有用:
public class Pair<T, R> {
T first;
R second;
public Pair(T first, R second) {
this.first = first;
this.second = second;
}
public T getFirst() {
return first;
}
public R getSecond() {
return second;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
if (second != null ? !second.equals(pair.second) : pair.second != null) return false;
return true;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
}
公共类对{
T第一;
R秒;
公共对(T第一,R第二){
this.first=first;
这个秒=秒;
}
公共T getFirst(){
先返回;
}
公共R getSecond(){
返回第二;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
配对=(配对)o;
如果(first!=null?!first.equals(pair.first):pair.first!=null)返回false;
if(second!=null?!second.equals(pair.second):pair.second!=null)返回false;
返回true;
}
@凌驾
公共int hashCode(){
int result=first!=null?first.hashCode():0;
result=31*result+(秒!=null?秒。hashCode():0);
返回结果;
}
}
就像你想要一个映射
而不是集
。从映射中,你可以编写你自己的类,它有一个字符串和一个日期字段,然后重写equals方法,这样它就可以将字符串和日期与同一类型的对象进行适当的比较。这样,像ArrayList这样的集合类型的.contains方法在比较类型时就不会有任何问题。像您需要的是映射
而不是集
。通过映射,您可以编写自己的类,该类有一个字符串和一个日期字段,然后重写equals方法,以便它将字符串和日期与相同类型的对象进行适当的比较。这样,集合类型(如ArrayList)的.contains方法在比较类型时不会有任何问题。用户希望检查字符串的日期,而不是字符串的日期。这是正确的,我希望检查字符串的日期。我尝试反向应用for循环逻辑:for(stringDateMap.keySet():Date Date){}
但是我得到了错误语法错误,insert;)语句“要完成ForStatement
逻辑可以反向应用吗,或者键集是否