gonzalob

Untitled

Apr 17th, 2020
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package clases_abs;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Map.Entry;
  10. import java.util.Set;
  11.  
  12. public class Main {
  13.  
  14.     public static void main(String[] args) {
  15.         // TODO Auto-generated method stub
  16.     //  Libro oLibro = new Libro(); //No se puede!
  17.         Libro oLibro = new LibroElectronico(20,"pdf");//si se puede! principio de susticion!
  18.        
  19.        
  20.         Libro oLibro2 = new LibroImpreso(10);//si se puede! principio de susticion!
  21.  
  22.                
  23.         if (oLibro instanceof LibroElectronico)
  24.         {
  25.             LibroElectronico prueba = (LibroElectronico)oLibro;
  26.             prueba.getFormato();
  27.         }
  28.        
  29.         ArrayList<Libro> libros = new ArrayList<Libro>();
  30.        
  31.        
  32.         libros.add(oLibro2);
  33.         libros.add(oLibro);
  34.        
  35.         int f = Collections.frequency(libros, oLibro);
  36.        
  37.         Libro oLibroMax = Collections.max(libros);
  38.        
  39.         System.out.println(oLibroMax.getPrecio());
  40.        
  41.         /*
  42.        
  43.         for (Libro aux : libros)
  44.         {
  45.             System.out.println(aux.getPrecio());
  46.         }
  47.        
  48.         for (int i=0;i<libros.size();i++)
  49.         {
  50.             System.out.println(libros.get(i).getPrecio());
  51.         }
  52.         */
  53.        
  54.         HashSet<Libro> hashSet = new HashSet<Libro>();
  55.         System.out.println(hashSet.add(oLibro));
  56.         System.out.println(hashSet.add(oLibro2));
  57.        
  58.         Iterator<Libro> iterator = hashSet.iterator();
  59.         while(iterator.hasNext())
  60.         {
  61.             Libro temp = (Libro)iterator.next();
  62.             System.out.println(temp.getCantidadhojas());
  63.         }
  64.        
  65.         HashSet<String> hset = new HashSet<String>();
  66.  
  67.           // Adding elements to the HashSet
  68.           hset.add("Apple");
  69.           hset.add("Mango");
  70.           hset.add("Grapes");
  71.           hset.add("Orange");
  72.           hset.add("Fig");
  73.           //Addition of duplicate elements
  74.           hset.add("Apple");
  75.           hset.add("Mango");
  76.           //Addition of null values
  77.           hset.add(null);
  78.           hset.add(null);
  79.  
  80.           //Displaying HashSet elements
  81.           System.out.println(hset);
  82.          
  83.           HashMap<Integer, Libro> hashmap2 = new HashMap<Integer, Libro>();//es un ejemplo con numeros
  84.          
  85.           HashMap<String, Libro> hashmap = new HashMap<String, Libro>();
  86.          
  87.           //genero
  88.           HashMap<String, ArrayList<Libro>> hashmap3 = new HashMap<String,  ArrayList<Libro>>();
  89.          
  90.          
  91.           boolean existeClave = hashmap.containsKey("libro 1");
  92.          
  93.           String clave = "aaaa";
  94.          
  95.           hashmap.put(clave, oLibro);
  96.           hashmap.put("libro 2", oLibro2);
  97.           hashmap.put("123", oLibro);
  98.           hashmap.put("libro 1", oLibro2);//Se pisa
  99.          // hashmap.put("aaa", "1");
  100.          
  101.           System.out.println(hashmap.get("libro 1").getCantidadhojas());
  102.          
  103.           Set<Entry<String, Libro>> set = hashmap.entrySet();
  104.          
  105.           Iterator<Entry<String, Libro>> it = set.iterator();
  106.          
  107.           System.out.println("-----------LISTA------------");
  108.          
  109.           while (it.hasNext())
  110.           {
  111.               Map.Entry<String, Libro> me = (Map.Entry<String, Libro>) it.next();
  112.              
  113.               System.out.println(me.getKey());
  114.              
  115.               Libro aux = (Libro)me.getValue();
  116.               System.out.println(aux.getCantidadhojas());
  117.           }
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment