java基本类实现

hashmap

哈希节点

package hashmap;

/**
 * 节点类
 * @author HP
 *
 */
public class Node {
    private Object value;
    private Object key;
    private Node next;

    /**
     * 空节点
     */
    public Node() {
    }

    /**
     * 值为key value的节点
     * @param data
     */
    public Node(Object key,Object value) {
        this.key = key;
        this.value = value;
    }


    //接下来就是一些数据和节点的set,get
    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 {
    //哈希数组的长度初始化为8
    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);//初始化的时候就将空节点头添加到数组中去
        }
    }


    /**
     * 根据 键值对 生成节点
     * 将节点放入哈希表中
     * @param key 键
     * @param value 值
     */
    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() {


    }


    /**
     * @param key
     * @param value
     * @return 返回键值对应得节点
     */
    public Object get(Object key) {

        int code = hashcode(key.toString());//得到哈希码
        int position = locate(code);//得到该哈希码所对应的哈希数组中的位置

        //找到该位置对应的链表
        LinkedList list_head = (LinkedList) array_head.get(position);

        //从头遍历链表 ,找到与键key对应的节点的value值进行输出
        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)) {
//					System.out.println("node.getValue() :"+node.getValue());
                    return node.getValue();
                }
                node = node.getNext();
            }
        }

        return null;//否则返回空
    }


    /**
     * 移除键为key的节点
     * @param key
     * @return 返回删除节点的key对应的value
     */
    public Object remove(Object key) {
        number--;
        int code = hashcode(key.toString());//得到哈希码
        int position = locate(code);//得到该哈希码所对应的哈希数组中的位置

        //找到该位置对应的链表
        LinkedList list_head = (LinkedList) array_head.get(position);

        //从头遍历链表 ,找到与键key对应的节点的value值进行输出
        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);

        //从头遍历链表 ,找到与键key对应的节点的value值进行输出
        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;//否则返回空


    }

    /**
     * @param key
     * @return 哈希表中含有该key,返回true
     */
    public boolean containsKey(Object key) {

        int code = hashcode(key.toString());//得到哈希码
        int position = locate(code);//得到该哈希码所对应的哈希数组中的位置

        //找到该位置对应的链表
        LinkedList list_head = (LinkedList) array_head.get(position);

        //从头遍历链表 ,找到与键key对应的节点的value值进行输出
        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);

            //从头遍历链表 ,找到与键key对应的节点的value值进行输出
            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();
    }

    /**
     *
     * @return 返回存储的节点的个数
     */
    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;
    }



    /**
     * 计算字符串的哈希码
     * ASCII码相加
     * @param s
     * @return
     */
    public int hashcode(String s) {
        int k = 0;
        for(int i=0;i<s.length();i++) {
            k += s.charAt(i);
        }
        return k;
    }

    /**
     * 得到哈希码对应在数组中的位置
     * @param k
     * @return
     */
    public int locate(int k) {
        int m = k%size;
        return m;
    }
}

布隆过滤器

import java.util.BitSet;

/**
 * Created by haicheng.lhc on 18/05/2017.
 *
 * @author haicheng.lhc
 * @date 2017/05/18
 */
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;

    /**
     * 定义char型数组,核心变量
     */
    private final char[] value;

    /**
     * 空构造函数
     */
    public MyString() {
        value = new char[0];
    }

    /**
     * @param str 默认String值的构造方法
     */
    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;
    }

    //TODO
    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);  //定义一个新的char[]数组,长度为原数组+要拼接的数组长度,并将原来的数组内容复制到新的数组
        System.arraycopy(str.value, 0, buf, len, otherLen);
        return new MyString(buf);
    }

    public boolean startsWith(MyString prefix) {
        return this.startsWith(prefix, 0);
    }

    /**
     * 检测字符串是否以某子串开头
     * @param   prefix  子串
     * @param   toffset 字符串的起始位置
     * @return  {@code true} 如果字符串以该子串开头就返回true; {@code false} 否则返回false.
     * @since   1. 0
     * @author SpringChang
     */

    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);
    }


    /**
     * 将字符串转换为字符数组
     * @return 新的字符数组,不能是原来的数组,否则用户可以操作和修改String内容,这样违反String final设计原则。
     */
    public char[] toCharArray() {
        //return this.value; 不能直接这样返回
        char[] charArr = new char[value.length];
        System.arraycopy(value, 0, charArr, 0, value.length);
        return charArr;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值