Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: GROOVY Script: List / Map Collection Problem

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GROOVY Script: List / Map Collection Problem

    Firstly, I'm new to Groovy so apologies in advance for any school boy errors in the below.

    I've created a script which I'm using to simulate the behaviour of a SOAP service in SOAP UI (as a mock service) for sprint testing purposes but am having problems when trying to iterate over a List I've created. The List is made up of a number of Maps, and each Map contains a List. It should look something like this:

    [[Rec: 1, Items: ["AB123","BC234","CD345"]],[Rec: 2, Items: ["AB123","BC234","CD345","DE456"]]]

    And this is the code I have to build up the List:

    def offerMap      = [:] 
    def outputList    = [] 
    def offerItemList = [] 
     
    def outputMap     = [:] 
    def outList       = [] 
     
    def item          = "" 
    def rec           = "" 
     
    offerItemList.add("AB123") 
    offerItemList.add("BC234") 
    offerItemList.add("CD345") 
     
    offerMap.put("Rec",1) 
    offerMap.put("Items",offerItemList) 
     
    outputList.add(offerMap) 
     
    log.info "OUT: outputList.size ${outputList.size()}" 
    log.info "OUT: offerItemList.size ${offerItemList.size()}" 
     
    offerMap.clear() 
    offerItemList.clear() 
     
    offerItemList.add("AB123") 
    offerItemList.add("BC234") 
    offerItemList.add("CD345") 
    offerItemList.add("DE456") 
     
    offerMap.put("Rec",2) 
    offerMap.put("Items",offerItemList) 
     
    outputList.add(offerMap) 
     
    log.info "OUT: outputList.size ${outputList.size()}" 
    log.info "OUT: offerItemList.size ${offerItemList.size()}"

    And this is the the code I have to iterate over the list:

    outputList.each { 
     
        log.info "OUT: outputList.size ${outputList.size()}" 
     
        outputMap.clear() 
        outputMap = it 
     
        row = outputMap.get("Rec") 
        log.info "OUT: REC ${rec}" 
     
        outList.clear() 
        outList = outputMap.get("Items") 
     
        outList.each { 
     
            item = it 
            log.info "OUT: Item ${item}" 
     
        } 
    }

    But this is not giving me the results I expect, first problem is that the .each loop for outputList appears to immediately be jumping to the second entry in the list, as witnessed from the output:

    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 1 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 3 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 4 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC 2 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item AB123 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item BC234 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item CD345 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item DE456 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 
    Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC null

    I'm running out of ideas and fear I may be missing something fundamental due to my lack of experience with Groovy.

    If you can put me right, I'd be most grateful.

    TIA
    Last edited by ColRiley; November 4th, 2017 at 03:39 AM.

Similar Threads

  1. Problem with Collection sort
    By bhk in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 30th, 2013, 09:33 AM
  2. collection problem
    By freakycoder in forum Collections and Generics
    Replies: 13
    Last Post: June 26th, 2012, 11:05 AM
  3. Collection.sort problem
    By Minken in forum What's Wrong With My Code?
    Replies: 12
    Last Post: September 21st, 2010, 06:24 PM
  4. Bulk operations on sets/ map problem
    By kyuss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 14th, 2010, 12:48 PM
  5. Problem with a waitFor() executing a script
    By Baldurian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 29th, 2010, 10:45 AM

Tags for this Thread