Java什么是按关键字搜索对象的最佳数据结构 [英] Java what's the best data structure to search objects by keywords

查看:133
本文介绍了Java什么是按关键字搜索对象的最佳数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个期刊文章类,其中包含年份,作者,标题,期刊名称,关键字等变量。

suppose I have a "journal article" class which has variables such as year, author(s), title, journal name, keyword(s), etc.

作者和关键字等变量可能被声明为 String []作者 String []关键字

variables such as authors and keywords might be declared as String[] authors and String[] keywords

什么是通过一个或几个关键字,或几个作者姓名之一或标题的一部分,在期刊论文的一组对象中搜索的最佳数据结构?

What's the best data structure to search among a group of objects of "journal paper" by one or several "keywords", or one of several author names, or part of the title?

谢谢!

================== ================================================== ======
在大家的帮助下,通过Processing环境实现的测试代码如下所示。建议非常感谢!谢谢!

========================================================================== Following everybody's help, the test code realized via the Processing environment is shown below. Advices are greatly appreciated! Thanks!

ArrayList<Paper> papers = new ArrayList<Paper>();

HashMap<String, ArrayList<Paper>> hm = new HashMap<String, ArrayList<Paper>>();

void setup(){
  Paper paperA = new Paper();
  paperA.title = "paperA";
  paperA.keywords.append("cat");
  paperA.keywords.append("dog");
  paperA.keywords.append("egg");
  //println(paperA.keywords);
  papers.add(paperA);

  Paper paperC = new Paper();
  paperC.title = "paperC";
  paperC.keywords.append("egg");
  paperC.keywords.append("cat");
  //println(paperC.keywords);
  papers.add(paperC);

  Paper paperB = new Paper();
  paperB.title = "paperB";
  paperB.keywords.append("dog");
  paperB.keywords.append("egg");
  //println(paperB.keywords); 
  papers.add(paperB);

  for (Paper p : papers) {
    // get a list of keywords for the current paper
    StringList keywords = p.keywords;

    // go through each keyword of the current paper
    for (int i=0; i<keywords.size(); i++) {
      String keyword = keywords.get(i);

      if ( hm.containsKey(keyword) ) { 
        // if the hashmap has this keyword
        // get the current paper list associated with this keyword
        // which is the "value" of this keyword
        ArrayList<Paper> papers = hm.get(keyword);        
        papers.add(p); // add the current paper to the paper list        
        hm.put(keyword, papers); // put the keyword and its paper list back to hashmap
      } else { 
        // if the hashmap doesn't have this keyword
        // create a new Arraylist to store the papers with this keyword
        ArrayList<Paper> papers = new ArrayList<Paper>();        
        papers.add(p); // add the current paper to this ArrayList        
        hm.put(keyword, papers); // put this new keyword and its paper list to hashmap
      }
    }

  }

  ArrayList<Paper> paperList = new ArrayList<Paper>();
  paperList = hm.get("egg");
  for (Paper p : paperList) {
    println(p.title);
  }
}

void draw(){}

class Paper 
{
  //===== variables =====
  int ID;
  int year;
  String title;
  StringList authors  = new StringList();
  StringList keywords = new StringList();
  String DOI;
  String typeOfRef;
  String nameOfSource;
  String abs; // abstract


  //===== constructor =====

  //===== update =====

  //===== display =====
}


推荐答案

使用 HashMap< String,JournalArticle> 数据结构。

例如

Map<String, JournalArticle> journals = new HashMap<String, JournalArticle>();
journals.put("keyword1", testJA);

if (journals.containsKey("keyword1")
{
    return journals.get("keyword1");
}

您可以将关键字作为字符串类型的键放在此地图中,但是,它只支持完全匹配类型的搜索,这意味着您必须在搜索中使用关键字(在Hashmap中存储为密钥)。

you can put your keywords as the key of String type in this map, however, it only supports "exact-match" kind of search, meaning that you have to use the keyword (stored as key in the Hashmap) in your search.

如果您正在寻找喜欢类型的搜索,我建议您将您的对象保存在支持喜欢查询的数据库中。

If you are looking for " like " kind of search, I suggest you save your objects in a database that supports queries for "like".

编辑:再想一想,我想你可以做一些 - 一种喜欢的查询(就像SQL中的like子句一样),但效率不会太好,因为无论何时进行查询,你都会遍历HashMap中的所有键。如果你知道正则表达式,您可以通过修改以下示例代码来执行各种查询(例如key.matches(pattern)):

on a second thought, I think you can do some-kind-of "like" queries (just like the like clause in SQL), but the efficiency is not going to be too good, because you are iterating through all the keys in the HashMap whenever you do a query. If you know regex, you can do all kinds of queries with modification of the following example code (e.g. key.matches(pattern)):

    List<JournalArticle> results = null;

    for (String key : journals.keySet())
    {
        if (key.contains("keyword"))  /* keyword has to be part of the key stored in the HashMap, but does not have to be an exact match any more */
            results.add(journals.get(key));
    }

    return results;

这篇关于Java什么是按关键字搜索对象的最佳数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆