Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help needed on following questions

 
Jaideep Kshirsagar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic