• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help needed on following questions

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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--;
}
}
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic