初始化Map <Foo,Vector <Bar >> - Java 7

Bog*_* M. 1 java

我需要初始化一个像Map x<Apartment,Vector<Expense>>它允许我,如果我@override的Map方法

Description Resource    Path    Location    Type
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.isEmpty()   Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.size()  Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.keySet()    Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.remove(Object)  Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.entrySet()  Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.putAll(Map<? extends Apartment,? extends Vector<Expense>>)  Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.values()    Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.clear() Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.get(Object) Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.containsKey(Object) Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.containsValue(Object)   Repository.java /proj_individual/src/repo   line 12 Java Problem
The type new Map<Apartment,Vector<Expense>>(){} must implement the inherited abstract method Map<Apartment,Vector<Expense>>.put(Apartment, Vector<Expense>) Repository.java /proj_individual/src/repo   line 12 Java Problem
Run Code Online (Sandbox Code Playgroud)

但我在互联网上看到Map不是一个界面,它是一个类,我做错了,还是这个必须要做的?

JB *_*zet 7

Map<Apartment, Vector<Expense>> map = new HashMap<Apartment, Vector<Expense>>();
Run Code Online (Sandbox Code Playgroud)

Map是一个接口(正如javadoc所说 - javadoc应该是你的参考,而不是互联网).您需要选择一个实现(HashMap是最常用的实现,但还有其他实现,具有其他特性).

你也应该忘记Vector.它不应该再使用了.使用List作为类型,使用ArrayList作为实现(还有其他List实现,但ArrayList几乎总是你想要的):

Map<Apartment, List<Expense>> map = new HashMap<Apartment, List<Expense>>();
Run Code Online (Sandbox Code Playgroud)

或者干脆

Map<Apartment, List<Expense>> map = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Java 7.

  • 所以,如果你正在学习这个链接应该帮助你:[收藏](http://docs.oracle.com/javase/tutorial/collections/index.html)您当前的问题或这[链接](http:// docs其他一切.oracle.com/javase/tutorial/index.html) (2认同)