home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (CountMap.java)

This example Java source code file (CountMap.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

atomicdouble, concurrenthashmap, countmap, iterator, override, pair, sequenceelement, threading, threads, unsupportedoperationexception, util

The CountMap.java Java example source code

package org.deeplearning4j.models.glove.count;

import com.google.common.util.concurrent.AtomicDouble;
import lombok.Data;
import org.deeplearning4j.berkeley.Pair;
import org.deeplearning4j.models.sequencevectors.sequence.SequenceElement;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

 * Drop-in replacement for CounterMap
 * @author raver119@gmail.com
public class CountMap<T extends SequenceElement> {
    private volatile Map<Pair backingMap = new ConcurrentHashMap<>();

    public CountMap() {
        // placeholder

    public void incrementCount(T element1, T element2, double weight) {
        Pair<T, T> tempEntry = new Pair<>(element1, element2);
        if (backingMap.containsKey(tempEntry)) {
        } else {
            backingMap.put(tempEntry, new AtomicDouble(weight));

    public void removePair(T element1, T element2) {
        Pair<T, T> tempEntry = new Pair<>(element1, element2);

    public void removePair(Pair<T, T> pair) {

    public double getCount(T element1, T element2) {
        Pair<T, T> tempEntry = new Pair<>(element1, element2);
        if (backingMap.containsKey(tempEntry)) {
            return backingMap.get(tempEntry).get();
        } else return 0;

    public double getCount(Pair<T, T> pair) {
        if (backingMap.containsKey(pair)) {
            return backingMap.get(pair).get();
        } else return 0;

    public Iterator<Pair getPairIterator() {
        return new Iterator<Pair() {
            private  Iterator<Pair iterator = backingMap.keySet().iterator();

            public boolean hasNext() {
                    return iterator.hasNext();

            public Pair<T, T> next() {
                //MapEntry<T> entry = iterator.next();
                return iterator.next(); //new Pair<>(entry.getElement1(), entry.getElement2());

            public void remove() {
                throw new UnsupportedOperationException("remove() isn't supported here");

    public int size() {
        return backingMap.size();

Other Java examples (source code examples)

Here is a short list of links related to this Java CountMap.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.