hashmap
哈希节点
package hashmap;
public class Node {
private Object value;
private Object key;
private Node next;
public Node() {
}
public Node(Object key,Object value) {
this.key = key;
this.value = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Object getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
哈希表
package hashmap;
import java.util.ArrayList;
import java.util.LinkedList;
public class MyHash {
private int size = 8;
private int number = 0;
private ArrayList<LinkedList> array_head = new ArrayList<LinkedList>(size);
public MyHash() {
for(int i=0;i<size;i++) {
LinkedList mylist = new LinkedList();
array_head.add(mylist);
}
}
public void put(Object key,Object value) {
if(number/size == 10) {
rehash();
}
number++;
Node new_node = new Node(key,value);
int code = hashcode(key.toString());
int position = locate(code);
LinkedList list_head = (LinkedList) array_head.get(position);
list_head.add(new_node);
}
private void rehash() {
}
public Object get(Object key) {
int code = hashcode(key.toString());
int position = locate(code);
LinkedList list_head = (LinkedList) array_head.get(position);
for(int i=0;i<list_head.size();i++) {
Node head = (Node) list_head.get(i);
Node node = head;
while(node!=null) {
if(node.getKey().equals(key)) {
return node.getValue();
}
node = node.getNext();
}
}
return null;
}
public Object remove(Object key) {
number--;
int code = hashcode(key.toString());
int position = locate(code);
LinkedList list_head = (LinkedList) array_head.get(position);
for(int i=0;i<list_head.size();i++) {
Node head = (Node) list_head.get(i);
Node node = head;
while(node!=null) {
if(node.getKey().equals(key)) {
Object value = node.getValue();
list_head.remove(node);
return value;
}
node = node.getNext();
}
}
return null;
}
public Object replace(Object key,Object newvalue) {
System.out.println("replace");
int code = hashcode(key.toString());
int position = locate(code);
LinkedList list_head = (LinkedList) array_head.get(position);
for(int i=0;i<list_head.size();i++) {
Node head = (Node) list_head.get(i);
Node node = head;
while(node!=null) {
if(node.getKey().equals(key)) {
Object oldvalue = node.getValue();
node.setValue(newvalue);
return oldvalue;
}
node = node.getNext();
}
}
return null;
}
public boolean containsKey(Object key) {
int code = hashcode(key.toString());
int position = locate(code);
LinkedList list_head = (LinkedList) array_head.get(position);
for(int i=0;i<list_head.size();i++) {
Node head = (Node) list_head.get(i);
Node node = head;
while(node!=null) {
if(node.getKey().equals(key)) {
return true;
}
node = node.getNext();
}
}
return false;
}
public Object containsValue(Object value) {
for(int k=0;k<size;k++) {
LinkedList list_head = (LinkedList) array_head.get(k);
for(int i=0;i<list_head.size();i++) {
Node head = (Node) list_head.get(i);
Node node = head;
while(node!=null) {
if(node.getValue().equals(value)) {
return true;
}
node = node.getNext();
}
}
}
return false;
}
public void show() {
System.out.println("打印哈希表");
for(int i=0;i<size;i++) {
LinkedList list_head = array_head.get(i);
System.out.println("链表 :"+(i+1));
for(int j=0;j<list_head.size();j++) {
Node head = (Node) list_head.get(j);
Node node = head;
while(node!=null) {
System.out.print("节点"+(j+1)+" :("+node.getKey()+":"+node.getValue()+")"+"-->");
node = node.getNext();
}
}
System.out.println();
}
System.out.println();
}
public int size() {
return number;
}
public void clear() {
for(int i=0;i<size;i++) {
LinkedList list_head = array_head.get(i);
list_head.clear();
}
number = 0;
}
public int hashcode(String s) {
int k = 0;
for(int i=0;i<s.length();i++) {
k += s.charAt(i);
}
return k;
}
public int locate(int k) {
int m = k%size;
return m;
}
}
布隆过滤器
import java.util.BitSet;
public class SimpleBloomFilter {
private static final int DEFAULT_SIZE = 2 << 24;
private static final int[] seeds = new int[] {7, 11, 13, 31, 37, 61,};
private BitSet bits = new BitSet(DEFAULT_SIZE);
private SimpleHash[] func = new SimpleHash[seeds.length];
public static void main(String[] args) {
String value = " stone2083@yahoo.cn ";
System.out.println(DEFAULT_SIZE);
SimpleBloomFilter filter = new SimpleBloomFilter();
System.out.println(filter.contains(value));
filter.add(value);
System.out.println(filter.contains(value));
}
public SimpleBloomFilter() {
for (int i = 0; i < seeds.length; i++) {
func[i] = new SimpleHash(DEFAULT_SIZE, seeds[i]);
}
}
public void add(String value) {
for (SimpleHash f : func) {
bits.set(f.hash(value), true);
}
}
public boolean contains(String value) {
if (value == null) {
return false;
}
boolean ret = true;
for (SimpleHash f : func) {
ret = ret && bits.get(f.hash(value));
}
return ret;
}
public static class SimpleHash {
private int cap;
private int seed;
public SimpleHash(int cap, int seed) {
this.cap = cap;
this.seed = seed;
}
public int hash(String value) {
int result = 0;
int len = value.length();
for (int i = 0; i < len; i++) {
result = seed * result + value.charAt(i);
}
return (cap - 1) & result;
}
}
}
string
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collections;
import java.util.Locale;
public class MyString implements Serializable, Comparable<String>, CharSequence {
private static final long serialVersionUID = -204188373852348874L;
private final char[] value;
public MyString() {
value = new char[0];
}
public MyString(String str) {
this.value = str.toCharArray();
}
public MyString(char[] value) {
this.value = value;
}
public MyString(byte[] bytes) {
this.value = new char[bytes.length];
for(int i = 0; i < bytes.length; i++) {
this.value[i] = (char) bytes[i];
}
}
public MyString(char value[], int offset, int count) {
if(offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if(count <= 0) {
if(count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if(offset <= value.length) {
this.value = new char[0];
return;
}
}
if(offset > value.length-count) {
throw new StringIndexOutOfBoundsException(offset);
}
this.value = Arrays.copyOfRange(value, offset, count);
}
@Override
public int length() {
return value.length;
}
@Override
public char charAt(int index) {
if(index > this.value.length || index < 0) {
throw new StringIndexOutOfBoundsException(index);
}
return this.value[index];
}
@Override
public CharSequence subSequence(int start, int end) {
if(start < 0) {
throw new StringIndexOutOfBoundsException(start);
}
if(end > value.length) {
throw new StringIndexOutOfBoundsException(end);
}
if(start > end) {
throw new StringIndexOutOfBoundsException("start index is bigger than end index.");
}
MyString result = new MyString(Arrays.copyOfRange(value, start, end));
return result;
}
@Override
public int compareTo(String o) {
return 0;
}
public int compareTo(MyString anotherString) {
if(this == anotherString) {
return 0;
}
return 0;
}
@Override
public String toString() {
return new String(this.value);
}
public boolean equals(Object anObject) {
if(this == anObject) {
return true;
}
if(anObject instanceof MyString) {
MyString anString = (MyString)anObject;
if(anString.length() == value.length) {
char[] v1 = anString.value;
char[] v2 = value;
int n = v1.length;
while(n-- != 0) {
if(v1[n] != v2[n]) {
return false;
}
}
return true;
}
}
return false;
}
public boolean contains(CharSequence s) {
return true;
}
public int indexOf(int ch) {
return indexOf(0, ch);
}
public int indexOf(int ch, int fromIndex){
for(int i = 0; i < value.length; i++) {
if(value[i] == ch) {
return i;
}
}
return -1;
}
public int indexOf(MyString str) {
return indexOf(str, 0);
}
public int indexOf(MyString str, int fromIndex){
char[] source = value;
char[] target = str.value;
for(int i = fromIndex; i < source.length; i++) {
if(source[i] == target[0]){
int index = i;
int sameLen = 1;
for(int j = 1; j < target.length && (i+j) < source.length; j++) {
if(target[j] != source[i+j]) {
break;
}
sameLen++;
}
if(sameLen == target.length) {
return index;
}
}
}
return -1;
}
public MyString concat(MyString str) {
if(str.length() == 0) {
return this;
}
int len = value.length;
int otherLen = str.length();
char[] buf = Arrays.copyOf(value, len + otherLen);
System.arraycopy(str.value, 0, buf, len, otherLen);
return new MyString(buf);
}
public boolean startsWith(MyString prefix) {
return this.startsWith(prefix, 0);
}
public boolean startsWith(MyString prefix, int toffset){
char[] target = prefix.value;
char[] source = value;
int sameLen = 0;
for(int i = 0; i < target.length && (toffset+i) < source.length; i++) {
if(source[toffset+i] != target[i]) {
break;
}
sameLen++;
}
if(sameLen == target.length) {
return true;
}
return false;
}
public MyString toLowerCase(){
return this.toLowerCase(Locale.getDefault());
}
public MyString toLowerCase(Locale locale){
if(locale == null) {
throw new NullPointerException();
}
char[] newString = new char[value.length];
for(int i = 0; i < value.length; i++) {
if(value[i] >= 'A' && value[i] <= 'Z') {
newString[i] = (char) (value[i] + 32);
} else {
newString[i] = value[i];
}
}
return new MyString(newString);
}
public MyString toUpperCase(){
return this.toUpperCase(Locale.getDefault());
}
public MyString toUpperCase(Locale locale) {
if(locale == null) {
throw new NullPointerException();
}
char[] newString = new char[value.length];
for(int i = 0; i < value.length; i++) {
if(value[i] >= 'a' && value[i] <= 'z') {
newString[i] = (char) (value[i] - 32);
} else {
newString[i] = value[i];
}
}
return new MyString(newString);
}
public MyString substring(int beginIndex){
if(beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
return beginIndex==0?this:new MyString(value, beginIndex, value.length-beginIndex);
}
public MyString substring(int beginIndex, int endIndex){
if(beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if(endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
return beginIndex==0?this:new MyString(value, beginIndex, endIndex);
}
public char[] toCharArray() {
char[] charArr = new char[value.length];
System.arraycopy(value, 0, charArr, 0, value.length);
return charArr;
}
}