jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Help needed on following questions 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 "Help needed on following questions" Watch "Help needed on following questions" New topic
Author

Help needed on following questions

Jaideep Kshirsagar
Greenhorn

Joined: Aug 29, 2007
Posts: 27
How you will go about writing a function to extract an object from the list (collection) on the basis of one of the attributes in the object? (The parameter passed to this function will be one of the attributes of the object and return type will be the object).
How you will write a function for reversing the character array? If you are using two arrays for this function, how you will go about optimizing the code and memory requirement by using only one array?
Can there be multiple same keys in hash map? How to sort and search on the basis of keys in hash map? What is hash code and how it is used?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Originally posted by Jaideep Kshirsagar:
How you will write a function for reversing the character array? If you are using two arrays for this function, how you will go about optimizing the code and memory requirement by using only one array?


Can there be multiple same keys in hash map? How to sort and search on the basis of keys in hash map? What is hash code and how it is used?

The first line in the API documentation of java.util.Map:
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

This is the same with maps in all languages I know. I mean, what's the use of a map when you can't get the value for a key? If you have multiple values for one key, which one do you return?

Should you need to have multiple values for a key, use a collection (e.g. List) as the value.
[ September 21, 2007: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
Here is the answer of IInd question, reversing char array

public static void reverse(char[] c)
{
int z = c.length - 1;
for(int i=0;i<(c.length)/2;i++)
{
char t = c[i];
c[i] = c[z];
c[z] = t;
z--;
}
}
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Originally posted by Rob Prime:

This is the same with maps in all languages I know. I mean, what's the use of a map when you can't get the value for a key? If you have multiple values for one key, which one do you return?

[ September 21, 2007: Message edited by: Rob Prime ]


In fact, MultiMaps do exist, but are not very common and not a standard in Java
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

But it also says
A MultiMap is a Map with slightly different semantics. Putting a value into the map will add the value to a Collection at that key. Getting a value will return a Collection, holding all the values put to that key.

So technically it's a special type of map, where there is still one value for each key. That value happens to be a collection of other values.
Jaideep Kshirsagar
Greenhorn

Joined: Aug 29, 2007
Posts: 27
Thanks for prompt responses, that was of real help.
Here is an example, just to explain first question
How you will go about writing a function to extract an object from the list (collection) on the basis of one of the attributes in the object? (The parameter passed to this function will be one of the attributes of the object and return type will be the object).
in more detail -
Suppose there is a list containing employee objects. The employee object contains employee id and employee name as attributes. Then how to write a function which takes employee id as an argument and returns the respective employee object?
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Originally posted by Jaideep Kshirsagar:
How you will go about writing a function to extract an object from the list (collection) on the basis of one of the attributes in the object? (The parameter passed to this function will be one of the attributes of the object and return type will be the object).

You can use reflection to extract the object. Something like below:


You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Congratulations, you all just helped this fellow cheat on his homework.

Kids, we don't do people's work for them. Teach them, instruct them, give them hints -- but make them show that they've tried to write the code themselves first, please.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed on following questions
 
Similar Threads
HashMap collisions question
Hashtable sort by key
K&B doubt in hashcode() and equals(): Is my understanding correct ?
whats a hash map???
Database Basics