Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:37:16+00:00 2026-06-18T04:37:16+00:00

I have a Map like: Map(product1 -> List(Product1ObjectTypes), product2 -> List(Product2ObjectTypes)) where ProductObjectType has

  • 0

I have a Map like:

Map("product1" -> List(Product1ObjectTypes), "product2" -> List(Product2ObjectTypes))

where ProductObjectType has a field usage. Based on the other field (counter) I have to update all ProductXObjectTypes.

The issue is that this update depends on previous ProductObjectType, and I can’t find a way to get previous item when iterating over mapValues of this map. So basically, to update current usage I need: CurrentProduct1ObjectType.counter - PreviousProduct1ObjectType.counter.

Is there any way to do this?

I started it like:

val reportsWithCalculatedUsage =
  reportsRefined.flatten.flatten.toList.groupBy(_._2.product).mapValues(f)

but I don’t know in mapValues how to access previous list item.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:37:18+00:00Added an answer on June 18, 2026 at 4:37 am

    I’m not sure if I understand completely, but if you want to update the values inside the lists based on their predecessors, this can generally be done with a fold:

    case class Thing(product: String, usage: Int, counter: Int)
    
    val m = Map(
      "product1" -> List(Thing("Fnord", 10, 3), Thing("Meep", 0, 5))
      //... more mappings
    )
    
    //> Map(product1 -> List(Thing(Fnord,10,3), Thing(Meep,0,5)))
    
    m mapValues { list => list.foldLeft(List[Thing]()){
      case (Nil, head) =>
        List(head)
      case (tail, head) =>
        val previous = tail.head
        val current = head copy (usage = head.usage + head.counter - previous.counter)
        current :: tail
    } reverse }
    
    //> Map(product1 -> List(Thing(Fnord,10,3), Thing(Meep,2,5)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that has a list of gps coordinates. I would like
I have a a map that looks like this: public class VerbResult { @JsonProperty("similarVerbs")
I have several complex data structures like Map< A, Set< B > > Set<
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have a map array with objects stuffed with variables looking like this: var
I have got map a lot of ranges to a value like 0-300 =
I have a Map that uses a Set for the key type, like this:
I have something like this: Map<String, String> myMap = ...; for(String key : myMap.keySet())
I have a terrain map which i would like to represent some data in.
I have this class which i would like to map: public class Contract {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.