How to create a database Last name First name Patronymic through HashMap? HashMap map = new HashMap <> (); How to add middle name and passport series? Create a new hashmap?
Closed due to the fact that the essence of the question is not clear to the participants by default locale , Sergey Gornostaev , 0xdb , LFC , Kromster 21 Mar at 4:22 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Concentrate on one question and try to paint it in as much detail as possible. Give your solution attempts - default locale
- Map This is a key value. Determine what should be the key, and what should be the value - Oleksiy Mororets
|
1 answer
Yes, it is easy (I really do not know why this trash). For example:
import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; public class People { private static final Map <Long,People> PEOPLES = new LinkedHashMap<>(); private static Long ID = 0L; private final Long id; private final String surname; private final String name; private final String patronomic; private final String passport; private People(Long id, String surname, String name, String patronomic, String passport) { this.id = id; this.surname = surname; this.name = name; this.patronomic = patronomic; this.passport = passport; } public static People create(String surname, String name, String patronomic, String passport) { final People people = new People(++ID, surname, name, patronomic, passport); PEOPLES.put(ID, people); return people; } public static People getById(Long id) { return PEOPLES.get(id); } public static People deleteById(Long id) { return PEOPLES.remove(id); } public static void deleteAll() { PEOPLES.clear(); ID = 0L; } public static Collection<People> getAll() { return PEOPLES.values(); } public static Long getID() { return ID; } public Long getId() { return id; } public String getSurname() { return surname; } public String getName() { return name; } public String getPatronomic() { return patronomic; } public String getPassport() { return passport; } @Override public int hashCode() { int hash = 7; return hash; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final People other = (People) obj; if (!Objects.equals(this.surname, other.surname)) { return false; } if (!Objects.equals(this.name, other.name)) { return false; } if (!Objects.equals(this.patronomic, other.patronomic)) { return false; } if (!Objects.equals(this.passport, other.passport)) { return false; } if (!Objects.equals(this.id, other.id)) { return false; } return true; } @Override public String toString() { return "People{" + "id=" + id + ", surname=" + surname + ", name=" + name + ", patronomic=" + patronomic + ", passport=" + passport + '}'; } } And then we call static methods from another class for adding, searching by ID, getting a list, deleting, clearing:
public class Main{ public static void main(String[] args) { People.create("Pupkin", "Egor", "Ivanovich", "KK852741"); People.create("Zalupkin", "Igor", "Aleksandrovich", "KK147896"); People.create("Zalupupkin", "Ivan", "Ivanovich", "KK632147"); System.out.println(People.getAll()); System.out.println(People.getById(1L)); } } - We did this thing on the courses, too, although it is not usable from the word at all. Well, nothing, the guy needs, and you beautifully wrote everything, so to speak, optimally) - Mykola Murza
- Thank you, but the most optimal in this code is the names in the database)) - Dmitry
|