LinkedHashMap程序给出的不是通用的;它不能参数化错误

时间:2017-09-09 13:58:01

标签: java collections linkedhashmap

我创建了一个Student类,并且没有使用LinkedHashMap进行初始化。给编译时错误:

The type LinkedHashMap is not generic; 
it cannot be parameterized with arguments <Student, Integer>.

代码:

StudentLinkedMap:

 public class LinkedHashMap {

public static void main(String[] args) {

Map <Student,Integer> students = new  LinkedHashMap<Student,Integer>();

Student s1 = new Student(1,2,"Dhwani","Pandya");
Student s2 = new Student(2,2,"Priyam ","Parekh");
Student s3 = new Student (3,2,"Sucheta","Shrivastava");
Student s4 = new Student(3,6,"Nirali","Rokadia");
Student s5 = new Student(2,6,"Kajari","Agrawal");

students.put(s1,2);
students.put(s2,4);
students.put(s3,6);
students.put(s4,5);
students.put(s5,7);

for(Map.Entry lhs:students.entrySet()){
    System.out.println(lhs.getKey()+ "  "+ lhs.getValue());
}

}

}

学生班:

  public class Student {

private int rollno;
private  String firstname;
private  String lastname;
private  int std;

 public Student(int rollno, int std,String firstname, String lastname){

    this.firstname=firstname;
    this.lastname=lastname;
    this.rollno=rollno;
    this.std=std;

    }

@Override
public String toString(){
    return ("\troll no="+ this.rollno + "\tstandard="+ 
 this.std+"\tfirstname="+this.firstname+"\tlastname="+this.lastname);
 }


 }

1 个答案:

答案 0 :(得分:2)

您已命名您的LinkedHashMap,因此名称为java.util.LinkedHashMap。您可以更改班级名称,使用完全限定的班级名称。

Map<Student,Integer> students = new java.util.LinkedHashMap<>();