Search code examples
javasortedlistsorting

implementing sorted "watchlist" class in java: what instruments to use?


I need to implement a WatchList class as a part of a Java client-server app. WL is essentially an array of Items, each of which has a timestamp. I am responsible for for the client side of the app. The WL might be updated on the client side manually, i.e. new elements can be added to it. It can also be modified with a regularly schedule update from the server. Similarly, regular uploads are also performed with the terms that had been added manually being sent to the server.

Since I am fairly new to Java, I need advice on what built-in instruments (classes) I should use to implement this WL class. It will obviously be some type of sorted structure with a custom comparator that would compare dates. I will probably also want to maintain it in a newest-items-first order so that I could quickly retrieve the newest items to send to the server. In which case the item that will be received during a download from the server will be added to the beginning of the list rather than at the end. Or keeping it in the newest-items-last order is just as efficient?

Thanks much!


Solution

  • Newest first or newest last, it's a purely functional choice. The comparator of one is just the inverse of the comparator of the other.

    You'll need to understand the classes of the collections framework.

    I wouldn't keep unsaved schedules at client side. If the client crashes, you lose all the unsaved items. Why don't you simply call the server each time an item is created at client-side?