JCF - Main.java
Cerca
 











Main.java

Caricato da: Bonny
Scarica il programma completo

  1. package esercizicollection;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) throws IOException {
  8.  
  9.  
  10.         InputStreamReader in = new InputStreamReader(System.in);
  11.         BufferedReader tast = new BufferedReader(in);
  12.  
  13.         String stringa = "";
  14.  
  15.         List lista = new ArrayList();//lista sequenziale
  16.  
  17.         Set s = new HashSet();//insieme senza duplicati non ordinato
  18.         Set st =new TreeSet();//insieme senza duplicati ordinato
  19.  
  20.         Map m = new HashMap();//tabella non ordinato, tempo d'accesso costante
  21.         Map ms = new TreeMap();//tabella ordinato, tempo di accesso non costante
  22.         //put inserisce in tabella una coppia (chiave, elemento)
  23.         //get accede a un elemento in tabella, data la chiave
  24.         Integer freq;
  25.  
  26.         while (true) {
  27.             //System.out.println("inse");
  28.             stringa = tast.readLine();
  29.             if (stringa.equals("")) {
  30.                 break;
  31.             } else {
  32.                 lista.add(stringa);
  33.                 s.add(stringa);//HashSet
  34.                 st.add(stringa);//TreeSet
  35.                 //hashMap
  36.                 freq = (Integer) m.get(stringa);
  37.                 m.put(stringa, (freq == null ? new Integer(1) : new Integer(freq.intValue() + 1)));
  38.                 //TreeMap
  39.                 freq = (Integer) ms.get(stringa);
  40.                 ms.put(stringa, (freq == null ? new Integer(1) : new Integer(freq.intValue() + 1)));
  41.             }
  42.         }
  43.  
  44.         System.out.println("Stampa HashSet con print " + s + "\n");
  45.  
  46.         System.out.println("Stampa TreeSet con print " + st + "\n");
  47.  
  48.         //esempio stampa elementi con iteratore
  49.         System.out.println("\nStampa HashSet con iterator \n");
  50.         for (Iterator i = s.iterator(); i.hasNext();) {//stampa s
  51.             System.out.print(i.next() + "\t");
  52.         }
  53.  
  54.         System.out.println("\n\nStampa TreeSet con iterator \n");
  55.         for (Iterator i = st.iterator(); i.hasNext();) {//stampa st
  56.             System.out.print(i.next() + "\t");
  57.         }
  58.         //stampa parole distinte di HashMap (non ordinata)
  59.         System.out.println("\n\n" + m.size() + " parole distinte di HashMap:");
  60.         System.out.println(m);
  61.  
  62.         //stampa parole distinte di TreeMap (ordinata)
  63.         System.out.println("\n\n" + ms.size() + " parole distinte di TreeMap:");
  64.         System.out.println(ms);
  65.  
  66.         Record[] r = {
  67.             new Record("d"),
  68.             new Record("f"),
  69.             new Record("d"),
  70.             new Record("s"),
  71.             new Record("c"),
  72.             new Record("b"),
  73.             new Record("u"),
  74.             new Record("l"),};
  75.         List l = Arrays.asList(r);
  76.         Collections.sort(l);
  77.         System.out.println("\n\n Lista ottenuta da un vettore\n"+l);
  78.  
  79.         Collections.sort(lista);
  80.         System.out.println("\n\n Lista di tutti gli elementi inseriti da tastiera\n"+lista);
  81.  
  82.         //prova for
  83.         System.out.println("\nStampa lista con enhanced for\n");
  84.         for(Object x : lista){
  85.          System.out.print(x+"\t");
  86.         }
  87.     }
  88. }
 

Creative Commons License
Il layout di questo sito è concesso sotto licenza Creative Commons.
Per maggiori informazioni sulle licenze dei contenuti del sito, clicca.