-1

So I understand hashmaps usually have keys and values. I looked around and it was advised that if I want to display 3 values, I should use nested hashmaps. But now, I am unable to get the data from the hashmap. Please give me pointers on how I can improve the code below.

Initial code (Working)

String todo = task.getText().toString().trim();
String description = desc.getText().toString().trim();

Map<String, String> dataToSave = new HashMap<>();
        dataToSave.put("todo", todo);
        dataToSave.put("description", description);

Current code (Not working)

ListItems is a Java class where I defined to-do and description. I want to map userid to the key, and the to-do + description as a value.

String todo = task.getText().toString().trim();
String description = desc.getText().toString().trim();
String userid = mAuth.getCurrentUser().getUid();

Map<String, ListItem> dataToSave = new HashMap<>();
dataToSave.put("user", userid);
dataToSave.put("todo", BLANK);
dataToSave.put("description", BLANK);

So, the portions I put BLANK are actually the ones I need help in. How can I map the todo and description?

Thanks in advance.

  • Possible duplicate of [Iterate through a HashMap](https://stackoverflow.com/questions/1066589/iterate-through-a-hashmap) – ADM Jul 12 '18 at 03:34
  • I don't think it is a duplicate because one of the elements in the hashmap is an object, and not a value / integer. That's the portion I'm having trouble with. – codinglearner21 Jul 12 '18 at 03:41
  • any reasons why you didn't use `dataToSave.put(, BLANK);`? I hope BLANK is something like `ListItem BLANK = new ListItem("","");` – Sagar Jul 12 '18 at 04:00
  • `map.get("KEY")` will return you the Object . It is related to iteration is't it ? – ADM Jul 12 '18 at 04:30

1 Answers1

0

Before I tell you how to do what you are asking, did you try compiling the program? As it stands, your program will not compile. You hashmap is expecting a value whose object is of type ListItem. What you are passing it in your put() calls are String objects.

Does your ListItem class look like this?

class ListItem {
    String todo;
    String description;
}

If yes, then you need to add a constructor to instantiate the ListItem object. This is what it will look like.

class ListItem {
    String todo;
    String description;

    ListItem(String to, String desc) {
        todo = to;
        description = desc;
    }
}

Once you get todo and description (first two lines of your code in the question), you need to create a ListItem object, passing todo and description as parameters to the constructor.

ListItem listItem = new ListItem(todo, description);

Once you have done this, you can add ListItem object to the hashmap.

Map<String, ListItem> dataToSave = new HashMap<>();
dataToSave.put(userId, listItem);

To read todo and description back, you need to do this -

ListItem item = dataToSave.get(userId);   //whatever userid key you want to retrieve data from

System.out.println("UserId: " + userId + ", Todo: " + item.todo + ", Desc: " + item.desc);

At this point, it looks like you do not have much grasp on Objects. I would suggest revisiting the basics in textbooks or read from tons of resources online.

  • Yes, this is actually my first ever project so I'm trying to learn as I go along. I have 2 questions. Firstly, in the last block of code, why do I have to get the value that I need as a ListItem? Why do I have to instantiate another object ListItem again? – codinglearner21 Jul 12 '18 at 07:04
  • 1. why do I have to get the value that I need as a ListItem? - Because your value is a ListItem object. There is no other way to read it. 2. Why do I have to instantiate another object ListItem again? - You are not creating/instantiating a new object again. It is just a reference to the same object. If this is your first project, I would suggest trying something drastically simpler than something that involves HashMap. I would even forget about coding for a bit until you get your theoretical concepts right. –  Jul 12 '18 at 07:20
  • I see, that's very helpful. Then am I correct to say that by pointing back to the value that I need as a ListItem, I would only be able to get the values I need from the "values" (i.e the elements in the ListItem object), and not the string (i.e the key)? – codinglearner21 Jul 12 '18 at 07:30
  • Sorry to be blunt, but your question does not make any sense. Think of HashMap as a collection of locked treasures which is already created. Now, HashMap will let you open only those treasures for which you have a key. So, you should have the key before you can get inside. If I have answered your question, mark the answer as accepted. –  Jul 12 '18 at 08:15