简体   繁体   English

如何检查Set是否包含一个成员变量等于某个值的对象

[英]How to check if a Set contains an object which has one member variable equal to some value

I have a java Set of Result objects. 我有一组java的Result对象。 My Result class definition looks like this: 我的Result类定义如下所示:

private String url;
private String title;
private Set<String> keywords;

I have stored my information in a database table called Keywords which looks like this 我已将我的信息存储在名为Keywords的数据库表中,如下所示

Keywords = [id, url, title, keyword, date-time] 关键字= [id,url,title,keyword,date-time]

As you can see there isn't a one-to-one mapping between an object and a row in the database. 如您所见,数据库中的对象和行之间没有一对一的映射。 I am using SQL (MySQL DB) to extract the values and have a suitable ResultSet object. 我正在使用SQL(MySQL DB)来提取值并拥有合适的ResultSet对象。

How do I check whether the Set already contains a Result with a given URL. 如何检查Set是否已包含具有给定URL的Result。

If the set already contains a Result object with the current URL I simply want to add the extra keyword to the Set of keywords, otherwise I create a new Result object for adding to the Set of Result objects. 如果集合已经包含带有当前URL的Result对象,我只想将extra关键字添加到Set of keywords中,否则我创建一个新的Result对象以添加到Result of Result对象。

When you iterate over the JDBC resultSet (to create your own set of Results) why don't you put them into a Map? 当您遍历JDBC resultSet(创建自己的结果集)时,为什么不将它们放入Map? To create the Map after the fact: 事后创建Map:

Map<String, List<Result>> map = new HashMap<String, List<Result>>();
for (Result r : resultSet) {
    if (map.containsKey(r.url)) {
        map.get(r.url).add(r);
    } else {
        List<Result> list = new ArrayList<Result>();
        list.add(r);
        map.put(r.url, list);
    }
}

Then just use map.containsKey(url) to check. 然后只需使用map.containsKey(url)进行检查。

If it's possible, I suggest changing your database design to eliminate this problem. 如果可能,我建议您更改数据库设计以消除此问题。 Your current design requries storing the id, url, title and date-time once per key word, which could waste quite a bit of space if you have lots of key words 您当前的设计要求每个关键字存储一次id,url,title和date-time,如果你有很多关键词,可能会浪费相当多的空间

I would suggest having two tables. 我建议有两张桌子。 Assuming that the id field is guarenteed to be unique, the first table would store the id, url, title and date-time and would only have one row per id. 假设id字段被保证是唯一的,第一个表将存储id,url,title和date-time,并且每个id只有一行。 The second table would store the id and a key word. 第二个表将存储id和关键字。 You would insert multiple rows into this table as required. 您可以根据需要在此表中插入多行。

Is that possible / does that make sense? 这有可能吗/这有意义吗?

You can use a Map with the URLs as the keys: 您可以使用带有URL的Map作为键:

Map<String, Result> map = new HashMap<String, Result>();

for (Result r : results) {
    if (map.containsKey(r.url)) {
        map.get(r.url).keywords.addAll(r.keywords);
    } else {
        map.put(r.url, r);
    }
}

I think that you need to make an override on equals() method of your Result class. 我认为你需要在Result类的equals()方法上进行覆盖。 In that method you will put your logic that will check what you are looking for. 在那种方法中,您将使用您的逻辑来检查您要查找的内容。

NB You also need to know that overrideng the equals() method, you need to override also hashCode() method. 注意您还需要知道覆盖equals()方法,还需要覆盖hashCode()方法。

For more on "overriding equals() and hashCode() methods" topic you can look at the this another question. 欲了解更多关于“重写equals()和hashCode()方法”主题,你可以看看一个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 Set 包含具有某些字符串值的对象,如何检查 Java? - How to check in java if Set contains object with some string value? 如何创建一个包含多个对象的JList? 例如价格,重量,颜色等 - How to create a JList that contains an object which has more than one value? e.g. Price, Weight, Colour etc 如何检查只包含一个单词的字符串。 如果一个字符串有一个句子,它应该返回false - How to check a string which only contains one word. If a string has a sentence it should return false 我如何检查ArrayList中的每个对象的值是否等于零? - How can i check if every object in my ArrayList has a value equal to zero? 如何序列化具有一个成员(即json字符串)的Java对象 - How can I serialize a Java object that has one member which is a json string 解析Java中的复杂json对象,该对象在一个节点中具有一些int值 - parse a complex json object in java which has some int value in one node 如何检查一个字符串是否包含一系列相等的符号,这些符号与同一字符串中的另一个符号序列相等? (爪哇) - How to check if a string contains a sequence of equal symbols, which are are equal to another sequence of symbols in the same string? (JAVA) 检查类是否有成员变量? - Check if class has member variable? 如何检查一个数组是否包含相同的值? - How to check if one array contains value that is the same? 如何检查一组对象是否包含 object,该 object 的特定字段在 Java 中有特定值? - How can I check if a Set of objects contains an object having a specific fields with a specific value in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM