package com.itranswarp.learnjava;
import java.util.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
Person a = new Person("Xiao", "Ming", 18);
Person b = new Person("Xiao", "Hong", 25);
Person c = new Person("Bob", "Smith", 20);
List<Person> list = List.of(a, b, c);
boolean exist = list.contains(new Person("Bob", "Smith", 20));
System.out.println(exist ? "测试成功!" : "测试失败!");
}
class Person {
String firstName;
String lastName;
int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
public boolean equals(Object o) {
if (o instanceof Person) {
Person p = (Person) o;
return Objects.equals(this.firstName, p.firstName) && Objects.equals(this.lastName, p.lastName) && this.age == p.age;
return false;
上面是代码下面是错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method of(Person, Person, Person) is undefined for the type List
at com.itranswarp.learnjava.Main.main(Main.java:11)
同样代码我测试过了没出错,大概是环境的问题
我用的jdk1.8一直不好使,但是重新装了一遍IDE还是同样的配置就好使了,不知道为啥
Prior to Java 9, you need to go for Arrays.asList() instead of List.of()
Arrays.asList()
List.of()
Sign in to make a reply
芒果味儿momo
package com.itranswarp.learnjava;
import java.util.*;
import java.util.List;
public class Main {
public static void main(String[] args) {
Person a = new Person("Xiao", "Ming", 18);
Person b = new Person("Xiao", "Hong", 25);
Person c = new Person("Bob", "Smith", 20);
List<Person> list = List.of(a, b, c);
boolean exist = list.contains(new Person("Bob", "Smith", 20));
System.out.println(exist ? "测试成功!" : "测试失败!");
}
}
class Person {
String firstName;
String lastName;
int age;
public Person(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public boolean equals(Object o) {
if (o instanceof Person) {
Person p = (Person) o;
return Objects.equals(this.firstName, p.firstName) && Objects.equals(this.lastName, p.lastName) && this.age == p.age;
}
return false;
}
}
上面是代码下面是错误
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method of(Person, Person, Person) is undefined for the type List
at com.itranswarp.learnjava.Main.main(Main.java:11)