title: Guava Sets.intersection bad performance
body: <p>I've encountered a strange problem in production today. Though I love Guava, I ran into a use case where Guava's <code>Sets.intersection()</code> was performing pretty badly. I've written a sample code: </p>
  
  <pre><code>Set&lt;Long&gt; cache = new HashSet&lt;&gt;();
  for (long i = 0; i &lt; 1000000; i++) {
      cache.add(i);
  }
  Set&lt;Long&gt; keys = new HashSet&lt;&gt;();
  for (long i = 0; i &lt; 100; i++) {
      keys.add(i);
  }
  long start = System.currentTimeMillis();
  Set&lt;Long&gt; foundKeys = new HashSet&lt;&gt;();
  for (Long key : keys) {
      if (cache.contains(key)) {
          foundKeys.add(key);
      }
  }
  System.out.println("Java search: " + (System.currentTimeMillis() - start));
  start = System.currentTimeMillis();
  SetView&lt;Long&gt; intersection = Sets.intersection(keys, cache);
  System.out.println("Guava search: " + (System.currentTimeMillis() - start));
  </code></pre>
  
  <p>I've tried to create a similar production scenario where I've a keys cache, and I'm looking for all the keys present in the cache. Strangely, Guava search is taking a lot longer than Java search. After running this I got:</p>
  
  <pre><code>Java search: 0
  Guava search: 36
  </code></pre>
  
  <p>Can anyone tell why this isn't suited for my use case or is there a bug in Guava?</p>