Aggiunta di parziale
This commit is contained in:
@@ -10,9 +10,6 @@ public class Videogioco implements Comparable<Videogioco>{
|
||||
private Integer voto;
|
||||
|
||||
// Costruttore
|
||||
|
||||
private Videogioco() {}
|
||||
|
||||
public Videogioco(
|
||||
String titolo,
|
||||
String piattaforma,
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class EsercizioParole {
|
||||
|
||||
/*
|
||||
* EX1 del 27-01-2026 (I Appello)
|
||||
* Si scriva un metodo
|
||||
* public List<String> DifferentWordsList(List<String>)
|
||||
* che, data in input una lista di stringhe, restituisce una
|
||||
* nuova lista priva di elementi duplicati, contenente tutte le
|
||||
* stringhe distinte di list e ordinata per lunghezze decrescenti
|
||||
* (a parità di lunghezza ordinamento lessicografico crescente)
|
||||
*/
|
||||
public List<String> DifferentWordsList(List<String> lista) {
|
||||
Set<String> listaOrd = new TreeSet<String>(new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
int l1 = s1.length();
|
||||
int l2 = s2.length();
|
||||
|
||||
int cmp = Integer.compare(l2, l1);
|
||||
|
||||
if (cmp != 0) return cmp;
|
||||
|
||||
return s1.compareTo(s2);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String> it = lista.iterator();
|
||||
while (it.hasNext()) {
|
||||
listaOrd.add(it.next());
|
||||
}
|
||||
|
||||
return new ArrayList<String>(listaOrd);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
// Insiemi di test
|
||||
Integer[] aArray = {1, -2, 3, -4, 3};
|
||||
Integer[] bArray = {1, 5, -6, -4, 5};
|
||||
|
||||
// Input come HashSet (Ordine non garantito)
|
||||
Set<Integer> aHashSet = new HashSet<Integer>(Arrays.asList(aArray));
|
||||
Set<Integer> bHashSet = new HashSet<Integer>(Arrays.asList(bArray));
|
||||
|
||||
// Stampa delle due collezioni
|
||||
System.out.println("Stampa della collezone a: ");
|
||||
System.out.println(aHashSet);
|
||||
System.out.println("Stampa della collezione b: ");
|
||||
System.out.println(bHashSet);
|
||||
|
||||
// Unione
|
||||
System.out.println("Stampa della unione: ");
|
||||
System.out.println(union(aHashSet, bHashSet));
|
||||
|
||||
// Intersezione
|
||||
System.out.println("Stampa della intersezione: ");
|
||||
System.out.println(intersection(aHashSet, bHashSet));
|
||||
|
||||
// Difference
|
||||
System.out.println("Stampa della differenza: ");
|
||||
System.out.println(difference(aHashSet, bHashSet));
|
||||
|
||||
// Abs
|
||||
System.out.println("Stampa della ABS: ");
|
||||
System.out.println(abs(union(aHashSet, bHashSet)));
|
||||
|
||||
}
|
||||
|
||||
// Operazioni di insiemistica
|
||||
|
||||
/*
|
||||
* Per eseguire una unione, è conveniente
|
||||
* adoperare la Set poichè l'implementazione
|
||||
* TreeSet permette di non mantenere duplicati.
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> Set<T> union(Set<T> s1, Set<T> s2) {
|
||||
Set<T> union = new HashSet<T>();
|
||||
union.addAll(s1);
|
||||
union.addAll(s2);
|
||||
return union;
|
||||
}
|
||||
|
||||
/*
|
||||
* Per eseguire una intersezione, è conveniente
|
||||
* adoperare la Set allo stesso modo
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> Set<T> intersection(Set<T> s1, Set<T> s2) {
|
||||
Set<T> intersection = new HashSet<T>();
|
||||
intersection.addAll(s1);
|
||||
intersection.retainAll(s2);
|
||||
return intersection;
|
||||
}
|
||||
|
||||
/*
|
||||
* Per eseguire la differenza, si usa
|
||||
* la Set
|
||||
*/
|
||||
public static <T extends Comparable<? super T>> Set<T> difference(Set<T> s1, Set<T> s2) {
|
||||
Set<T> difference = new HashSet<T>();
|
||||
difference.addAll(s1);
|
||||
difference.removeAll(s2);
|
||||
return difference;
|
||||
}
|
||||
|
||||
public static Set<Integer> abs(Set<Integer> s) {
|
||||
Set<Integer> abs = new HashSet<Integer>();
|
||||
Iterator<Integer> it = s.iterator();
|
||||
while (it.hasNext()) abs.add(Math.abs(it.next()));
|
||||
return abs;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista = new ArrayList<String>();
|
||||
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("tree");
|
||||
lista.add("flower");
|
||||
lista.add("animal");
|
||||
lista.add("flower");
|
||||
lista.add("fruit");
|
||||
|
||||
// Stampa collections
|
||||
System.out.println("Lista con duplicati: ");
|
||||
System.out.println(lista);
|
||||
System.out.println();
|
||||
|
||||
// Test metodi privati
|
||||
List<String> testList = new LinkedList<String>();
|
||||
|
||||
Iterator<String> itNoDup = getIteratorNoDuplicates(lista.iterator());
|
||||
while (itNoDup.hasNext()) testList.add(itNoDup.next());
|
||||
|
||||
System.out.println("Lista senza duplicati: ");
|
||||
System.out.println(testList);
|
||||
System.out.println();
|
||||
|
||||
testList = new LinkedList<String>();
|
||||
System.gc();
|
||||
|
||||
Iterator<String> itNoDupOrd = getIteratorNoDuplicatesOrdinated(lista.iterator());
|
||||
while (itNoDupOrd.hasNext()) testList.add(itNoDupOrd.next());
|
||||
|
||||
System.out.println("Lista senza duplicati ordinata: ");
|
||||
System.out.println(testList);
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Passare una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T extends Comparable<? super T>> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T> tmp = new HashSet<T>();
|
||||
while (it.hasNext()) tmp.add(it.next());
|
||||
return tmp.iterator();
|
||||
}
|
||||
|
||||
// Esercizio 2
|
||||
/*
|
||||
* Passare come parametro una lista con duplicati.
|
||||
* Ottenere un iteratore senza duplicati e ordinato.
|
||||
*/
|
||||
private static <T extends Comparable<? super T>> Iterator<T> getIteratorNoDuplicatesOrdinated(Iterator<T> it) {
|
||||
TreeSet<T> tmp = new TreeSet<T>();
|
||||
while (it.hasNext()) tmp.add(it.next());
|
||||
return tmp.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user