在 Java 中传递一个 HashMap 作为参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42709454/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:19:13  来源:igfitidea点击:

Pass a HashMap as parameter in Java

javahashmap

提问by Fosh

I have the main class Librarywhere I create:

我有Library我创建的主类:

HashMap<String, HashSet<String>> students_books = new HashMap<String, HashSet<String>>(); 

Then I'll have class Studentwhere I'll make a constructor who will take the HashMap as a parameter like this:

然后我将有一个类Student,我将在其中创建一个将 HashMap 作为参数的构造函数,如下所示:

public class Student {

    private Student(HashMap students_books){

then, back in my main class (Library) I create a studentobject where I want to put the HashMap as parameter:

然后,回到我的主类(库)中,我创建了一个student对象,我想在其中将 HashMap 作为参数:

Student student = new Student(*HashMap as parameter*);

What I'm not finding is how I can do this and how the Studentclass knows what type of HashMap I'm passing, for example, <String, HashSet<String>>

我没有找到的是我如何做到这一点以及Student班级如何知道我正在传递的 HashMap 类型,例如,<String, HashSet<String>>

采纳答案by Phi Luu

knows what type of HashMap I'm passing

知道我传递的是什么类型的 HashMap

Firstly, your method is not a constructor since it has return type, remove its return type and public your constructor. Then just force them to pass what type of HashMap you want by doing this

首先,您的方法不是构造函数,因为它具有返回类型,删除其返回类型并公开您的构造函数。然后只需通过执行此操作强制他们传递您想要的 HashMap 类型

public class Student {

    public Student(HashMap<String, HashSet<String>> students_books){

and then passing them like this

然后像这样传递它们

HashMap<String, HashSet<String>> students_books = new HashMap<String, HashSet<String>>(); 
Student student = new Student(students_books);

回答by rpfun12

In your Student class constructor(which currently a method, since it has a return type), the argument is not utilizing generic type.

在您的 Student 类构造函数(当前是一个方法,因为它具有返回类型)中,该参数未使用泛型类型。

Please change it to the following.

请将其更改为以下内容。

public StudentHashMap(HashMap<String, HashSet<String>> students_books){

}

This will ensure type safetey during compile time

这将确保编译时的类型安全

回答by Vinod Krishnan

To answer your question - "How to pass HashMap as a parameter" and how Student class know the type I am providing a more generic and standard way of doing this

回答你的问题——“如何将 HashMap 作为参数传递”以及 Student 类如何知道类型我提供了一种更通用和标准的方法来做到这一点

Map<K,V> books = new HashMap<K,V>(); // K and V are Key and Value types
Student student = new Student(books); // pass the map to the constructor

..
//Student Constructor
public Student(Map<K,V> books){
..
}

回答by Dilshod Shaymanov

What you did is a private method, not a constructor. change your method to this:

你所做的是一个私有方法,而不是一个构造函数。将您的方法更改为:

public Student(HashMap<String, HashSet<String>> student_books) {
    //your code here
} 

I this way it will a true constructor you wanted, hope this will help

我这样它将成为您想要的真正构造函数,希望这会有所帮助