1

I have two Java classes. The first one is called "Index", and the second one, temporarily, I called it "IndexCache":

public class IndexCache {
    private static Map<String, Index> map = null;

    static {
        map = new HashMap<String, Index>();
    }

    @Override
    public Index getIndexString(key) {
        Index index = map.get(key);

        if (index == null) {
            index = new Index("yadayadayada");
            map.put(key, index);
        }

        return index;

    }

}

As you can see, IndexCache keeps a map of the Index objects to avoid to create the same object once and again.

Well, what programming technique is this? What would be its name?

  • 3
    I think you're running into memoization – Izkata Mar 3 '15 at 4:38
  • @Izkata Thank you. That's what I was searching for. – Mario S Mar 3 '15 at 4:42
  • 9
    Memoization is concerned with avoiding extra computations, such as functions that are expensive to calculate. You actually answered your own question with the class's name: this is simply a cache. This is normally used to avoid creating redundant objects. A similar concept is an object pool, often used to avoid expensive initialization logic (e.g. with database connections). – user22815 Mar 3 '15 at 5:20
12

It's called a cache, the technique is called caching.

A cache is something that stores data in order to speed up further requests for that same data. That data may be the result of some expensive computation and thus be cached to avoid repeating that costly computation. It might also be a copy of some data that is already pre-computed elsewhere but is costly to retrieve (e.g. caching filesystem blocks from a slow harddisk in RAM or caching HTTP documents from a remote server on a local proxy cache).

A cache is populated dynamically with different values each time, and may also grow and shrink.

This is different from a lookup table, which is populated statically, always with the same values, often even hardcoded into the source code or pre-computed at compilation time.

Memoization is a special case, it refers specifically to caching the results of function application with the same arguments.

| improve this answer | |
  • I think memoization is the general technique, caching is an implementation of memoization. – kevin cline Mar 4 '15 at 1:01
  • 2
    @kevincline: Hmm … I have never heard of, say, the L2 cache on a Core2 Duo, or a caching HTTP proxy, or a filesystem cache being referred to as "memoization". Memoization, the way I have seen it used, refers specifically to caching the results of function applications with the same arguments. Sometimes automatically (as in GHC's call-by-need-with-memoization evaluation strategy), sometimes as a manual speedup for some algorithm. – Jörg W Mittag Mar 4 '15 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.