programación i - Universidad ORT Uruguay
programación i - Universidad ORT Uruguay
programación i - Universidad ORT Uruguay
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Programación I 163<br />
public int compareTo(Camion otro){<br />
return this.getChapa()-otro.getChapa();<br />
}<br />
2) HashMap<br />
package practico12;<br />
//Es necesario importar java.util.*<br />
import java.util.*;<br />
import camion.*;<br />
public class ClaseHashMap {<br />
public static void main(String[] args) {<br />
// Defino un HashMap<br />
HashMap miMapa = new HashMap();<br />
// Creo cuatro camiones<br />
Camion c1 = new Camion("blanco", 123);<br />
Camion c2 = new Camion("azul", 789);<br />
Camion c3 = new Camion("verde", 456);<br />
Camion c4 = new Camion("rojo", 567);<br />
miMapa.put(c1, 0);<br />
miMapa.put(c2, 3);<br />
miMapa.put(c3, 2);<br />
miMapa.put(c4, 4);<br />
// Indico si c2 está en la estructura<br />
if (miMapa.containsKey(c2)) {<br />
System.out.println("c2 está en la estructura");<br />
} else {<br />
System.out.println("c2 no está en la estructura");<br />
}<br />
// Recupero el número anterior de revisiones<br />
int cantRevisionesC4 = miMapa.get(c4);<br />
// Agrego una revisión a c4<br />
miMapa.put(c4, cantRevisionesC4 + 1);<br />
// Listo los elementos<br />
Iterator miIterator = miMapa.entrySet().iterator();<br />
while (miIterator.hasNext()) {<br />
Map.Entry entrada = (Map.Entry) miIterator.next();<br />
System.out.println("Clave: " + entrada.getKey() + " Valor: "<br />
+ entrada.getValue());<br />
}<br />
}<br />
// ¿Hay algún Camion con 3 revisiones?<br />
if (miMapa.containsValue(3)) {<br />
System.out.println("Hay algun camión con 3 revisiones");<br />
} else {<br />
System.out.println("NO Hay ningun camión con 3 revisiones");<br />
}