Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:50:58+00:00 2026-06-17T13:50:58+00:00

I have a nice problem – create a phone book – containing list of

  • 0

I have a nice problem – create a phone book – containing list of contacts.
As a phonebook goes,

  1. Contacts are to be always sorted.(by name)
  2. Can star mark certain
    contacts, so they have to be above all the rest.(the * contacts
    are ordered by the time of contact creation)

    class PhoneBook{
    //require an always sorted d.s
    TreeSet<Contact> contacts = new TreeSet<Contact>();
    
    @Override
    public String toString() {
        return "PhoneBook [contacts=" + contacts + "]";
    }
    
    public boolean addContact(Contact contact){
        //validate before adding the contact.
        return contacts.add(contact);
    }
    

    }

    class Contact implements Comparable<Contact>{
    String name;
    int phoneNo;
    Date timeAdded;
    boolean starContact;
    
    
    
    public Contact(String name, int phoneNo, Date timeAdded, boolean starContact) {
        super();
        this.name = name;
        this.phoneNo = phoneNo;
        this.timeAdded = timeAdded;
        this.starContact = starContact;
    }
    
    
    
    @Override
    public int compareTo(Contact otherContact) {
        if(this.starContact && otherContact.starContact){
            return this.timeAdded.before(otherContact.timeAdded)?-1:1; //impossible to add 2 contacts at the same time
        }else if(this.starContact){
            return -1;
        }else if(otherContact.starContact){
            return 1;
        }else{
            //simple Contacts
            return this.name.compareTo(otherContact.name);
        }
    }
    
    
    
    
    
    
    @Override
    public String toString() {
        return "\nContact [name=" + name + ", timeAdded=" + timeAdded
                + ", starContact=" + starContact + "]";
    }
    
    
    }
    

Test Code

    public class MobilePhoneBookDemo {

/**
 * @param args
 */
public static void main(String[] args) {

    PhoneBook phoneBook = new PhoneBook();

    Contact frnd1 = new Contact("Z",56,new Date(),false);
    phoneBook.addContact(frnd1);
    Contact frnd2 = new Contact("A",3,new Date(),false);
    phoneBook.addContact(frnd2);
    Contact frnd3 = new Contact("C",30,new Date(),false);
    phoneBook.addContact(frnd3);
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Contact ta = new Contact("Ta", 5, new Date(), true);
    phoneBook.addContact(ta);
    Contact ma = new Contact("Ma", 31, new Date(), true);
    phoneBook.addContact(ma);
    Contact baba = new Contact("Baba", 300, new Date(), true);
    phoneBook.addContact(baba);

    //change the priority later for one of my friends.
    System.out.println(phoneBook);
    frnd1.starContact = true;
    System.out.println(phoneBook.contacts.contains(frnd1));

    if(phoneBook.contacts.remove(frnd1)){
        System.out.println("removed");
        phoneBook.contacts.add(frnd1);
    }

    System.out.println(phoneBook);
}

}

Problems faced:

  1. The contains doesn’t find the entry anymore, what’s amiss?
    I did try and put an equals and a hashcode on Contact, apparently, if there is a Comparator/Comparable present, the compare* is only invoked.
  2. Is it fair to use a TreeSet here, or should any other datastructure be used?
    For eg. HashSet and then convert to a TreeSet?
  3. The contains() doesn’t even compare for all entries in the map, it just compared against C,Ma and Ta entries. Why was that?

Questions priority according to order.
I appreciate all the answers, but this is indeed a complete test case, so please try and run PhoneBook just once before providing an answer. Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:50:59+00:00Added an answer on June 17, 2026 at 1:50 pm

    The solution is simple, first remove, then change the value and add it back again.
    I comment the modification first,I have also put in the modified compareTo() since there were lots of confusion around it.

    //frnd1.starContact = true;
        System.out.println("Entry present>"+phoneBook.contacts.contains(frnd1));
    
        if(phoneBook.contacts.remove(frnd1)){
            System.out.println("removed");
            frnd1.starContact = true;
            phoneBook.contacts.add(frnd1);
        }
    
    
    @Override
    public int compareTo(Contact otherContact) {
        if(otherContact.phoneNo == this.phoneNo){
            return 0;
        }
        if(this.starContact && otherContact.starContact){
            return this.timeAdded.before(otherContact.timeAdded)?-1:1; //impossible to add 2 contacts at the same time
        }else if(this.starContact){
            return -1;
        }else if(otherContact.starContact){
            return 1;
        }else{
            //simple Contacts
            return this.name.compareTo(otherContact.name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a nice day. I got problem when trying to create an image from
Here's a strange question for you guys, I have a nice sorted list that
Ive come across a nice problem recently, I have a Map LinkedHashMap<String, List<MyCustomObject>> I
I have a nice graph (a list) containing 81 vertices (each vertex is an
I have a very simple problem but cannot find a nice solution. I have
I have a reacurring problem. I code nice standards compliant code only to have
I am looking for a nice code solution for the following problem. I have
I have a nice problem. I have an application that suspend the request and
I have a nice little threading problem with a library I use to generate
first post so be nice :) My problem is that I have a class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.