• 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

Adding Map<String, POS> to List<Map<String,POS>

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not able to add new map objects to the ArrayList. It simply repeats the first object over and over. Please help determine why it is not updating the List with a new map object

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the whole code.
Your return return taggedSentenceList; But I do not see where you are initializing taggedSentenceList.


 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
where is taggedSentenceList initialising.I can see it is not inside in any loop.so it will have only the one object .Please also post the caller code.
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'm initializing it in the class scope variable

 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whole code:

 
suraj august
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems a loop problem.You are adding map from outside the loop.
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I checked that possibility. Also check the two values prior to adding to the map and they are indeed correct. For some reason, the new maps are not created. It repeats the first map created and put it in there 15 times
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron Ingram wrote:Thank you. I'm initializing it in the class scope variable

That isn't the right way to do it. You are creating an object of a raw type, so you may lose type‑safety. It should read something like this:-
List<LinkedHashMap<String, POS>> taggedSentenceList = new ArrayList<>();
I thought they were going to prohibit raw types in Java6, but that particular enhancement never got off the ground. Another problem you have with generics is that in line 98 you are casting what is got from the Map. The whole idea of generics was to obviate casting altogether.

In lines 109‑110 you are adding map to the List. You seem to be adding the same Map several times.
Your type names are poorly chosen. You have Pos POS and pos in your code; that is a certain recipe for confusion.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I was mistaken; you are creating a new Map inside the loop. Sorry.
Your methods are unnecessarily long and there are some problems with what they return. The first method returns null, and that is usually a dangerous mistake.
The method which returns the List does not do one thing. It does several. A method should do one thing. For example populating the List. Or returning it. But your method does both, so every time you call it you duplicate the information inside the List and the List gets bigger. Since the memory footprint of a few additional Map references is small, you won't get any out of memory problems. But you will get the duplication problem you describe.
You are also returning mutable objects from your methods. That is potentially dangerous, if any other code modifies those objects. Find out about the unmodifiableXXX methods of the Collections class which can use to return the Lists and Maps in a read‑only form.
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I appreciate the feedback and agree. Actually, I did have most of the code broken out as you mention, but after hours of troubleshooting, its sort of mutated itself in what you see. I will clean it up. I'm still not sure why it is not producting expected results in the loop.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron Ingram wrote: . . . but after hours of troubleshooting, its sort of mutated itself . . .

You need to crack the whip over your code and remind it who's boss!
It is usually inefficient to do that sort of thing; guessing about code is more likely to introduce errors than to correct them.
 
Ron Ingram
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
haha.. will do!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could we have a look at lines 91 to 104?



The comment says "load into pennTreeConversionMap" but the code doesn't do that. So that makes me suspect there's been some copy-pasting going on without sufficient fixing up.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic