zoukankan      html  css  js  c++  java
  • AbstractMap模拟实现

    import java.util.*;
    import java.util.Map.Entry;
    /**
    * Created by Administrator on 2017/7/25.
    */
    public abstract class AbstractMap_Imitate<K,V> {
    public abstract Set<Entry<K,V>> entrySet();
    transient Set<K> keySet;
    transient Collection<V> values;
    protected AbstractMap_Imitate(){}

    public int size(){return entrySet().size();}

    public boolean isEmpty(){return size() == 0;}

    public boolean containsVlaue(Object value){
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if(value == null){
    while(i.hasNext()){
    if(i.next().getValue() == null)
    return true;
    }
    }else{
    while(i.hasNext()){
    if(i.next().getValue().equals(value))
    return true;
    }
    }
    return false;
    }

    public boolean containsKey(Object key){
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if(key == null){
    while(i.hasNext()){
    if(i.next().getKey() == null)
    return true;
    }
    }else{
    while(i.hasNext()){
    if(i.next().getKey().equals(key))
    return true;
    }
    }
    return false;
    }

    public V getValue(Object key){
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if(key == null){
    while(i.hasNext()){
    Entry<K,V> e = i.next();
    if(e.getKey() == null)
    return e.getValue();
    }
    }else{
    while(i.hasNext()){
    Entry<K,V> e = i.next();
    if(key.equals(e.getKey()))
    return e.getValue();
    }
    }
    return null;
    }
    public V put(K key, V value){throw new UnsupportedOperationException();}

    public V remove(Object key){
    Iterator<Entry<K,V>> i = entrySet().iterator();
    Entry<K,V> correctEntry = null;
    if(key == null){
    while(correctEntry == null && i.hasNext()){
    Entry<K,V> e = i.next();
    if(e.getKey() == null)
    correctEntry = e;
    }
    }else{
    while(correctEntry == null && i.hasNext()){
    Entry<K,V> e = i.next();
    if(key.equals(e.getKey()))
    correctEntry = e;
    }
    }
    V oldValue = null;
    if(correctEntry != null){
    oldValue = correctEntry.getValue();
    i.remove();
    }
    return oldValue;
    }

    public void putAll(Map<? extends K, ? extends V> map){
    for(Map.Entry<? extends K, ? extends V> e : map.entrySet()){
    put(e.getKey(), e.getValue());
    }
    }

    public void clear(){
    entrySet().clear();
    }

    public Set<K> keySet(){
    Set<K> ks = keySet;
    if(ks == null){
    ks = new AbstractSet<K>() {
    @Override
    public Iterator<K> iterator() {
    return new Iterator<K>() {
    private Iterator<Entry<K,V>> i = entrySet().iterator();
    @Override
    public boolean hasNext() {
    return i.hasNext();
    }

    @Override
    public K next() {
    return i.next().getKey();
    }
    };
    }

    @Override
    public int size() {
    return AbstractMap_Imitate.this.size();
    }
    };
    keySet = ks;
    }
    return ks;
    }

    public Collection<V> values(){
    Collection<V> vals = values;
    if(vals == null){
    vals = new AbstractCollection<V>() {
    @Override
    public Iterator<V> iterator() {
    return new Iterator<V>() {
    Iterator<Entry<K,V>> i = entrySet().iterator();
    @Override
    public boolean hasNext() {
    return i.hasNext();
    }

    @Override
    public V next() {
    return i.next().getValue();
    }
    };
    }

    @Override
    public int size() {
    return AbstractMap_Imitate.this.size();
    }
    };
    values = vals;
    }
    return vals;
    }

    public boolean equals(Object o){
    if(o == this){
    return true;
    }
    if(!(o instanceof Map)){
    return false;
    }
    Map<?,?> m = (Map<?,?>)o;
    if(m.size() != size()){
    return false;
    }
    try{
    Iterator<Entry<K,V>> i = entrySet().iterator();
    while(i.hasNext()){
    Entry<K,V> e = i.next();
    K key = e.getKey();
    V value = e.getValue();
    if(value == null){
    if(!(m.get(key) == null && m.containsKey(key))){
    return false;
    }
    }else {
    if(!(value.equals(m.get(key)))){
    return false;
    }
    }
    }
    }catch (ClassCastException unused){
    return false;
    }catch (NullPointerException unused){
    return false;
    }
    return true;
    }

    public int hashCode(){
    int h = 0;
    Iterator<Entry<K,V>> i = entrySet().iterator();
    while(i.hasNext()){
    h += i.next().hashCode();
    }
    return h;
    }

    public String toString(){
    Iterator<Entry<K,V>> i = entrySet().iterator();
    if(!i.hasNext()){
    return "{}";
    }
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    for(;;){
    Entry<K,V> e = i.next();
    K key = e.getKey();
    V value = e.getValue();
    sb.append(key == this? "this map" : key);
    sb.append("=");
    sb.append(value == this? "this map" : value);
    if(!i.hasNext()){
    return sb.append("}").toString();
    }
    sb.append(",").append(" ");
    }
    }

    protected Object clone() throws CloneNotSupportedException{
    AbstractMap_Imitate<?,?> result = (AbstractMap_Imitate<?,?>)super.clone();
    result.keySet = null;
    result.values = null;
    return result;
    }
    //实现Entry接口
    public static class simpleEntry<K,V> implements Entry<K,V>, java.io.Serializable{
    @Override
    public K getKey() {
    return null;
    }

    @Override
    public V getValue() {
    return null;
    }

    @Override
    public V setValue(V value) {
    return null;
    }
    }
    }
  • 相关阅读:
    IOS数组
    caffe-fasterrcnn程序理解
    pytorch官网上两个例程
    python:批量修改文件名批量修改图片尺寸
    faster rcnn报错:TypeError: slice indices must be integers or None or have an __index__ method
    faster-rcnn原理讲解
    caffe + ssd网络训练过程
    运行Keras版本的Faster R-CNN(1)
    Ubuntu16.04 faster-rcnn+caffe+gpu运行环境配置以及解决各种bug
    ubuntu16+caffe fast-rcnnCPU运行步骤
  • 原文地址:https://www.cnblogs.com/zawjdbb/p/7234954.html
Copyright © 2011-2022 走看看