Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

map with string and object

Hi,

I am looking at below link but not very clear

stackoverflow.com/questions/6870973/want-to-create-a-mapstring-object-object-can-be-string-and-can-be-class-obj

CAn you please provide some background why and how to declare or use above map.

can yo please provide complete example on this.

please advise
Any links resources ideas highly appreciated. Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One of the comments on that question says this:

It is not quite clear what you want to achieve.

The asker never clarified. The commenter's point is still valid. What are you trying to use this map for? If you don't have a specific use case, and are just confused about that StackOverflow question, my advice is to forget about it, because, as dpearson says above, it's not a very useful data structure.
Avatar of gudii9

ASKER

I am creating objects ObjectDef and FieldDef

Then I am creating an example input file ObjectDefs.txt

Then i will create initialization method that reads that files and creates

a map <className, ObjectDefinition>

so that i can look up after that just by the className to get the ObjectDef for it using reflection.

Then I parse the text file and send to one other application/system.

So i am bit not clear how to achieve this in this context. Any good working example around this. Please advise.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpearson
dpearson

I think CPColin is right it's

Map<String, ObjectDefinition> where String is className

Then you may also want

Map<ObjectDefinition, FieldDef>

if you are keeping a set of fields per object.

So it's two maps, not a single map containing two types of values.

Doug
Avatar of gudii9

ASKER

These are kind of real time complex scenarios i come across in the projects. But java doc which always give simple arraylist, hash map etc etc examples.

Where i can i find these kind of real time complex project scenarios, examples to practice, to understand better, faster?
Some times for me understanding the requirement is bit challenging and then next step development which is some what manageable with some help?

please advise
I don't think there are any real shortcuts.  I think you're doing the right thing, reading a lot, asking a lot of questions, looking at real code.  Not sure what else you can do to learn this stuff.

Doug
Avatar of gudii9

ASKER

I think you're doing the right thing, reading a lot, asking a lot of questions, looking at real code.  Not sure what else you can do to learn this stuff.

I am learing definitely but not as fast as my new projects want me to. Sometimes they expect me to be be rockstar with the code within no time that is where i need to master these complex collection related things with object holding other object and having different kinds of keys and how to undertasnad write etc. please advise
Avatar of gudii9

ASKER

Map<String, ObjectDefinition> where String is className

Then you may also want

Map<ObjectDefinition, FieldDef>

if you are keeping a set of fields per object.

So it's two maps, not a single map containing two types of values.


any compelte code example to explain this concept. Please advise
I am creating objects ObjectDef and FieldDef

Then I am creating an example input file ObjectDefs.txt

Then i will create initialization method that reads that files and creates...

How about you post your code first and then we suggest how to modify it, rather than us writing a whole app for you?
Avatar of gudii9

ASKER

Map<String, ObjectDefinition> where String is className

Then you may also want

Map<ObjectDefinition, FieldDef>

if you are keeping a set of fields per object.

So it's two maps, not a single map containing two types of values.

It is making more sense now.

Looks like we are using one Map as below
Map<String, ObjectDefinition> where String is className

Then on LinkedList as below to read line by line different kind of text files like StockDefinition.txt, GeneralRecordDefinition.txt, TradeApplicationDefinition.txt, ConsumerDefinition.txt etc
List<FieldDef> FieldDefList = new LinkedList<FieldDef>();
not 2nd map as below
Map<ObjectDefinition, FieldDef>

Each line is as below
600,900,Stocksegment,List,Trade,1
900,902,NN_DATE_PPPP_MM,Primitive,String
(where Index start point, end point index,size,name of the field,fieldPrimitive that is defined,Class Name,optional list Count

I wonder what is advantage or practical use of LinkedList compared to map here. Please advise.
It looks like they are using a list of fields because the fields have no names.

If you're reading a line like this:

600,900,Stocksegment,List,Trade,1

then you're relying on the position in the list to give it meaning.  So a LinkedList is a reasonable way to represent this.  Element 2 in the list is the "end index" because of its position.

If you had XML or JSON then you'd have a name to go with the value:

<myline start="600" end="900" name="Stocksegment" ...>

and a map would be a more natural representation where the name of the field would be the key in the map.

Hope that all makes sense.

Doug
Avatar of gudii9

ASKER

then you're relying on the position in the list to give it meaning.  So a LinkedList is a reasonable way to represent this


Can you please elaborate on this. I am not clear on this point
If I give you two sets of data:

500, 800, IBM

and

start="500", end="800", company="IBM"

you can see that in some sense they both represent the same data.

But to know that "500" is the start price in the first set, you need to know that the start price comes first in the list.  It relies on the order of that list.

In the second one, the order doesn't matter.
end="800", start="500", company="IBM" is just fine - there's no confusion

So when the order doesn't matter and you have the names, a map makes sense:
<start="500", end="800", company="IBM"/>   <-- use a map

But when the order does matter and you have no names then a list makes sense:
500, 800, IBM    <-- use a list

Is that clear now?

Doug
Avatar of gudii9

ASKER

Map<String, Object> map = new HashMap<String, Object>();
...
for (Map.Entry<String, Object> entry : map.entrySet()) {
    if (entry.getValue() instanceof String) {
       String value = (String)entry.getValue() ;
        // Do something with entry.getKey() String value
    } else if (entry.getValue() instanceof List) {

for (Map.Entry<String, Object> entry : map.entrySet()) {

what is meaning of above statement. What does map.entrySet() do
What does map.entrySet() do

If you're not familiar with how to look up Javadocs for library methods it's good to learn.  In any case here's the documentation:
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()

In simple terms it just returns a set of "Entry" objects, which Entry has both the key and the value from the map.  So for a map like:

"A"->10
"B"->20
"C"->100

The entry set would just be a list of pairs like this:

("A",10), ("B",20), ("C",100)

Doug
Avatar of gudii9

ASKER

if (entry.getValue() instanceof String) {


there should be similar method entry.getKey() to return key similar to entry.getValue() to return value right. please advise
Avatar of gudii9

ASKER

entrySet
Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
Returns:
a set view of the mappings contained in this map

In above explanation why are they talking about Set and Map and entrySet and what is meaning of line
Set<Map.Entry<K,V>> entrySet() esp >> please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER


Then the method returns a Set of those entries - which is just a way to say a collection or list or entries.

So in simpler terms this is the same as:

Collection<Entry> entrySet()


when you say Set is it is a collection Set?
or just bunch of those entries.

Is there is a simple example to understand this? please advise
A Set is a specific type of Collection.
A List is another Collection.

A "set" means:
1) The elements have no particular ordering
2) The elements have no duplicates
E.g. a set of colors ("red", "blue", "green")

A "list" means:
1) The elements have a particular ordering
2) The elements can have duplicates
E.g. a list of prime numbers ("2","3","5","7","11")

A collection just means "a group of things" - which is why both sets and lists are collections.

In particular in Java, when you have a collection you can walk through the elements, like this:

Collection<Integer> myCollection ;

// Go through each in turn
for (Integer value : myCollection) {
    System.out.println(value) ;
}

Doug