作为初学者编写我自己的 Java Map 实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10184554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write my own Java Map implementation as a beginner
提问by dghtr
I am new to Java Collections. I was going through Map, so please tell me just like java have provided the Map, can we also make our own Map? Let's say a Map named MineMap
. Please advise me how to achieve this. I was doing googling, and I discovered something like this:
我是 Java 集合的新手。我正在使用Map,所以请告诉我就像java提供了Map一样,我们也可以制作自己的Map吗?假设有一个名为 的地图MineMap
。请告诉我如何实现这一目标。我在谷歌上搜索,我发现了这样的事情:
public interface MyMap
{
public void put(Object key,Object value);
public Object get(Object key);
public int size();
public Set keySet();
public Set entrySet();
public interface MyEntry
{
public Object getKey();
public Object getValue();
}
}
and its implementation:
及其实施:
class MySimpleMap implements MyMap
{
private ArrayList keys;
private ArrayList values;
private int index;
public MySimpleMap()
{
keys=new ArrayList();
values=new ArrayList();
index=0;
}
public void put(Object key,Object value)
{
keys.add(key);
values.add(value);
index++;
}
public Object get(Object key)
{
int i=keys.indexOf(key);
if (i>=0)
return values.get(i);
else
return null;
}
public int size()
{
return index;
}
public Set keySet()
{
HashSet set=new HashSet();
set.addAll(keys);
return set;
}
//Nested class starts...
class MySimpleEntry implements MyMap.MyEntry
{
Object key;
Object value;
public MySimpleEntry(Object k,Object v)
{
key=k;
value=v;
}
public Object getKey()
{
return key;
}
public Object getValue()
{
return value;
}
}// Nested class ends.
public Set entrySet()
{
HashSet set=new HashSet();
for (int i=0;i<index;i++)
{
Object k=keys.get(i);
Object v=values.get(i);
MySimpleEntry temp=new MySimpleEntry(k,v);
set.add(temp);
}
return set;
}
}
and finally this was the class that was using this:
最后这是使用这个的类:
class MyMapDemo
{
public static void main(String arr[])
{
MySimpleMap map=new MySimpleMap();
map.put("Amit","Java");
map.put("Rahul",".Net");
map.put("Nitin","SQT");
map.put("Ajay","PHP");
map.put("Raman","Java");
System.out.println("There are "+map.size()+" elemenets in the map...");
System.out.println("contents of Map...");
Set s=map.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
MyMap.MyEntry m=(MyMap.MyEntry) itr.next();
System.out.println(m.getKey()+"\t"+m.getValue());
}
Scanner in=new Scanner(System.in);
System.out.println("Enter Name to find out course, ctrl+c to terminate...");
while(true)
{
System.out.println("Name:");
String n=in.nextLine();
System.out.println("Course is:"+map.get(n));
}
}
}
but I need some implemenation that would be simpler.
但我需要一些更简单的实现。
回答by neo
You mentioned that you are beginner and want to implement your own map. At beginner level it's too early to write your Map. Collection framework has already given optimized and proven implementation of several data types. It's important to understand those instead of reinventing wheel.
您提到您是初学者并希望实现自己的地图。在初学者级别,编写地图还为时过早。集合框架已经提供了几种数据类型的优化和经过验证的实现。理解这些而不是重新发明轮子很重要。
You can get java library sources easily and try to see how they have implemented collection classes. It's state of the art development and covers many minute details.
Regarding implementation.
您可以轻松获取 Java 库源并尝试查看它们是如何实现集合类的。这是最先进的开发,涵盖了许多细节。
关于实施。
However, what ever you have found can not be classified as map directly. No doubt does similar thing.
但是,您发现的任何内容都不能直接归类为地图。毫无疑问,类似的事情。
- not generic implementation.
- not implementing Map so can to use impl vice-versa with existing Map type variables.
- can not use algorithms from the Collections.class.
- 不是通用的实现。
- 不实现 Map 因此可以将 impl 反之亦然与现有 Map 类型变量一起使用。
- 不能使用Collections.class 中的算法。
You may want to write implementation such as to include above features.
您可能想要编写实现,例如包含上述功能。
// Hash map java library defined as follows.
public class HashMap<K,V>
extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable
// Hence, you may want to write this.
public class MyMap<K,V>
extends AbstractMap<K, V> // May skip this
implements Map<K,V>, Cloneable, Serializable // important
{
//Now it will ask to write all the methods which are defined in Map interface.
//These are minimum methods required for Map operations.
}