This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes efficient way to pass values from method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "efficient way to pass values from method. " Watch "efficient way to pass values from method. " New topic
Author

efficient way to pass values from method.

Tanya Sharma
Greenhorn

Joined: Oct 27, 2010
Posts: 10

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.
Thanks
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Without knowing more about your requirements, I think the best approach is to create an object that holds servername and serverip attributes.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
Hi Tanya,

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..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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.
Tanya Sharma
Greenhorn

Joined: Oct 27, 2010
Posts: 10
Thanks everyone

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.

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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.


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Tanya Sharma
Greenhorn

Joined: Oct 27, 2010
Posts: 10
Yes Pete
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

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
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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.


James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

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?

This:


Or this:



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.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

i tend to like dennis' approach. i have written classes like that before.


SCJP
Visit my download page
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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.
Palash Nandi
Ranch Hand

Joined: Jul 09, 2009
Posts: 34
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 )
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Tanya Sharma wrote:Yes Pete
I only need this information (ip and name) and pass it to GUI to display the values .....

(emphasis added).

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: efficient way to pass values from method.
 
Similar Threads
How to determine which is better?
passing value from one class to another class(which extends JFrame)
regarding operators
hashCode() method !
How to determine which is better?