Aggiunta di parziale
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#Sat May 09 20:34:12 CEST 2026
|
||||
#Sun May 10 01:31:23 CEST 2026
|
||||
host=DESKTOP-ALBQHEK
|
||||
process-id=5732
|
||||
process-id=13140
|
||||
user=eslusarz
|
||||
|
||||
+129
@@ -488,3 +488,132 @@ This warning can be switched off on the Team > Git > Confirmations and Warnings
|
||||
|
||||
!ENTRY org.eclipse.e4.ui.workbench 2 0 2026-05-09 20:34:38.769
|
||||
!MESSAGE Removing HandlerImpl with the "org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CloseDialogHandler" id.It points to the non available "bundleclass://org.eclipse.e4.ui.workbench.renderers.swt/org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CloseDialogHandler" class. Bundle might have been uninstalled
|
||||
!SESSION 2026-05-10 01:30:19.995 -----------------------------------------------
|
||||
eclipse.buildId=4.39.0.20260305-0817
|
||||
java.version=21.0.10
|
||||
java.vendor=Eclipse Adoptium
|
||||
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_GB
|
||||
Framework arguments: -product org.eclipse.epp.package.java.product
|
||||
Command-line arguments: -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.java.product
|
||||
|
||||
!ENTRY ch.qos.logback.classic 1 0 2026-05-10 01:31:16.787
|
||||
!MESSAGE Activated before the state location was initialized. Retry after the state location is initialized.
|
||||
|
||||
!ENTRY org.eclipse.core.resources 2 10035 2026-05-10 01:31:23.569
|
||||
!MESSAGE The workspace exited with unsaved changes in the previous session; refreshing workspace to recover changes.
|
||||
|
||||
!ENTRY ch.qos.logback.classic 1 0 2026-05-10 01:31:26.705
|
||||
!MESSAGE Logback config file: C:\Users\eslusarz\Documents\eclipse-workspace\.metadata\.plugins\org.eclipse.m2e.logback\logback.2.7.101.20251017-1242.xml
|
||||
|
||||
!ENTRY org.eclipse.e4.ui.workbench 4 0 2026-05-10 01:31:29.507
|
||||
!MESSAGE Unable to load class 'org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CocoaUIHandler' from bundle '147'
|
||||
!STACK 0
|
||||
java.lang.ClassNotFoundException: org.eclipse.e4.ui.workbench.renderers.swt.cocoa.CocoaUIHandler cannot be found by org.eclipse.e4.ui.workbench.renderers.swt_0.17.0.v20260131-0926
|
||||
at org.eclipse.osgi.internal.loader.BundleLoader.generateException(BundleLoader.java:570)
|
||||
at org.eclipse.osgi.internal.loader.BundleLoader.findClass0(BundleLoader.java:564)
|
||||
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:439)
|
||||
at org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:195)
|
||||
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
|
||||
at org.eclipse.osgi.internal.framework.EquinoxBundle.loadClass(EquinoxBundle.java:663)
|
||||
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.createFromBundle(ReflectionContributionFactory.java:88)
|
||||
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.doCreate(ReflectionContributionFactory.java:59)
|
||||
at org.eclipse.e4.ui.internal.workbench.ReflectionContributionFactory.create(ReflectionContributionFactory.java:37)
|
||||
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.createE4Workbench(E4Application.java:276)
|
||||
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:603)
|
||||
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:339)
|
||||
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:583)
|
||||
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:173)
|
||||
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:185)
|
||||
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:219)
|
||||
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:149)
|
||||
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:115)
|
||||
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:467)
|
||||
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:298)
|
||||
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
|
||||
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
|
||||
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:615)
|
||||
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:563)
|
||||
at org.eclipse.equinox.launcher.Main.run(Main.java:1415)
|
||||
|
||||
!ENTRY org.eclipse.egit.ui 2 0 2026-05-10 01:32:02.503
|
||||
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
|
||||
user global configuration and to define the default location to store repositories: 'C:\Users\eslusarz'. If this is
|
||||
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
|
||||
EGit might behave differently since they see different configuration options.
|
||||
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
|
||||
|
||||
!ENTRY org.eclipse.jdt.ui 4 0 2026-05-10 01:42:30.620
|
||||
!MESSAGE AbortCompilation computing hover information in EsercizioParole.java at offset 631
|
||||
!STACK 0
|
||||
org.eclipse.jdt.internal.compiler.problem.AbortCompilation:
|
||||
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11585)
|
||||
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11975)
|
||||
at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:11916)
|
||||
at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:10308)
|
||||
at org.eclipse.jdt.core.dom.CompilationUnitResolver.parse(CompilationUnitResolver.java:656)
|
||||
at org.eclipse.jdt.core.dom.CompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:1432)
|
||||
at org.eclipse.jdt.core.dom.CompilationUnitResolver$ECJCompilationUnitResolver.toCompilationUnit(CompilationUnitResolver.java:109)
|
||||
at org.eclipse.jdt.core.dom.ASTParser.internalCreateASTCached(ASTParser.java:1412)
|
||||
at org.eclipse.jdt.core.dom.ASTParser.lambda$1(ASTParser.java:1291)
|
||||
at org.eclipse.jdt.internal.core.JavaModelManager.cacheZipFiles(JavaModelManager.java:5709)
|
||||
at org.eclipse.jdt.core.dom.ASTParser.internalCreateAST(ASTParser.java:1291)
|
||||
at org.eclipse.jdt.core.dom.ASTParser.createAST(ASTParser.java:933)
|
||||
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocContentAccessUtility.createAST(CoreJavadocContentAccessUtility.java:350)
|
||||
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocContentAccessUtility.getJavadocNode(CoreJavadocContentAccessUtility.java:318)
|
||||
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.javadoc2HTML(CoreJavadocAccess.java:423)
|
||||
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.getHTMLContentFromSource(CoreJavadocAccess.java:372)
|
||||
at org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess.getHTMLContent(CoreJavadocAccess.java:229)
|
||||
at org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2.getHTMLContent(JavadocContentAccess2.java:49)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.getHoverInfo(JavadocHover.java:794)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.internalGetHoverInfo(JavadocHover.java:712)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.JavadocHover.getHoverInfo2(JavadocHover.java:704)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:165)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:131)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89)
|
||||
at org.eclipse.jface.text.TextViewerHoverManager$1.run(TextViewerHoverManager.java:155)
|
||||
Suppressed: java.lang.Throwable: Source line 20 :
|
||||
-----
|
||||
/*
|
||||
* 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) {
|
||||
|
|
||||
-----
|
||||
at org.eclipse.jdt.internal.ui.util.SelectionUtil.logException(SelectionUtil.java:157)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:178)
|
||||
... 3 more
|
||||
|
||||
!ENTRY org.eclipse.jdt.ui 4 2 2026-05-10 01:59:29.866
|
||||
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ui".
|
||||
!STACK 0
|
||||
java.lang.NullPointerException: Cannot invoke "org.eclipse.jdt.core.dom.CompilationUnit.getModule()" because "astRoot" is null
|
||||
at org.eclipse.jdt.internal.ui.text.correction.proposals.AddModuleRequiresCorrectionProposalCore.createAddRequiresChange(AddModuleRequiresCorrectionProposalCore.java:87)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.proposals.AddModuleRequiresCorrectionProposalCore.createChange(AddModuleRequiresCorrectionProposalCore.java:73)
|
||||
at org.eclipse.jdt.core.manipulation.ChangeCorrectionProposalCore.getChange(ChangeCorrectionProposalCore.java:153)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.getCompositeChangeProposal(UnresolvedElementsBaseSubProcessor.java:1080)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.getCompositeChangeProposal(UnresolvedElementsBaseSubProcessor.java:1072)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.addSimilarTypeProposals(UnresolvedElementsBaseSubProcessor.java:1037)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsBaseSubProcessor.collectTypeProposals(UnresolvedElementsBaseSubProcessor.java:791)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.UnresolvedElementsSubProcessor.getTypeProposals(UnresolvedElementsSubProcessor.java:143)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.process(QuickFixProcessor.java:450)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.QuickFixProcessor.getCorrections(QuickFixProcessor.java:370)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionCollector.safeRun(JavaCorrectionProcessor.java:381)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.run(JavaCorrectionProcessor.java:341)
|
||||
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:47)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor$SafeCorrectionProcessorAccess.process(JavaCorrectionProcessor.java:336)
|
||||
at org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor.collectCorrections(JavaCorrectionProcessor.java:465)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.getJavaAnnotationFixes(ProblemHover.java:115)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.computeCompletionProposals(ProblemHover.java:89)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover$ProblemInfo.<init>(ProblemHover.java:74)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.ProblemHover.createAnnotationInfo(ProblemHover.java:178)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.AbstractAnnotationHover.getHoverInfo2(AbstractAnnotationHover.java:957)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:165)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.BestMatchHover.getHoverInfo2(BestMatchHover.java:131)
|
||||
at org.eclipse.jdt.internal.ui.text.java.hover.JavaEditorTextHoverProxy.getHoverInfo2(JavaEditorTextHoverProxy.java:89)
|
||||
at org.eclipse.jface.text.TextViewerHoverManager$1.run(TextViewerHoverManager.java:155)
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T>
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
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() {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T
|
||||
}
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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 = HashSet<Integer>();
|
||||
Iterator<Integer> it = s.iterator();
|
||||
while (it.hasNext()) abs.add(Math.abs(it.next()));
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Operazioni di insiemistica
|
||||
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
// Insiemi di test
|
||||
Integer[] aArray = {1, -2, 3, -4, 3};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String> it = lista.iterator();
|
||||
while (it.hasNext()) {
|
||||
listaOrd.add(it.next());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T>
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(List<T> ) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> union(Set<T> s1, Set<T> s2) {
|
||||
Set<T> intersection = new HashSet<T>();
|
||||
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> union(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
|
||||
*/
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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>
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> union(Set<T> s1, Set<T> s2) {
|
||||
Set<T> intersection = new HashSet<T>();
|
||||
intersection.addAll(s1);
|
||||
intersection.retainAll(s2);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String> it = lista.iterator();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
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();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// Esercizio 3
|
||||
/*
|
||||
* Usare la classe TreeSet per eliminare i duplicati
|
||||
* da un iteratore su una struttura di elementi che implementano
|
||||
* Comparable<E>, restituendo in output un iteratore che scorre
|
||||
* i dati secondo l'ordine indotto dal metodo compareTo();
|
||||
*/
|
||||
public static <E extends Comparable >
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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>();
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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 = HashSet<Integer>();
|
||||
Iterator<Integer> it = s.iterator();
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T> tmp =
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -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();
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
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)
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> union(Set<T> s1, Set<T> s2) {
|
||||
Set<T> intersection = new HashSet<T();
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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>();
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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
|
||||
*/
|
||||
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
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>();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
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) {
|
||||
return s2.length().compareTo(s1.length());
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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();
|
||||
System.out.println(lista);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return listaOrd;
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T> tmp = new HashSet<T>();
|
||||
while ()
|
||||
}
|
||||
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
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());
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
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)
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T> tmp = new HashSet<T>();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// Esercizio 3
|
||||
/*
|
||||
* Usare la classe TreeSet per eliminare i duplicati
|
||||
* da un iteratore su una struttura di elementi che implementano
|
||||
* Comparable<E>, restituendo in output un iteratore che scorre
|
||||
* i dati secondo l'ordine indotto dal metodo compareTo();
|
||||
*/
|
||||
public static <E extends Comparable<? super E>> Iterator<E>
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// Esercizio 3
|
||||
/*
|
||||
*
|
||||
*/
|
||||
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates(Iterator<T> it) {
|
||||
HashSet<T> tmp = new HashSet<T>();
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
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() {
|
||||
@Override
|
||||
public int compare(String s1, String s2) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
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) {
|
||||
return s2.length().compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <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.
|
||||
*/
|
||||
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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();
|
||||
|
||||
// Stampa collections
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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 () {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList(listaOrd);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Operazioni di insiemistica
|
||||
|
||||
/*
|
||||
* Per eseguire una unione, è conveniente
|
||||
* adoperare la Set poichè l'implementazione
|
||||
* TreeSet permette di non mantenere duplicati.
|
||||
*/
|
||||
public static <T>
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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>();
|
||||
}
|
||||
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
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>();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
new IteratoreSenzaDuplicati().run();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
List<String> lista =
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
private static <T> Iterator<T> getIteratorNoDuplicates() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String> it = lista.iterator();
|
||||
while (it.hasNext()) {
|
||||
listaOrd.add();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if (lista == null || lista.isEmpty()) return new ArrayList<String>(listaOrd);
|
||||
|
||||
Iterator<String> it = lista.iterator();
|
||||
while (it.hasNext()) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
// Insiemi di test
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Esercizio 1
|
||||
/*
|
||||
* Ottenere una lista con duplicati.
|
||||
* Ottenere un iteratore privo di duplicati.
|
||||
*/
|
||||
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
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> itNoDup = getIteratorNoDuplicatesOrdinated();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
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> itNoDup = getIteratorNoDuplicatesOrdinated(lista.iterator());
|
||||
while (itNoDup.hasNext()) testList.add();
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
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));
|
||||
|
||||
// Unione
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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 =
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class Insiemistica {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> union(Set<T> s1, Set<T> s2) {
|
||||
Set<T> intersection = new HashSet<T>();
|
||||
intersection.addAll(s1);
|
||||
intersection.retainAll(s2);
|
||||
return intersection;
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Iterator;
|
||||
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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Comparator;
|
||||
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) {
|
||||
return s2.compareTo(s1);
|
||||
}
|
||||
});
|
||||
|
||||
if ()
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
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())
|
||||
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
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
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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>>
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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> s1) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
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();
|
||||
}
|
||||
|
||||
// Esercizio 3
|
||||
/*
|
||||
* Usare la classe TreeSet per eliminare i duplicati
|
||||
* da un iteratore su una struttura di elementi che implementano
|
||||
* Comparable<E>, restituendo in output un iteratore che scorre
|
||||
* i dati secondo l'ordine indotto dal metodo compareTo();
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
public class IteratoreSenzaDuplicati {
|
||||
|
||||
public static void main(String[] main) {
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
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() {
|
||||
@Override
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
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));
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 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>
|
||||
}
|
||||
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package jcf_set.exercise;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
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 (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);
|
||||
}
|
||||
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
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();
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user