Java 通用地图与地图<;键类型、值类型>;在爪哇
我有一个类,Java 通用地图与地图<;键类型、值类型>;在爪哇,java,generics,map,Java,Generics,Map,我有一个类,TestMap,只有一个静态方法(包括main),用于测试map。例如,类中有一个方法,它接受映射,键和值的类型分别表示为KeyType和ValueType,如下所示 public static <KeyType,ValueType> void printMap( String msg, Map<KeyType,ValueType> m ) { System.out.println( msg + ":" ); Set<Map.Entry&
TestMap
,只有一个静态方法(包括main
),用于测试map
。例如,类中有一个方法,它接受映射,键和值的类型分别表示为KeyType
和ValueType
,如下所示
public static <KeyType,ValueType> void printMap( String msg, Map<KeyType,ValueType> m )
{
System.out.println( msg + ":" );
Set<Map.Entry<KeyType,ValueType>> entries = m.entrySet( );
for( Map.Entry<KeyType,ValueType> thisPair : entries )
{
System.out.print( thisPair.getKey( ) + ": " );
System.out.println( thisPair.getValue( ) );
}
}
publicstaticvoidprintmap(字符串msg,Map m)
{
System.out.println(msg+“:”);
Set entries=m.entrySet();
用于(映射条目此对:条目)
{
System.out.print(thisPair.getKey()+“:”);
System.out.println(thisPair.getValue());
}
}
我的问题是,如果我想重新编写这个类,以便它可以被实例化,而不仅仅由静态方法组成,那么如何在类中定义一个可以使用映射的映射
我试图定义一个地图如下,但它似乎不工作
private Map<KeyType, ValueType> internalMap;
私有地图;
有什么想法吗
根据第一条注释,我试图添加到类定义中,然后我设置了一个构造函数,如下所示:
public class TestMap<KeyType, ValueType>
{
private Map<KeyType, ValueType> internalMap;
/*
* Constructor which accepts a generic Map for testing
*/
public <KeyType,ValueType> TestMap(Map<KeyType, ValueType> m)
{
this.internalMap = m;
}
}
公共类TestMap
{
私有地图;
/*
*接受通用映射进行测试的构造函数
*/
公共测试地图(地图m)
{
这个.internalMap=m;
}
}
但是,构造函数中的赋值抛出了一个错误,表示它是类型不匹配,并且无法从java.util.Map转换为java.util.Map。您的意思是:
class MyMap<KeyType, ValueType> {
private Map<KeyType, ValueType> internalMap;
}
classmymap{
私有地图;
}
编辑:构造函数上不需要类型参数:
class TestMap<KeyType, ValueType>
{
private Map<KeyType, ValueType> internalMap;
/*
* Constructor which accepts a generic Map for testing
*/
public TestMap(Map<KeyType, ValueType> m)
{
this.internalMap = m;
}
}
类测试映射
{
私有地图;
/*
*接受通用映射进行测试的构造函数
*/
公共测试地图(地图m)
{
这个.internalMap=m;
}
}
您可以尝试声明internalMap
,但由于Map
是一个接口,因此需要使用具体的类类型(例如,HashMap
,TreeMap
等)对其进行实例化
公共类TestMap{
私有地图;
公共测试图(){
internalMap=新的HashMap();
}
公共测试地图(地图m){
内部映射=m;
}
公共void打印映射(字符串msg)
{
System.out.println(msg+“:”);
Set entries=internalMap.entrySet();
用于(映射条目此对:条目)
{
System.out.print(thisPair.getKey()+“:”);
System.out.println(thisPair.getValue());
}
}
…//要添加到内部映射的方法,等等。
}
@Ted Hopp:他只是说他尝试了内部地图定义,但“没有用”。我猜他错过了类本身的类型参数。好的,很好的推断。我假设问题是实例化一个泛型映射,但您可能是对的(特别是在OP编辑的情况下)。不要在构造函数本身上重复类型参数。请看我的答案。构造函数上不需要类型参数。@克里斯·科尔宾:在问题已标记为已回答后,请不要将问题编辑为新问题。你不会吸引新的答案(因为问题看起来已经回答了),而且对已经回答的人也不公平(因为他们的答案不再适用于你的新问题)。相反,只需通过单击页面右上角的“提问”按钮来提出新问题。如果我希望构造函数接受映射,然后将其分配给internalMap,该怎么办?@ChrisCorbin-编辑以显示它是如何完成的。只需删除构造函数声明中的类型参数。谢谢!我的教科书为建造师提供了以下内容:;公共测试地图(MapYeesh)。这是什么教科书?上面应该有危险标签。
public class TestMap<KeyType, ValueType> {
private Map<KeyType, ValueType> internalMap;
public TestMap() {
internalMap = new HashMap<KeyType, ValueType>();
}
public TestMap(Map<KeyType, ValueType> m) {
internalMap = m;
}
public void printMap( String msg )
{
System.out.println( msg + ":" );
Set<Map.Entry<KeyType,ValueType>> entries = internalMap.entrySet( );
for( Map.Entry<KeyType,ValueType> thisPair : entries )
{
System.out.print( thisPair.getKey( ) + ": " );
System.out.println( thisPair.getValue( ) );
}
}
. . . // methods to add to internal map, etc.
}