3.2.19
JAVA
4 The Question number
public static HashMap <String, Integer> Info = new HashMap<String, Integer>() ;
public static int countMSG ;
public HashTable(HashMap <String, Integer> hash , int counter) {//constructor
countMSG=counter;
Info= hash;
}
public static void GetMSG(Message m)//get message and check if the sender exist
{
int sended=0;
countMSG++;
if(Info.containsKey(m.getFrom()) && sended ==0) {
sended = Info.get(m.getFrom());
sended++;
Info.remove(m.getFrom());
Info.put(m.getFrom(), sended);
}
else {
Info.put(m.getFrom(), 1);
}
}
public static HashSet<String> AllSend10Precent(HashMap <String, Integer> M) {
HashSet <String> Answer = new HashSet <String>();
double Ten = countMSG *0.1;
System.out.println("10 % is : " +Ten);
double countofSMessage = countMSG*0.1;
System.out.println("count of Sender is : " + countofSMessage);
double stop=0;
if(!M.isEmpty()) {
for (Map.Entry<String, Integer> entry : M.entrySet()) {
System.out.println(M.entrySet());
if(entry.getValue() > Ten && stop < countofSMessage) {
Answer.add(entry.getKey());
stop = Answer.size();
System.out.println("Stop is : " +stop);
}
}
}
return Answer;
}
public static void main(Stringargs) {
int sum = 0 ;
HashMap <String, Integer> InfoMSG = new HashMap<String, Integer>();
HashTable ht = new HashTable(InfoMSG, sum);
Message sms = new Message ("dana","ron","ello" ,"");
ht.GetMSG(sms);
Message sms1 = new Message ("yoni","kim","nice", "");
ht.GetMSG(sms1);
Message sms2 = new Message ("yoni","bur","juli", "");
ht.GetMSG(sms2);
Message sms3 = new Message ("lol","pp","nice", "");
ht.GetMSG(sms3);
Message sms13 = new Message ("lol","pp","nce", "");
ht.GetMSG(sms13);
Message sms4 = new Message ("hadar","kim","nice", "");
ht.GetMSG(sms4);
Message sms5 = new Message ("yoni","kim","nice", "");
ht.GetMSG(sms5);
Message sms6 = new Message ("jim","kim","nice","");
ht.GetMSG(sms6);
Message sms7 = new Message ("kim","kim","nice","");
ht.GetMSG(sms7);
Message sms8 = new Message ("roni","kim","nice","");
ht.GetMSG(sms8);
Message sms9 = new Message ("asaf","kim","nice","");
ht.GetMSG(sms9);
Message sms10 = new Message ("ran","kim","nice","");
ht.GetMSG(sms10);
HashSet <String> setMSG = new HashSet<String>();
setMSG = AllSend10Precent(InfoMSG);
System.out.println(ht.countMSG);
System.out.println(setMSG);
}