Doubt with exercises with Java maps

0

Suppose there is a trend dictionary with a string representing a date (mm-dd-yyyy) as a key and as a value a set of tags (hashtags) that were trends on Twitter for that date. For example:

  

trends = {'08 -22-2016 ': {' # Rio2016 ',' #BSC ',' #ECU '},   '08 -25-2016 ': {' # GYE ',' #BRA '}, ..., '08 -27- 2016': {'# YoSoyEspol',   '#GYE', '#BSC'}}

Implement:

a) report Trends (trends, date list) that the trend dictionary receives and a list with strings that represent dates (mm-dd-yyyy). The function should show on screen:

  • 1) The labels that were trending all dates in the Date list
  • 2) The labels that were trending at least on one of the dates in the Date list
 public static void reportaTendencias(HashMap<String, LinkedList<String>> 
     m, LinkedList<String> l) {
            for (String clave : m.keySet()) {
                for (String valor : m.get(clave)) {
                System.out.println("fecha: " + clave + "," + "etiquetas: " + valor);
            }
        }
    }

I did this for literal 1 and it works but I do not know if it's efficient and 2 I'm not sure how to do it.

b) Excluding trends (trends, date1, date2) that the trend dictionary receives and two strings that represent dates (mm-dd-yyyy). The function should show the labels on the screen which were trends or on date1 or date2, but not both. Note: suppose that date1 and date2 exist in the dictionary as keys.

  public static void tendenciasExcluyentes(HashMap<String, 
 LinkedList<String>> m, String fecha1, String fecha2) {
       LinkedList<String> lista = new LinkedList();
        if (m.containsKey(fecha1)) {
             System.out.println(m.get(fecha1));
       }
   }
 } 

And for this I'm not sure how to continue

    
asked by Michelle 17.02.2018 в 01:33
source

1 answer

0

I think you were raising the problem wrong. On the one hand, you have to use the list of dates that pass you by parameter.

As I imagine it is part of the structure test exercise, here I give you a solution using the same structure for auxiliary purposes.

/ *     a) report Trends (trends, date list) that the trend dictionary receives and a list with strings that represent dates (mm-dd-yyyy). The function should show on screen:

1) Las etiquetas que fueron tendencia todas las fechas en listaFechas
2) Las etiquetas que fueron tendencia al menos en una de las fechas en listaFechas

 */
public static void reportaTendencias(Map<String, LinkedList<String>> m, LinkedList<String> l) {
    System.out.println("Ejercicio A, apartado 1");
    // Recorrermos las fechas que nos pasan por parametro
    Map<String, Integer> coincidencias = new HashMap<>(); //Recuerda que en Maps la Key no puede repetirse
    for (String clave : l) {
        //Obtenemos el valor asociado a la clave de la fecha
        for (String valor : m.get(clave)) {
            // Si el valor ya existe en nuestro mapeo coincidencias, simplemente aumentamos el numero de veces encontrado
            if (coincidencias.containsKey(valor)) {
                coincidencias.put(valor, coincidencias.get(valor) + 1);
            } else {
                // En caso contrario debemos meter el valor nuevo
                coincidencias.put(valor, 1);
            }
        }
    }
    // Imprimimos solo aquellos valores que hayan aparecido en TODAS las fechas
    for (String valor : coincidencias.keySet()) {
        if (coincidencias.get(valor) == l.size()) {
            System.out.print(valor + " ");
        }
    }
    System.out.println("");
    //Recorremos la lista de fechas y guardamos el valor
    System.out.println("Ejercicio A, apartado 2");
    coincidencias.clear();
    for (String clave : l) {
        for (String valor : m.get(clave)) {
            coincidencias.put(valor, 1);
        }
    }

    // Imprimimos los valores una unica vez
    for (String valor : coincidencias.keySet()) {
        System.out.print(valor + " ");
    }
    System.out.println("");
}

public static void tendenciasExcluyentes(Map<String, LinkedList<String>> m, String fecha1, String fecha2) {
    System.out.println("Ejercicio B");
    Map<String, Integer> coincidencias = new HashMap<>(); //Recuerda que en Maps la Key no puede repetirse
    for (String clave : m.keySet()) {
        //Solo comprobaremos aquellos que coincidan con las fechas pasadas por parametro
        if (clave.equals(fecha1) || clave.equals(fecha2)) {
            for (String valor : m.get(clave)) {
                // Si el valor ya existe en nuestro mapeo coincidencias, simplemente aumentamos el numero de veces encontrado
                if (coincidencias.containsKey(valor)) {
                    coincidencias.put(valor, coincidencias.get(valor) + 1);
                } else {
                    // En caso contrario debemos meter el valor nuevo
                    coincidencias.put(valor, 1);
                }
            }
        }
    }
    // NOTA: Ponemos 1 porque solo permitimos que apareza una vez
    for (String valor : coincidencias.keySet()) {
        if (coincidencias.get(valor) == 1) {
            System.out.print(valor + " ");
        }
    }

}
    
answered by 17.02.2018 / 16:26
source