public
Created

Java Simple Key Value Pair

  • Download Gist
Pair.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
package ai.ilikeplaces.util;
 
import ai.ilikeplaces.doc.License;
 
/**
* Simple Key Value Pair.
* <p/>
* <p/>
* Example Usage:
* <p/>
* <p/>
* <code>
* System.out.println(new Pair<String, Integer>().setPair("Susan Boyle", 1).toString());
* </code>
* <p/>
* <code>
* System.out.println(new Pair<String, Integer>().setKey("Susan Boyle").setValue(1).toString());
* </code>
* <p/>
* <code>
* System.out.println(new Pair<String, Integer>("Susan Boyle", 1).toString());
* </code>
* <p/>
* <p/>
* <p/>
* {@link #equals(Object)} checks only on key for equality
* <p/>
* Created by IntelliJ IDEA.
* User: <a href="http://www.ilikeplaces.com"> http://www.ilikeplaces.com </a>
* Date: Oct 10, 2010
* Time: 5:36:16 PM
*/
@License(content = "This code is licensed under Apache License Version 2.0")
public class Pair<K, V> {
private K key;
private V value;
private static final String OPEN_BRACE = "{";
private static final String COMMA = ",";
private static final String CLOSE_BRACE = "}";
 
public Pair() {
}
 
/**
* @param key key to be set
* @param value value to be set
*/
public Pair(final K key, final V value) {
this.key = key;
this.value = value;
}
 
/**
* @param key key to be set
* @param value value to be set
* @return this
*/
public Pair<K, V> setPair(final K key, final V value) {
this.key = key;
this.value = value;
return this;
}
 
/**
* @return key
*/
public K getKey() {
return key;
}
 
/**
* @param key key to be set
* @return this
*/
public Pair<K, V> setKey(final K key) {
this.key = key;
return this;
}
 
/**
* @return value
*/
public V getValue() {
return value;
}
 
/**
* @param value value to be set
* @return this
*/
public Pair<K, V> setValue(final V value) {
this.value = value;
return this;
}
 
public String toString() {
return OPEN_BRACE + key + COMMA + value + CLOSE_BRACE;
}
 
/**
* @return {key,val} as string
*/
public String toTuple() {
return OPEN_BRACE + key + COMMA + value + CLOSE_BRACE;
}
 
public static void main(String args[]) {
System.out.println(new Pair<String, Integer>().setPair("Susan Boyle", 1).toString());
 
System.out.println(new Pair<String, Integer>().setKey("Susan Boyle").setValue(1).toString());
 
System.out.println(new Pair<String, Integer>("Susan Boyle", 1).toString());
}
 
/**
* Matches checked only on key
*
* @param o
* @return
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
 
Pair pair = (Pair) o;
 
if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
 
return true;
}
 
@Override
public int hashCode() {
return key != null ? key.hashCode() : 0;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.