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.
-
You probably want to look at a tree map - docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html where the key is the student object and the values are an integer of the grades.Brian Hoover– Brian Hoover12/01/2013 01:23:51Commented Dec 1, 2013 at 1:23
3 Answers 3
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.
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
.
-
I can't use TreeMap because LinkedHashMap is required for this project :) I've read about ituser3043278– user304327812/01/2013 01:28:14Commented 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.diegoaguilar– diegoaguilar12/01/2013 01:29:22Commented Dec 1, 2013 at 1:29
-
I could use an example to follow, if you want to share your code I'm openeduser3043278– user304327812/01/2013 01:32:28Commented Dec 1, 2013 at 1:32
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 usingput
method.obtenerUsuarios
iterates over the map's keys usingkeySet
method.obtenerSaludos
iterates over the map's values usingvalues
method.obtenerResultados
is a combination of both methods.
-
By the way, Usuario means "User" and Saludo means "Greeting". That's spanish ;)diegoaguilar– diegoaguilar12/01/2013 01:44:40Commented Dec 1, 2013 at 1:44
-
I use
StringBuilder
to construct Strings while iterating over the Map.diegoaguilar– diegoaguilar12/01/2013 01:47:31Commented Dec 1, 2013 at 1:47 -
1Thanks 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 answeruser3043278– user304327812/01/2013 01:49:56Commented 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 ;)diegoaguilar– diegoaguilar12/01/2013 01:51:26Commented Dec 1, 2013 at 1:51
-
Thank you it helped me to fix my problem :)user3043278– user304327812/04/2013 11:28:48Commented Dec 4, 2013 at 11:28