Skip to content

Instantly share code, notes, and snippets.

@tarunbod
Last active December 23, 2015 03:23
Show Gist options
  • Save tarunbod/bc0c9e6400ac7eaf7c2f to your computer and use it in GitHub Desktop.
Save tarunbod/bc0c9e6400ac7eaf7c2f to your computer and use it in GitHub Desktop.
ThreeMap - A hash map with three arguments.
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
/**
* <p/>
* The MIT License (MIT)
* <p/>
* Copyright (c) 2015 Tarun Boddupalli
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
public class ThreeMap<K, V, E> {
private LinkedList<ThreeEntrySet<K, V, E>> entries = new LinkedList<>();
public void put(K key, V value1, E value2) {
for (ThreeEntrySet<K, V, E> set : entries) {
if (set.getKey().equals(key)) {
set.setKey(key);
set.setFirstVal(value1);
set.setSecondVal(value2);
return;
}
}
entries.add(new ThreeEntrySet<>(key, value1, value2));
}
public void remove(K key) {
ThreeEntrySet<K, V, E> tempSet = null;
for (ThreeEntrySet<K, V, E> set : entries) {
if (set.getKey().equals(key)) {
tempSet = set;
}
}
entries.remove(tempSet);
}
public ThreeEntrySet<K, V, E> get(K key) {
for (ThreeEntrySet<K, V, E> set : entries) {
if (set.getKey().equals(key)) {
return set;
}
}
return null;
}
public LinkedList<ThreeEntrySet<K, V, E>> entrySet() {
return entries;
}
public static class ThreeEntrySet<K, V, E> {
private K key;
private V firstVal;
private E secondVal;
public ThreeEntrySet(K key, V firstVal, E secondVal) {
this.key = key;
this.firstVal = firstVal;
this.secondVal = secondVal;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getFirstVal() {
return firstVal;
}
public void setFirstVal(V firstVal) {
this.firstVal = firstVal;
}
public E getSecondVal() {
return secondVal;
}
public void setSecondVal(E secondVal) {
this.secondVal = secondVal;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment