title: Convert list to hashmap
body: <p>Title of the question may give you the impression that it is duplicate question, but according to me it is not.</p>
  
  <p>I am just a few months old in Java and a month old in MongoDB, SpringBoot and REST.</p>
  
  <p>I have a Mongo Collection with 3 fields in a document, <strong>_id (default field), appName and appKey</strong>. I am using <strong>list</strong> to iterate through all the documents and find one document whose appName and appKey matches with the one that is passed. This collection right now has only 4 entries, and thus it is running smoothly. But I was reading a bit about collections and found that if there will be a higher number of documents in a collection then the result with <strong>list</strong> will be much slower than <strong>hashMap</strong>.</p>
  
  <p>But as I have already said that I am quite new to Java, I am having a bit of trouble converting my code to <strong>hashMap</strong>, so I was hoping if someone can guide me through this.</p>
  
  <p>I am also attaching my code for reference.</p>
  
  <pre><code>public List&lt;Document&gt; fetchData() {
      // Collection that stores appName and appKey
      MongoCollection&lt;Document&gt; collection = db.getCollection("info");
      List&lt;Document&gt; nameAndKeyList = new ArrayList&lt;Document&gt;();
  
      // Getting the list of appName and appKey from info DB
      AggregateIterable&lt;Document&gt; output = collection
              .aggregate(Arrays.asList(new BasicDBObject("$group", new BasicDBObject("_id",
                      new BasicDBObject("_id", "$id").append("appName", "$appName").append("appKey", "$appKey"))
  
      )));
      for (Document doc : output) {
  
          nameAndKeyList.add((Document) doc.get("_id"));
  
      }
      return nameAndKeyList;
  
  }// End of Method
  </code></pre>
  
  <p>And then I am calling it in another method of the same class:</p>
  
  <pre><code>List&lt;Document&gt; nameAndKeyList = new ArrayList&lt;&gt;();
  
          //InfoController is the name of the class
          InfoController obj1 = new InfoController();
          nameAndKeyList = obj1.fetchData();
  
          // Fetching and checking if the appName &amp; appKey pair
          // is present in the DB one by one.
          // If appName &amp; appKey mismatches, it increments the value
          // of 'i' and check them with the other values in DB
          for (int i = 0; i &lt; nameAndKeyList.size(); i++) {
  
             "followed by my code"
  </code></pre>
  
  <p>And if I am not wrong then there will be no need for the above loop also.</p>
  
  <p>Thanks in advance.</p>