corsoJava
  • Corso JAVA
  • Introduzione linguaggio
  • Verifica tipi primitivi vs reference
  • Esercizi su equals
  • Introduzione su oggetti
  • Packages e import
  • Polimorfismo
    • Pit stop
  • Enum
  • String è speciale
  • Eccezioni
  • Nested Classes
  • Array, ArrayList e Hash Table
    • Esempio gioco carte
  • Linked data structures
  • Tipi generici
  • Comparing Java and C# Generics - Jonathan Pryor's web log
  • Contenitori
    • Esempi con classi container
  • Input/Output streams, Files
  • Basic I/O
  • Java IO Tutorial
  • Networking
  • I Thread e concorrenza
    • Esercizi multithreading
    • Thread interference
    • Esercizi thread interference
    • wait(), notify() e notifyAll() per la sincronizzazione tra i thread
    • Verifiche produttore/consumatore
    • Lock esplicito - java.util.concurrent.Locks
    • Semafori
    • Programmare con i thread
    • I Virtual Thread
    • Materiale
  • I Thread e networking
  • Esempi Java Socket Programming
  • Esempi Javascript Socket Programming
  • Messaggi datagram e invio multicast
  • Lambda Expression
  • Java Stream
  • Data Oriented Programming in Java
  • Java improved its 'Hello World' experience
  • Appendice A: utilizzo classe Scanner
  • Java For The Experienced Beginner
  • Modern Java
  • CodeJava - Java Core
  • Is OOP Relevant Today?
  • Book
Powered by GitBook
On this page
  • Utilizzo di TreeSet
  • Utilizzo di TreeMap
  • ESEMPI
  1. Contenitori

Esempi con classi container

Esempi

PreviousContenitoriNextInput/Output streams, Files

Last updated 6 years ago

Utilizzo di TreeSet

Esempio di utilizzo di un java.util.TreeSet<String> per contenere le parole univoche, lette in input da un file di testo, e poi stampare il risultato in un file di testo: .

Notate che l'output delle parole è ordinato in ordine lessicografico crescente proprio per l'utilizzo del TreeSet, se avessimo usato, come implementazione del Set, l'HashSet, l'output non sarebbe stato ordinato: l'iteratore su il TreeSet, ritorna gli elementi ordinati in modo crescente.

Utilizzo di TreeMap

Esempio programma, , che conta l'occorrenza delle parole in un file di testo di input e stampa in output per ogni parola il numero di occorrenze.

La classe per contenere il numero delle occorrenze:

    /**
     * Represents the data we need about a word:  the word and
     * the number of times it has been encountered.
     */
    private class WordData { 
        String word;
        int count;
        WordData(String w) {
                // Constructor for creating a WordData object when
                // we encounter a new word.
            word = w;
            count = 1;  // The initial value of count is 1.
        }
    } // end class WordData

La collezione utilizzata è TreeMap<String, WordData>. L' utilizzo di una TreeMap come implementazione di java.util.Map, rispetto ad un'HashMap, garantisce che l'iteratore sulle chiavi, restituisca le chiavi ordinate in modo crescente.

ESEMPI

WorldWithTreeSet
WordCount
https://github.com/checksound/EsempioCollections