title: Java what's the best data structure to search objects by keywords
body: <p>suppose I have a "journal article" class which has variables such as year, author(s), title, journal name, keyword(s), etc. </p>
  
  <p>variables such as authors and keywords might be declared as <strong>String[] authors</strong> and <strong>String[] keywords</strong></p>
  
  <p>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? </p>
  
  <p>Thanks!</p>
  
  <p>==========================================================================
  Following everybody's help, the test code realized via the Processing environment is shown below. Advices are greatly appreciated! Thanks!</p>
  
  <pre><code>ArrayList&lt;Paper&gt; papers = new ArrayList&lt;Paper&gt;();
  
  HashMap&lt;String, ArrayList&lt;Paper&gt;&gt; hm = new HashMap&lt;String, ArrayList&lt;Paper&gt;&gt;();
  
  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&lt;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&lt;Paper&gt; 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&lt;Paper&gt; papers = new ArrayList&lt;Paper&gt;();        
          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&lt;Paper&gt; paperList = new ArrayList&lt;Paper&gt;();
    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 =====
  }
  </code></pre>