Advertisement
apl-mhd

HasMap

Dec 27th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.HashMap;
  10. import java.util.Scanner;
  11.  
  12. public class Main {
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.  
  16.  
  17.         FileReader fileReader = new FileReader("PersonalInformation.txt");
  18.         BufferedReader bufferedReader = new BufferedReader(fileReader);
  19.  
  20.  
  21.         String s = bufferedReader.readLine();
  22.  
  23.  
  24.         String arr[] = s.split(" ");
  25.         ArrayList<Person> ob = new ArrayList<>();
  26.         ArrayList<Person> ob2 = new ArrayList<>();
  27.  
  28.  
  29.         HashMap<Person,Boolean> hashMap = new HashMap<>();
  30.  
  31.  
  32.         Person a = new Person("Apel","Mahmud","123");
  33.         Person b = new Person("Apel","Mahmud","123");
  34.  
  35.         ob2.add(a);
  36.         ob2.add(b);
  37.  
  38.  
  39.         while (true){
  40.  
  41.  
  42.             ob.add(new Person(arr[0],arr[1],arr[2]));
  43.             System.out.println(s);
  44.             s= bufferedReader.readLine();
  45.             if(s==null) break;
  46.             arr = s.split(" ");
  47.  
  48.         }
  49.  
  50.         hashMap.put(ob.get(0),true);
  51.  
  52.  
  53.         fileReader.close();
  54.         bufferedReader.close();
  55.  
  56.         Collections.sort(ob);
  57.  
  58.         System.out.println("after sort");
  59.  
  60.         for (Person i:ob){
  61.  
  62.             String x = i.firstName+" "+i.lastName+" "+i.pid;
  63.             System.out.println(x);
  64.             //System.out.println(i);
  65.         }
  66.  
  67.         System.out.println("\nnew person information\n");
  68.  
  69.         String info;
  70.         String infoArr[] = new String[3];
  71.  
  72.         Scanner in = new  Scanner(System.in);
  73.  
  74.         info = in.nextLine();
  75.         infoArr = info.split(" ");
  76.         System.out.println(infoArr[0]+infoArr[1]+infoArr[2]);
  77.  
  78.  
  79.         Person infoOb =new Person(infoArr[0],infoArr[1],infoArr[2]);
  80.         //ob.add(infoOb);
  81.  
  82.  
  83.  
  84.  
  85.  
  86. //        FileWriter fileWriter = new FileWriter("PersonalInformation.txt");
  87.  
  88.         if (null == hashMap.get(infoOb)){
  89.  
  90.             FileWriter fileWriter = new FileWriter("PersonalInformation.txt");
  91.  
  92.             ob.add(infoOb);
  93.             hashMap.put(infoOb, true);
  94.  
  95.             fileWriter.write(infoArr[0]+" "+infoArr[1]+" "+infoArr[2]+"\n");
  96.             System.out.println("write to file successfully");
  97.             fileWriter.close();
  98.         }
  99.         else
  100.             System.out.println("this user already exist");
  101.  
  102.  
  103.     }
  104. }
  105.  
  106.  
  107.  
  108. package com.company;
  109.  
  110. public class Person  implements Comparable<Person>{
  111.  
  112.  
  113.     String firstName;
  114.     String lastName;
  115.     String pid;
  116.  
  117.  
  118.     public Person(String firstName, String lastName, String pid) {
  119.         this.firstName = firstName;
  120.         this.lastName = lastName;
  121.         this.pid = pid;
  122.     }
  123.  
  124.  
  125.     public  int hashCode(){
  126.             System.out.println("In hashcode");
  127.             int hashcode = 0;
  128.             hashcode += pid.hashCode();
  129.             return hashcode;
  130.  
  131.     }
  132.  
  133.     public boolean equals(Object obj){
  134.         System.out.println("In equals");
  135.         if (obj instanceof Person) {
  136.             Person pp = (Person) obj;
  137.             return (pp.pid.equals(this.pid));
  138.         } else {
  139.             return false;
  140.         }
  141.     }
  142.  
  143.  
  144.     @Override
  145.     public String toString() {
  146.         return firstName;
  147.     }
  148.  
  149.  
  150.     @Override
  151.     public int compareTo(Person o) {
  152.         return firstName.compareTo(o.lastName);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement