-3

I have a general question about HashMaps vs. ArrayLists.

Ian a project I am working on I am scanning a barcode from a ticket, like entering a ballpark or concert.

As part of this scan there is also static information that the user has pre-selected, like userName, location. I also capture the Date and time of when the barcode is scanned. So I end up with a record consisting of Date/Time, userName,location,barcode scanned.

I need to store this information for 100 to 200 scans, then at the end of the day, convert this to a JSON object.

I am a bit confused on how to store this data. Can someone give me some pointers on this? I have googled it but am still confused.

Thanks much!

  • 2
    What have you tried so far? Please, also read https://stackoverflow.com/help/how-to-ask – luizfzs Jun 30 '17 at 13:20
  • Create an object to wrap the Date/Time, name, location, and barcode. You could store these in an ArrayList, since it doesn't sound like you plan on doing any manipulations to the data. Then you can reference other questions, such as https://stackoverflow.com/questions/4841952/convert-arraylistmycustomclass-to-jsonarray. – Dewick47 Jun 30 '17 at 13:23

1 Answers1

0

I would suggest not solely keeping this records to a hashmap or arraylist in memory. Maybe the phone will turn off or your application will crash. So the user will lose everything stored in memory so far.

What I would do is this. I would create a DTO object with the fields you want eg:

public class MyDTO{
    private String field1;
    private LocalDateTime field2;
    [...]

}

Then each object when created use Gson or whatever you like and store it as json either to a db or a file(s).

At the end of the day read the saved json objects. Combine them to an ArrayList of MyDTOs and use Gson to create an array of json and send them.

After you have successfully submitted the JSON just clean up the db or file(s).

idipous
  • 2,868
  • 3
  • 30
  • 45
  • Thanks for the fast reply. I came to the same conclusion after posting this question. I need permanent storage and since I have 8 pieces of information to store for each data scan I figure I will use SQLite database along with a helper class. Now to go learn that! :) – Donald S Chapman Jun 30 '17 at 14:35