Intersection of words I get a map from DB called doc1 and I also have the arrayList called someWord.  I will find the subscription of doc1 and someWord and store it in doc2.  I will also add the intersecting words with zero index in doc2.

This code consumes too much time:

public void setDocuments() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor&lt;Integer&gt;("sky"));
    neighbors.add(new Neighbor&lt;Integer&gt;("earth"));

    Map&lt;Long, String&gt; docInfo;
    // Document is a Map
    Document&lt;String&gt; doc1 = new Document&lt;String&gt;();
    Document&lt;Integer&gt; doc2;
    Map&lt;Integer, Long&gt; sorted;
    ArrayList&lt;String&gt; intersectionWords;
    HashSet&lt;String&gt; different;
    try {
        docInfo = dbDocMeta.getDocInfo();
        String family;
        int index = 0;
        for (Long id : docInfo.keySet()) {
            family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            for (Neighbor&lt;Integer&gt; neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (neighbor.getFamily().equalsIgnoreCase(family)) {
                    different = new HashSet&lt;String&gt;();
                    different.addAll(someWord);
                    doc1 = dbWords.getNeighbors(id);
                    doc2 = new Document&lt;Integer&gt;();
                    intersectionWords = new ArrayList&lt;String&gt;();
                    for (String word : doc1.getMap().keySet()) {
                        index = dbWords.getIndex(word);
                        if (selectedFeatures.contains(word)) {
                            doc2.add(index, doc1.getAttributes().get(word));
                            intersectionWords.add(word);
                        }
                    }
                    if (!doc2.getMap().isEmpty()) {
                        different.removeAll(intersectionWords);
                        Iterator&lt;String&gt; iterator = different.iterator();
                        while (iterator.hasNext()) {
                            String s = iterator.next();
                            index = dbWords.getIndex(s);
                            doc2.add(index, 0);
                        }
                        sorted = new TreeMap&lt;Integer, Long&gt;(doc2.getAttributes());
                        doc2.setMap(sorted);
                        neighbor.addDocument(doc2);
                        break;
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}


Neighbour variables are:

private ArrayList&lt;Document&lt;T&gt;&gt;  documents;
private int                     docCount;
private String                  family;


It also contains some setters and getters, and dbDocMeta contains the ID of all Documents.
You should declare variables in the smallest possible scope. E.g. instead of

String family;
for  (...) {
    family = ...;
    ...
}


we could do

for (...) {
    String family = ...;
    ...
}


The same applies to docInfo, doc1, doc2, sorted, intersectionWords, different, and index.

You have this snippet:

Iterator&lt;String&gt; iterator = different.iterator();
while (iterator.hasNext()) {
    String s = iterator.next();
    ...
}


which could be abbreviated to for (String s : different) { ... }.

If we clean up these two points, we end up with this:

public void setDocunents() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor&lt;Integer&gt;("sky"));
    neighbors.add(new Neighbor&lt;Integer&gt;("earth"));

    try {
        Map&lt;Long, String&gt; docInfo = dbDocMeta.getDocInfo();
        for (Long id : docInfo.keySet()) {
            String family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            for (Neighbor&lt;Integer&gt; neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (neighbor.getFamily().equalsIgnoreCase(family)) {
                    HashSet&lt;String&gt; different = new HashSet&lt;String&gt;();
                    different.addAll(someWord);
                    Document&lt;String&gt; doc1 = dbWords.getNeighbors(id);
                    Document&lt;Integer&gt; doc2 = new Document&lt;Integer&gt;();
                    ArrayList&lt;String&gt; intersectionWords = new ArrayList&lt;String&gt;();
                    for (String word : doc1.getMap().keySet()) {
                        int index = dbWords.getIndex(word);
                        if (selectedFeatures.contains(word)) {
                            doc2.add(index, doc1.getAttributes().get(word));
                            intersectionWords.add(word);
                        }
                    }
                    if (!doc2.getMap().isEmpty()) {
                        different.removeAll(intersectionWords);
                        for (String s : different) {
                            int index = dbWords.getIndex(s);
                            doc2.add(index, 0);
                        }
                        Map&lt;Integer, Long&gt; sorted = new TreeMap&lt;Integer, Long&gt;(doc2.getAttributes());
                        doc2.setMap(sorted);
                        neighbor.addDocument(doc2);
                        break;
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}


In a next step, we remove calculations from inner loops that don't have to be recalculated each time. E.g. doc1 = dbWords.getNeighbors(id) depends only on id, and probably does not have to be called for each neighbor. On the other hand, different is only needed inside a specific if branch.

Furthermore, doc2 will be discarded unless that branch is entered, so we will build it only in that case (assuming doc2.getMap().isEmpty() is equivalent to intersectionWords.isEmpty().

We can further remove unnecessary variables – their types just add clutter to the code.

A bare break inside nested loops can be a bit confusing, so we'll use the labelled for: break neighbors.

The HashSet constructor can directly take a collection, there is no need to separately call addAll.

If the whole body of a loop us subject to an if (cond), it is often easier to read when at the beginning of a loop, we if (!cond) continue.

Unrelated implementation details like calculating the intersection of two sets should be factored out into helper functions.

public void setDocunents() {
    // Neighbor contain the arrayList of Documents
    neighbors.add(new Neighbor&lt;Integer&gt;("sky"));
    neighbors.add(new Neighbor&lt;Integer&gt;("earth"));

    try {
        Map&lt;Long, String&gt; docInfo = dbDocMeta.getDocInfo();
        for (Long id : docInfo.keySet()) {
            String family = docInfo.get(id).contains("sky") ? "sky" : "earth";
            Document&lt;String&gt; doc1 = dbWords.getNeighbors(id);

            neighbors:
            for (Neighbor&lt;Integer&gt; neighbor : neighbors) {
                // check if the document in the Neighbour family
                if (!neighbor.getFamily().equalsIgnoreCase(family)) {
                    continue neighbors;
                }

                Set&lt;String&gt; intersectionWords = intersection&lt;String&gt;(doc1.getMap().keySet(), selectedFeatures);
                if (intersectionWords.isEmpty()) {
                    continue neighbors;
                }

                HashSet&lt;String&gt; different = new HashSet&lt;String&gt;(someWord);
                Document&lt;Integer&gt; doc2 = new Document&lt;Integer&gt;();
                for (String s : intersectionWords) {
                    doc2.add(dbWords.getIndex(word), doc1.getAttributes().get(word));
                    different.remove(s);
                }
                for (String s : different) {
                    doc2.add(dbWords.getIndex(s), 0);
                }
                // due to the choice of a TreeMap, the elements are sorted
                doc2.setMap(new TreeMap&lt;Integer, Long&gt;(doc2.getAttributes()));
                neighbor.addDocument(doc2);

                break neighbors;
            }
        }
    } catch (InterruptedException e) {
        logger.error("InterruptedException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    } catch (ClassNotFoundException e) {
        logger.error("ClassNotFoundException msg : {} \n {}", e.getMessage(), e.getStackTrace());
    }
}

private static &lt;A&gt; Set&lt;A&gt; intersection(final Set&lt;A&gt; xs, final Set&lt;A&gt; ys) {
    // make sure that xs is the smaller set
    if (ys.size() &lt; xs.size()) {
        return intersection&lt;A&gt;(ys, xs);
    }

    final HashSet&lt;A&gt; result = new HashSet&lt;&gt;();
    for (A x : xs) {
        if (ys.contains(x)) {
            result.add(x)
        }
    }

    return result;
}


This could be optimized further, e.g. when doc2.add(…) has no side effects except setting the underlying map, or if the composition of someWord is known.