Java uses a set to implement the address book function, and java sets to implement the address book function.
The examples in this article share with you the specific code for implementing address book using collections in java for your reference. The specific content is as follows:
The code is tedious and only suitable for beginners.
Project 1
Java address book
Method (declare the class of the method)
Code block
The code block syntax follows the standard markdown code, for example:
Package com. lanou. homwork; import java. util. collection; import java. util. hashMap; import java. util. using list; import java. util. imports; 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 the contact public H AshMap addContact (Person person) {// The map set is empty if (this. map. size () = 0) {partition list = new partition list (); // Add it to the list set list. add (person); this. map. put (person. getFirstCard (), list); // Add the map set System. out. println (person. getFirstCard ();} else {System. out. println ("1111111111111111111"); // when it is not null. first, check whether this group is Set keySet = this. map. keySet (); // obtain all key keys // traverse all keys for (Object object: keySet) {if (Person. getFirstCard (). equals (object) {Object object2 = this. map. get (person. getFirstCard (); Listing list listing = (listing list) object2; // This group contains listing list. add (person);} else {// this group does not exist; list3 = new vertex list (); 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 publi C void remove (Person person) {// first find the group // All values Set set = this. map. keySet (); for (Object object: set) {String nameString = person. getFirstCard (); if (nameString. equals (object) {// you can find the Collection group collection = this. map. values (); for (Object object1: collection) {// list array listing list list1 = (listing list) object1; // force conversion for (Object object2: list1) {// traverse Person person2 = (Person) object2 ;// If (person. getNameString (). equals (person2.getNameString () {if (list1.size () = 1) {this. map. remove (person2.getFirstCard (); // Delete the entire group show () ;}else {list1.remove (person2); show ();}}}}}}} // display method public void show () {System. out. println ("************************************* "); system. out. println ("select feature"); String [] newArrayStrings = {"1. add contact "," 2. delete contact "," 3. modify contact "," 4. show all contacts "}; (Int I = 0; I <newArrayStrings. length; I ++) {System. out. println ("*******" + newArrayStrings [I] + "*******");} System. out. println ("************************************* "); required bytes = new bytes (System. in); int I = random. nextInt (); switch (I) {case 1: {addPerosn ();} break; case 2: {removePerson ();} break; case 3: {upPerson ();} break; case 4: {printResult (this. map);} break; defau Lt: break; }}// modify the contact information public void upPerson () {Person person = new Person (); System. out. println ("enter the name of the person you want to modify:"); registrant domains = new users (System. in); String naString = signature. next (); person. setNameString (naString); shanChu (person);} // enter a new Person's name. Age: public void cc (person) {registrant domains = new domains (System. in); System. out. println ("enter a new name:"); String naString2 = signature. next (); Sys Tem. out. println ("Enter the New age:"); int age = middle. nextInt (); System. out. println ("enter a new group"); String group = bytes. next (); upPersonMessage (person, naString2, group, age);} // determines whether this Person is public void shanChu (person Person) {if (this. map. isEmpty () {System. out. println ("the address book is empty, modification failed"); System. out. println ("add a contact? (Yes/No) "); required bytes = new bytes (System. in); String nameString = bytes. next (); if (nameString. equals ("Yes") {addPerosn ();} else {System. out. println ("silly, roll your mom") ;}} Collection collection = this. map. values (); for (Object object: collection) {Entity list = (Entity list) object; for (Object object2: list) {Person person2 = (Person) object2; if (person. getNameString (). equals (person2. GetNameString () {System. out. println ("congratulations, this person exists in the address book"); cc (person);} else {System. out. println ("incorrect input") ;}}} public Person addPerosn () {Person p = new Person (); System. out. println ("enter the name of the person you want to add:"); required bytes = new bytes (System. in); // name String name = Beijing. next (); // age System. out. println ("Enter age:"); int age = middle. nextInt (); // group System. out. println ("Enter the group name:"); String firstCardStrin G = large. next (); p. setFirstCard (firstCardString); p. setAge (age); p. setNameString (name); System. out. println (p); addContact (p); System. out. println ("continue? Y/n "); if (then. next (). equals ("y") {show ();} else {System. out. println ("**************** welcome to come back next time ****************") ;} return p;} public Person removePerson () {Person p = new Person (); System. out. println ("enter the name of the person to delete:"); deleted bytes = new bytes (System. in); // name String name = Beijing. next (); // age System. out. println ("Enter the age of the deleted person:"); int age = deleted. nextInt (); // group System. out. println ("enter the name of the deleted group:"); String firstCardString = deleted. 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 whether this contact exists if (this. map. isEmpty () {System. out. println ("Address Book is blank");} else {String name = person. getFirstCard (); // contact Collection = this. map. values (); // obtain all values for (Object object: collection) {shortlist list = (shortlist) object; for (Object object2: list) {Person p = (Person) object2; if (person. getNameString (). equals (p. getNameString () {// equals, indicating that this person is in the System. out. println ("Once contact"); if (list. size () = 1) {this. map. remove (p. getFirstCard (); Person person2 = new Person (); person2.setAge (age); person2.setFirstCard (firstCard); person2.setNameString (nameString); shortlist list2 = new shortlist (); 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;} // display all contacts public void printResult (HashMap map1) {if (map1.isEmpty () {System. out. println ("Address Book is blank");} else {System. out. println (map1); reserved bytes = new bytes (System. in); System. out. println ("Continue y/n"); if (continue. next (). equals ("y") {show ();} else {System. out. println ("**************** welcome to come back 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 ;}}
Footer
Project 2
Test class
Code block
The code block syntax follows the 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 for your learning and support for helping customers.