|
package esercizicollection;
import java.io.*;
import java.util.*;
public class Main {
Set s = new HashSet();//insieme senza duplicati non ordinato
Set st =new TreeSet();//insieme senza duplicati ordinato
Map m = new HashMap();//tabella non ordinato, tempo d'accesso costante
Map ms = new TreeMap();//tabella ordinato, tempo di accesso non costante
//put inserisce in tabella una coppia (chiave, elemento)
//get accede a un elemento in tabella, data la chiave
while (true) {
//System.out.println("inse");
stringa = tast.readLine();
if (stringa.equals("")) {
break;
} else {
lista.add(stringa);
s.add(stringa);//HashSet
st.add(stringa);//TreeSet
//hashMap
m. put(stringa, (freq == null ? new Integer(1 ) : new Integer(freq. intValue() + 1 )));
//TreeMap
ms. put(stringa, (freq == null ? new Integer(1 ) : new Integer(freq. intValue() + 1 )));
}
}
System. out. println("Stampa HashSet con print " + s + "\n");
System. out. println("Stampa TreeSet con print " + st + "\n");
//esempio stampa elementi con iteratore
System. out. println("\nStampa HashSet con iterator \n");
for (Iterator i = s. iterator(); i. hasNext();) {//stampa s
System. out. print(i. next() + "\t");
}
System. out. println("\n\nStampa TreeSet con iterator \n");
for (Iterator i = st. iterator(); i. hasNext();) {//stampa st
System. out. print(i. next() + "\t");
}
//stampa parole distinte di HashMap (non ordinata)
System. out. println("\n\n" + m. size() + " parole distinte di HashMap:");
//stampa parole distinte di TreeMap (ordinata)
System. out. println("\n\n" + ms. size() + " parole distinte di TreeMap:");
Record[] r = {
new Record("d"),
new Record("f"),
new Record("d"),
new Record("s"),
new Record("c"),
new Record("b"),
new Record("u"),
new Record("l"),};
System. out. println("\n\n Lista ottenuta da un vettore\n"+l );
System. out. println("\n\n Lista di tutti gli elementi inseriti da tastiera\n"+lista );
//prova for
System. out. println("\nStampa lista con enhanced for\n");
}
}
}
|
|