简体   繁体   English

如何使我的HashTable适用于特定类型

[英]How to make my HashTable work on for a specific type

I need to create a HashTable for a project. 我需要为项目创建一个HashTable。

Here is how I define what my HashTable is: 这是我定义HashTable的方式:

public class StudentHashTable<Name, DateOfBirth> {

}

The way you would create my HashTable is the following: 创建HashTable的方式如下:

StudentHashTable<String,Integer> hashMap = new StudentHashTable<String,Integer>();

As it stands now, it would actually work for any object that has a key of String type and a value of Integer type. 从目前的情况来看,它实际上适用于具有String类型的键和Integer类型的值的任何对象。 What do I need to think about so I can force the HashTable to only accept objects of type Student that has key of String type and value of Integer type? 我需要考虑什么,以便可以强制HashTable只接受具有String类型的键和Integer类型的值的Student类型的对象?

If you want your customized HashMap to only accept class Student, which is a very odd request as collections should be generic usually, then just do the following. 如果您希望自定义的HashMap 接受类Student,这是一个非常奇怪的请求,因为集合通常应该是通用的,则只需执行以下操作即可。 make the setters only work with class Student. 使二传手只能与班级学生一起工作。 This way you are guaranteed to not be able to accept anything except what you want. 这样,可以保证除了您想要的东西之外,不能接受其他任何东西。

Example: 例:

public void put(Student student){...}

public void putAll(Student... students){...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM