The examples in this article share with you the specific code of Java using collections to implement address book for your reference. The specific content is as follows
The code is a bit cumbersome and is only suitable for beginners.
Item 1
Java Address Book
Method (class that declares method)
Code block
The code block syntax follows standard markdown code, for example:
package com.lanou.homwork;import java.util.Collection;import java.util.HashMap;import java.util.LinkedList;import java.util.Scanner;import java.util.Set;class ContactPerson { public HashMap map = null; public HashMap getMap() { return map; } public void setMap(HashMap map) { this.map = map; } public ContactPerson(HashMap map) { super(); this.map = map; } public ContactPerson() { } // Add contact public HashMap addContact(Person person) { // map collection is empty if (this.map.size() == 0) { LinkedList list = new LinkedList(); // Add to list.add(person); this.map.put(person.getFirstCard(), list);// Add map collection System.out.println(person.getFirstCard()); } else { System.out.println("111111111111111111"); // When it is not empty, first determine whether there is this grouping Set keySet = this.map.keySet();// Get all keys // traverse all keys for (Object object : keySet) { if (person.getFirstCard().equals(object)) { Object object2 = this.map.get(person.getFirstCard()); LinkedList linkedList = (LinkedList) object2; // There is this grouping linkedList.add(person); } else { // No this grouping LinkedList list3 = new LinkedList(); list3.add(person); this.map.put(person.getFirstCard(), list3); System.out.println(keySet); System.out.println(map.values()); return map; } } } return map; } // Delete the contact public void remove(Person person) { // First find the grouping// All values Set set = this.map.keySet(); for (Object object : set) { String nameString = person.getFirstCard(); if (nameString.equals(object)) { // Find grouping Collection = this.map.values(); for (Object object1 : collection) {// list array LinkedList list1 = (LinkedList) object1;// cast for (Object object2 : list1) {// traversal Person person2 = (Person) object2;// if (person.getNameString().equals( person2.getNameString())) { if (list1.size() == 1) { this.map.remove(person2.getFirstCard());// Delete the entire grouping show(); } else { list1.remove(person2); show(); } } } } } } } } } } } } } } // Show method public void show() { System.out.println("************************************************"); System.out.println("Please select function"); String[] newArrayStrings = { "1.Add contacts", "2.Delete contacts", "3.Modify contacts", "4.Show all contacts" }; for (int i = 0; i < newArrayStrings.length; i++) { System.out.println("*********" + newArrayStrings[i] + "*********"); } System.out.println("******************************************"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); switch (i) { case 1: { addPerson(); } break; case 2: { removePerson(); } break; case 3: { upPerson(); } break; case 4: { printResult(this.map); } break; default: break; } } // Modify contact information public void upPerson() { Person person = new Person(); System.out.println("Please enter the name of the person to be modified:"); Scanner scanner = new Scanner(System.in); String naString = scanner.next(); person.setNameString(naString); shanChu(person); } // Enter the new name of the person, age public void cc(Person person) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a new name:"); String naString2 = scanner.next(); System.out.println("Please enter a new age:"); int age = scanner.nextInt(); System.out.println("Please enter a new group"); String group = scanner.next(); upPersonMessage(person, naString2, group, age); } // Check if there is any such person public void shanChu(Person person) { if (this.map.isEmpty()) { System.out.println("The address book is empty, modification failed"); System.out.println("Do you add a contact? (Yes/No)"); Scanner scanner = new Scanner(System.in); String nameString = scanner.next(); if (nameString.equals("Yes")) { addPerosn(); } else { System.out.println("Silly, get out of your mom"); } } Collection collection = this.map.values(); for (Object object : collection) { LinkedList list = (LinkedList) object; for (Object object2 : list) { Person person2 = (Person) object2; if (person.getNameString().equals(person2.getNameString())) { System.out.println("Congratulations, this person is present in the address book"); cc(person); } else { System.out.println("Input is incorrect"); } } } } public Person addPerosn() { Person p = new Person(); System.out.println("Please enter the name of the adder:"); Scanner scanner = new Scanner(System.in); // Name String name = scanner.next(); // Age System.out.println("Please enter age:"); int age = scanner.nextInt(); // Group System.out.println("Please enter group name:"); String firstCardString = scanner.next(); p.setFirstCard(firstCardString); p.setAge(age); p.setNameString(name); System.out.println(p); addContact(p); System.out.println("Does it continue?y/n"); if (scanner.next().equals("y")) { show(); } else { System.out.println("****************** Welcome to come again next time**********************"); } return p; } public Person removePerson() { Person p = new Person(); System.out.println("Please enter the name of the deleter:"); Scanner scanner = new Scanner(System.in); // Name String name = scanner.next(); // Age System.out.println("Please enter the age of the deleter:"); int age = scanner.nextInt(); // Group System.out.println("Please enter the deleted group name:"); String firstCardString = scanner.next(); p.setFirstCard(firstCardString); p.setAge(age); p.setNameString(name); remove(p); return p; } // Modify the contact public HashMap upPersonMessage(Person person, String nameString, String firstCard, int age) { // Determine if (this.map.isEmpty()) { System.out.println("The address book is empty"); } else { String name = person.getFirstCard(); // Find the contact Collection collection = this.map.values();// Get all values for (Object object : collection) { LinkedList list = (LinkedList) object; for (Object object2 : list) { Person p = (Person) object2; if (person.getNameString().equals(p.getNameString())) { // Equal words, it means that there is this person System.out.println("Contact once"); if (list.size() == 1) { this.map.remove(p.getFirstCard()); Person person2 = new Person(); person2.setAge(age); person2.setFirstCard(firstCard); person2.setNameString(nameString); LinkedList list2 = new LinkedList(); list2.add(person2); this.map.put(person2.getFirstCard(), list2); System.out.println(this.map); } else { Person person2 = new Person(); person2.setAge(age); person2.setFirstCard(firstCard); person2.setNameString(nameString); list.add(person2); System.out.println(this.map); } } } } } return map; } // Show all contacts public void printResult(HashMap map1) { if (map1.isEmpty()) { System.out.println("The address book is empty"); } else { System.out.println(map1); Scanner scanner = new Scanner(System.in); System.out.println("Does it continue y/n"); if (scanner.next().equals("y")) { show(); } else { System.out.println("*************** Welcome to come again next time*************************"); } } } } } } class Person { private String nameString; private String firstCard; private int age; public String getNameString() { return nameString; } public void setNameString(String nameString) { this.nameString = nameString; } public String getFirstCard() { return firstCard; } public void setFirstCard(String firstCard) { this.firstCard = firstCard; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } public Person(String nameString, String firstCard) { this.nameString = nameString; this.firstCard = firstCard; } public Person(String nameString, String firstCard, int age) { this(nameString, firstCard); this.age = age; } @Override public String toString() { return "Name:" + this.getNameString() + "Age:" + this.age + "Group:" + this.firstCard; }}footnote
Project 2
Test class
Code block
The code block syntax follows standard markdown code, for example:
package com.lanou.homwork;import java.util.HashMap;public class TestPerson { public static void main(String[] args) { HashMap map = new HashMap(); ContactPerson contactperson = new ContactPerson(map); contactperson.show(); }}Thanks for browsing.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.