Results 1 to 4 of 4
  1. #1
    michmauzer is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Question What are the other ways to run this code? Used class instance and Singleton+method

    Hi All,

    I was checking some HashMap article https://dzone.com/articles/java-8-hashmaps-keys-and-the-comparable-interface and got confused with ways to run some example.

    Basically I appreciate if you provide some other implementation techniques for the same article to make it running

    1)Example #1

    XML Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    public class Person implements Comparable<Person>
    
    {
    
      private String firsName;
     private String lastName;
     private UUID id;
    
    
      public Person(String firsName, String lastName, UUID id) {
      this.firsName = firsName;
      this.lastName = lastName;
      this.id = id;
     }
    
      @Override
     public int hashCode() {
      return 5;
     }
    
      @Override
     public int compareTo(Person person) {
      return this.id.compareTo(person.id);
     }
    
    
      private static final int LIMIT = 30000;
    
    
      public static void main(String[] args) {
      Person person = null;
      long start = System.currentTimeMillis();
      Map<Person, String> mymap = new HashMap<>();
      for (int i = 0; i < LIMIT; i++) {
       UUID randUUID = UUID.randomUUID();
       person = new Person("fn", "ln", randUUID);
       mymap.put(person, "comment" + i);
       System.out.println(mymap.get(person));
      }
    
       mymap.get(person);
      long stop = System.currentTimeMillis();
    
       System.out.println(stop - start + "msec");
     }
    }
    2) Example #2
    ---------------
    XML Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
    
    public class Person implements Comparable<Person>
    
    {
    
       private String firsName;
       private String lastName;
       private UUID id;
    
       //create an object of Person   private static Person personInstance = new Person();
    
       //make the constructor private so that this class cannot be   //instantiated   private Person() {
       }
    
       //Get the only object available   public static Person getPersonInstance() {
          return personInstance;
       }
    
       private void fillAndSearch() {
          Person person = null;
          Map<Person, String> mymap = new HashMap<>();
          for (int i = 0; i < LIMIT; i++) {
             UUID randomUUID = UUID.randomUUID();
             person = new Person("fn", "ln", randomUUID);
             mymap.put(person, "comment" + i);
             System.out.println(mymap.get(person));
          }
          long start = System.currentTimeMillis();
          mymap.get(person);
          long stop = System.currentTimeMillis();
    
          System.out.println(stop - start + " millis #1");
       }
    
       //end new <code>
       public Person(String firsName, String lastName, UUID id) {
          this.firsName = firsName;
          this.lastName = lastName;
          this.id = id;
       }
    
       @Override   public int hashCode() {
          return 5;
       }
    
       @Override   public int compareTo(Person person) {
          return this.id.compareTo(person.id);
       }
    
    
       private static final int LIMIT = 30000;
    
    
       public static void main(String[] args) {
          long start = System.currentTimeMillis();
          Person persobject = Person.getPersonInstance();
          persobject.fillAndSearch();
          long stop = System.currentTimeMillis();
          System.out.println(stop - start + " millis #2");
       }
    }
    >>
    comment29998
    comment29999
    0 millis #1
    515 millis #2

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What are the other ways to run this code? Used class instance and Singleton+meth

    Can you write a description of what you want the program to do that could be used to design it?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    michmauzer is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Default Re: What are the other ways to run this code? Used class instance and Singleton+meth

    I'm just wondering what are the ways of running this code. AFAIK, we can create mutable/immutable classes, what are the other major topics which could be considered?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: What are the other ways to run this code? Used class instance and Singleton+meth

    what are the ways of running this code
    I'm not sure what you are asking. I use the java command to run my java programs.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 14
    Last Post: 03-12-2015, 05:15 PM
  2. Replies: 10
    Last Post: 01-09-2012, 08:57 PM
  3. accessing a singleton class inside another class
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-08-2010, 06:23 PM
  4. Replies: 20
    Last Post: 06-03-2009, 05:33 PM
  5. Replies: 2
    Last Post: 10-06-2008, 09:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •