Last active
September 19, 2020 07:21
-
-
Save javerosanonimos/0a4a4a7ddbf6d9e3945afcb56a064d5f to your computer and use it in GitHub Desktop.
Código para quitar elementos repetidos de una lista utilizando HashSet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashSet; | |
/** | |
@Autor: www.javerosanonimos.com | |
Twitter: @javerosanonimos | |
Facebook: www.facebook.com/JaverosAnonimos | |
e-mail: javerosanonimos@gmail.com | |
**/ | |
public class QuitarRepetidosConHashSet { | |
public static void main(String[] args) { | |
//Creamos un array | |
ArrayList array = new ArrayList(); | |
//Agregamos y repetimos varios valores | |
array.add("www.JaverosAnonimos.blogspot.com"); | |
array.add("www.JaverosAnonimos.tk"); | |
array.add("@JaverosAnonimos en Twitter"); | |
array.add("www.JaverosAnonimos.tk"); | |
array.add("www.JaverosAnonimos.blogspot.com"); | |
array.add("www.JaverosAnonimos.tk"); | |
array.add("@JaverosAnonimos en Twitter"); | |
array.add("www.JaverosAnonimos.tk"); | |
array.add("www.JaverosAnonimos.tk"); | |
array.add("www.JaverosAnonimos.blogspot.com"); | |
array.add("@JaverosAnonimos en Twitter"); | |
//Creamos un objeto HashSet | |
HashSet hs = new HashSet(); | |
//Lo cargamos con los valores del array, esto hace quite los repetidos | |
hs.addAll(array); | |
//Limpiamos el array | |
array.clear(); | |
//Agregamos los valores sin repetir | |
array.addAll(hs); | |
//Imprimimos el resultado | |
for (int i = 0; i < array.size(); i++) { | |
System.out.println(array.get(i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muchas gracias, me ha servido de mucho