0

I'm trying to make a project and I have the following issue: the project is something related to students. The class Student contains 3 fields id, name, grade. I have to represent the students collection using a linkedHashMap, id being the key. I have tried this but I'm not sure is ok because the name is a string and the grade an int.

Map<Integer,ArrayList> myMap = new LinkedHashMap<Integer,ArrayList>();

Could you give me some ideas how to represent this collection? I need to be able to add/remove a student.

asked Dec 1, 2013 at 1:21
1

3 Answers 3

1

Why not just map from integer to student then?

Map<Integer,Student> myMap = new LinkedHashMap<Integer,Student>();

Add students via

myMap.put(student.id(), student);

A linked hash map preserves insertion order. A TreeMap would keep the students sorted by id; a plain HashMap avoids some overhead compared to a LinkedHashMap when no particular order is required.

answered Dec 1, 2013 at 1:25
0
0

You may include another LinkedHashMap inside first one.

Your declaration would be like:

Map<Integer, LinkedHashMap> my Map = new LinkedHashMap<Integer, LinkedHashMap>();

Or you might use a TreeMap instead.

Then you can just use the put, get and remove methods. Remember you can also use keySet and values to iterate and even containsKey.

answered Dec 1, 2013 at 1:24
3
  • I can't use TreeMap because LinkedHashMap is required for this project :) I've read about it Commented Dec 1, 2013 at 1:28
  • i agree with the other answer, I even already implemented SAME thing in the same way with HashMaps, I may share the code if you want. Commented Dec 1, 2013 at 1:29
  • I could use an example to follow, if you want to share your code I'm opened Commented Dec 1, 2013 at 1:32
0

You can add custom classes inside a Map:

Usuario class

public class Usuario implements Serializable {
 private String nombre;
 private String ciudadOrigen;
 private String paisOrigen;
 private String idiomaMaterno;
 private int edad;
 public Usuario(String nombre, String ciudadOrigen, String paisOrigen, String idiomaMaterno, int edad) {
 this.nombre = nombre;
 this.ciudadOrigen = ciudadOrigen;
 this.paisOrigen = paisOrigen;
 this.idiomaMaterno = idiomaMaterno;
 this.edad = edad;
 }
 public Usuario(String nombre, String paisOrigen, String idiomaMaterno) {
 this.nombre = nombre;
 this.paisOrigen = paisOrigen;
 this.idiomaMaterno = idiomaMaterno;
 }
 public Usuario(String nombre, String ciudadOrigen, String paisOrigen, String idiomaMaterno) {
 this.nombre = nombre;
 this.ciudadOrigen = ciudadOrigen;
 this.paisOrigen = paisOrigen;
 this.idiomaMaterno = idiomaMaterno;
 }
 public String getIdiomaMaterno() {
 return idiomaMaterno;
 }
 public void setIdiomaMaterno(String idiomaMaterno) {
 this.idiomaMaterno = idiomaMaterno;
 }
 public String getNombre() {
 return nombre;
 }
 public void setNombre(String nombre) {
 this.nombre = nombre;
 }
 public String getCiudadOrigen() {
 return ciudadOrigen;
 }
 public void setCiudadOrigen(String ciudadOrigen) {
 this.ciudadOrigen = ciudadOrigen;
 }
 public String getPaisOrigen() {
 return paisOrigen;
 }
 public void setPaisOrigen(String paisOrigen) {
 this.paisOrigen = paisOrigen;
 }
 public int getEdad() {
 return edad;
 }
 public void setEdad(int edad) {
 this.edad = edad;
 }
 }

Saludo class

public class Saludo implements Serializable {
 private String saludo;
 private String idioma;
 public Saludo(String saludo, String idioma) {
 this.saludo = saludo;
 this.idioma = idioma;
 }
 public String getIdioma() {
 return idioma;
 }
 public void setIdioma(String idioma) {
 this.idioma = idioma;
 }
 public String getSaludo() {
 return saludo;
 }
 public void setSaludo(String saludo) {
 this.saludo = saludo;
 }
}

Implementation

public class ModeloDatos implements Serializable {
 private HashMap<Usuario, Saludo> usuariosSaludos = new HashMap<>();
 public void ingresar (Usuario usuario, Saludo saludo) {
 usuariosSaludos.put(usuario, saludo);
 }
 public String obtenerUsuarios () {
 StringBuilder stringBuilder;
 if (usuariosSaludos.isEmpty()) return "Lista vacía";
 else {
 stringBuilder = new StringBuilder("Lista de usuarios:\n");
 for (Usuario usuario : usuariosSaludos.keySet()) 
 stringBuilder.append(usuario.getNombre()).
 append(" de ").
 append(usuario.getPaisOrigen()).
 append(".\n");
 }
 return stringBuilder.toString();
 }
 public String obtenerSaludos () {
 StringBuilder stringBuilder;
 if (usuariosSaludos.isEmpty()) return "Lista vacía";
 else {
 stringBuilder = new StringBuilder("Lista de saludos:\n");
 for (Saludo saludo : usuariosSaludos.values()) 
 stringBuilder.append("Se dice ").
 append(saludo.getSaludo()).
 append(" en ").
 append(saludo.getIdioma()).
 append(".\n");
 }
 return stringBuilder.toString();
 }
 public String obtenerResultados() {
 StringBuilder stringBuilder;
 if (usuariosSaludos.isEmpty()) return "Lista vacía";
 else {
 stringBuilder = new StringBuilder("Resultados:\n\n");
 for (Usuario usuario : usuariosSaludos.keySet()) {
 Saludo s = usuariosSaludos.get(usuario);
 stringBuilder.append(usuario.getNombre()).
 append(" de ").
 append(usuario.getPaisOrigen()).
 append(".\n").
 append("Nos dice ").
 append(s.getSaludo()).
 append("en ").
 append(s.getIdioma()).append("\n\n");
 }
 }
 return stringBuilder.toString(); 
 }
}

Where:

  • ingresar adds an entry to the map using put method.
  • obtenerUsuarios iterates over the map's keys using keySet method.
  • obtenerSaludos iterates over the map's values using values method.
  • obtenerResultados is a combination of both methods.
answered Dec 1, 2013 at 1:42
6
  • By the way, Usuario means "User" and Saludo means "Greeting". That's spanish ;) Commented Dec 1, 2013 at 1:44
  • I use StringBuilder to construct Strings while iterating over the Map. Commented Dec 1, 2013 at 1:47
  • 1
    Thanks I will explore it deeply but now I don't really have the time to do it. I promise that after I do it I'll come back with an answer Commented Dec 1, 2013 at 1:49
  • No worries, in your case you would only have an Integer as Key. So far would be exactly the same programm ;) Commented Dec 1, 2013 at 1:51
  • Thank you it helped me to fix my problem :) Commented Dec 4, 2013 at 11:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.