How to do it...

Follow these steps to implement the example:

  1. Create a class named Contact:
        public class Contact {
  1. Declare two private String attributes named name and phone:
        private final String name; 
private final String phone;
  1. Implement the constructor of the class to initialize its attributes:
        public Contact(String name, String phone) { 
this.name=name;
this.phone=phone;
}
  1. Implement the methods to return the values of the name and phone attributes:
        public String getName() { 
return name;
}

public String getPhone() {
return phone;
}
  1. Create a class named Task and specify that it implements the Runnable interface:
        public class Task implements Runnable {
  1. Declare a private ConcurrentSkipListMap attribute, parameterized by the String and Contact classes, named map:
        private final ConcurrentSkipListMap<String, Contact> map;
  1. Declare a private String attribute named id to store the ID of the current task:
        private final String id;
  1. Implement the constructor of the class to store its attributes:
        public Task (ConcurrentSkipListMap<String, Contact> map,String id){ 
this.id=id;
this.map=map;
}
  1. Implement the run() method. It stores 1,000 different contacts in the map using the ID of the task and an incremental number to create Contact objects. Use the put() method to store the contacts in the map:
        @Override 
public void run() {
for (int i=0; i<1000; i++) {
Contact contact=new Contact(id, String.valueOf(i+1000));
map.put(id+contact.getPhone(), contact);
}
}
  1. Implement the main class of the example by creating a class named Main and adding the main() method to it:
        public class Main { 
public static void main(String[] args) {
  1. Create a ConcurrentSkipListMap object, parameterized by the String and Conctact classes, named map:
        ConcurrentSkipListMap<String, Contact> map = new
ConcurrentSkipListMap<>();
  1. Create an array for 26 Thread objects to store all the Task objects that you're going to execute:
        Thread threads[]=new Thread[26]; 
int counter=0;
  1. Create and launch 26 task objects and assign a capital letter to the ID of each task:
        for (char i='A'; i<='Z'; i++) { 
Task task=new Task(map, String.valueOf(i));
threads[counter]=new Thread(task);
threads[counter].start();
counter++;
}
  1. Wait for the finalization of the threads using the join() method:
        for (Thread thread : threads){ 
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
  1. Get the first entry of the map using the firstEntry() method. Write its data to the console:
        System.out.printf("Main: Size of the map: %d
",map.size()); 

Map.Entry<String, Contact> element;
Contact contact;

element=map.firstEntry();
contact=element.getValue();
System.out.printf("Main: First Entry: %s: %s ", contact.getName(),
contact.getPhone());
  1. Get the last entry of the map using the lastEntry() method. Write its data to the console:
        element=map.lastEntry(); 
contact=element.getValue();
System.out.printf("Main: Last Entry: %s: %s ", contact.getName(),
contact.getPhone());
  1. Obtain a submap of the map using the subMap() method. Write its data to the console:
          System.out.printf("Main: Submap from A1996 to B1002: 
"); 
ConcurrentNavigableMap<String, Contact> submap=map
.subMap("A1996","B1002");
do {
element=submap.pollFirstEntry();
if (element!=null) {
contact=element.getValue();
System.out.printf("%s: %s ", contact.getName(),
contact.getPhone());
}
} while (element!=null);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.183.63