I have a method which querys the database and returns more than one value. It returns servername and serverip. I need to pass this combination back to the user interface (which is another class) and display the result.
One option is hashmap, but here there is a possiblity that name value pair will be repeated, yes even the ip can be repeated. So I dnt think I can use that.
Other option I can think of is concatenating the two and passing back an arraylist.
I want to know what is the efficient way of achieving the same. Could someone guide me on the same please.
I think the best method depends on your requirements.
Sol1: Just like you said,I think Array list in which you concatenate the serverName:ServerIP would be a good choice. pro: Simple object structure con: You have to do many string concatenation and splits, For a huge resultset that will take time and consume memory.
Sol2: Use solution 1 , but optimize your query by getting distinct results and maybe even string concatenation.
Sol3: Use an object as as Dennis mentioned, with two fields and return an array list of this object. But I think that might not be so efficient as you are using one extra reference for the Object.
The best according to me would be Sol2 then sol1 and then sol3 . There may be more. Keep thinking .
Note: Sol3 actually is quite good, if there is a possibility of adding more fields or more values from the Database later on, if and when more requirements come. Hence more maintainable
A TubeBulb May light slowely... But it lights the Brightest..
I'd suggest creating a class containing fields for whatever needs returning (name, IP address, anything else relevant), and then just return one of those. That way the meaning of what is returned is much clearer, rather than having to remember what the first/second/etc entries are for.
Joined: Oct 27, 2010
I am looking for efficient solution in terms of memory and time.
So using array list and concatenating it is probably not the best solution as it will take more memory and time if there is a lot of data involved.
Using an object seems to be a decent solution but still its not the best one.
Anyone can come up with something else or suggest me which one to use.
Thanks a lot Dennis, Palesh and Mathew for your explanation.
One good method of determining if you really need an Object (versus a simple formatted string), is to think about what the Object would be responsible for. It sounds like your Object would at least need to have a getServerIP() and getServerName() method. Does it need to compare itself to other similar objects? Does it need to do any sort of CIDR address matching?
If you think of all the responsibilities, and determine it only needs to do getServerIP() and getServerName(), a formatted string is probably a good solution following the KISS principal, rather than creating a special Object. You might offend some OO purists, but if it works, and is easy to maintain, why worry.
As far as consuming memory, whether you have an array of many, many Strings, or an array of many, many Objects, I would think the difference would be practically trivial. Unless you have billions of these strings (or have millions of users simultaneously looking at millions of strings), I wouldn't worry excessively about this. If you're concerned, use a memory profiler to test and see how much memory your ArrayList is consuming.
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Joined: Oct 27, 2010
I only need this information (ip and name) and pass it to GUI to display the values. So if I create an object, it will just have get and set methods and not other operations.
So may be creating an array list of tokenized strings is an option.
Tokenized strings? You're proposing to take your data and restructure it just so you don't have to spend half an hour designing a proper class to contain the data? And then make the code which receives the data have to un-restructure it? Don't do that. Create a proper class to hold the data.
Tanya Sharma wrote:I am looking for efficient solution in terms of memory and time.
Then stop doing that. You're just passing a couple of strings. No matter how you pass them, the memory and time required to do that will be ridiculously trivial. Don't waste your time trying to optimize such a tiny thing, until it becomes necessary to optimize it. Which it almost certainly won't.
Tanya Sharma wrote:I am looking for efficient solution in terms of memory and time...
The only time you should be looking for an "efficient" solution is when you can prove that the one you've come up with is unacceptably slow. Since you don't seem to have any solution yet, my advice would be to write the simplest one you can think of.
I refer you to my quote below.
Isn't it funny how there's always time and money enough to do it WRONG?
My neighbour at work has just spent quite a while fixing bugs in string encoding/decoding, because the seperator char is used in someones data.
I would never recommend passing data as a concat string, a simple DTO class takes no time to make in memory, and is far easier to extend
Wendy Gibbons wrote:My neighbour at work has just spent quite a while fixing bugs in string encoding/decoding, because the separator char is used in someones data.
Not a lot of risk of that here, if we know we'll find a valid host name and valid IP address.
If all that's being done here is to pass the information back to another class to display, what could be more simple and efficient than sending it in the same format it will use to be displayed? Start with the most simple code that fulfills the requirements, and only add complexity if needed.
Forget using arrays, lists or concatenating strings, an object holding the 2 items of data is the only way to go IMO. What you are effectively writing is a Data Transfer Object (DTO)
Think about it from a calling party perspective. What do you think they would rather do?
The second snippet doesn't need any comments to explain what the code is doing.
As someone else has already said, if requirements change and more data needs to be returned, you simply change the ServerData class. This is as opposed to adding the item to the list and then updating docs to tell clients that new item is at position 2 in returned list.
What about java.net.InetAddress? Use the static InetAddress.getByAddress(String host, byte addr) to create an InetAddress from your data (without any DNS resolution). If you need to use a class, might as well use one that's already there meant to hold your data.
Joined: Jul 09, 2009
well i guess, the problem is so simple , it would be diffficult to come up with any further strategies than the three mentioned ( Using Map, String concat List , Using object ).
And as always , If maintaining code is an issue, which it mostly is for Enterprise Java codes, going with an object is the way to go ( in case you sympathize with the one who is going to maintain it. :P )
Tanya Sharma wrote:Yes Pete
I only need this information (ip and name) and pass it to GUI to display the values .....
I'd understand the wish to optimize things (though it is premature optimization anyway) had you said that you're going to process millions of such record. But given that you're going to display them in GUI, performance should be the least of your concern. Displaying so many records in GUI as to create performance problems you've described would imply your poor users browsing through endless lists of records without any real chance to find out the ones they're looking for.