0

I have been asked to 'translate' some Scala code to Java for a course. However, the requirements of the assignment are that Java 8 and external libraries, such as Functional Java and Totally Lazy, are not allowed. The line in Scala is:

charges.groupBy(_.cc).values.map(_.reduce(_ combine _)).toList

I have been able to write groupBy and values but .map and _.reduce still elude me. I have looked at the source code of those two libraries as well as the Scala source to try and find something to help me with putting these together but I have not been able to make any headway.

GroupBy is implemented as follows:

public Map<CreditCard, List<Charge>> groupBy(List<Charge> list)
{
    Map<CreditCard, List<Charge>> map = new TreeMap<CreditCard, List<Charge>>();
    for(Charge c: list)
    {
        List<Charge> group = map.get(c.cc);
        if(group == null)
        {
            group = new ArrayList();
            map.put(c.cc, group);
        }
        group.add(c);
    }
    return map;
}
3
  • 1
    I'm a bit baffled that you're able to implement groupBy and values but struggling at map. What's the eluding part? Commented Feb 3, 2016 at 16:00
  • Values might not have been implemented correctly, since I used map.values() for it, but it gives correct results in this example. As for map...the eluding part is the fact of passing in functions as arguments and that type of thing. I wanted to find a map() implementation in Java using JDK7 or lower but didn't find a generic implementation. Commented Feb 3, 2016 at 16:18
  • 1
    groupBy has a function passed in to it too. You've just ignored that and used c.cc directly. If that's acceptable for groupBythen presumably you can do the same fpr map Commented Feb 3, 2016 at 16:25

2 Answers 2

1

you can use Google Guava for this. it doesn't require java8. you would especially be interested in class which call FluentIterable. here's some methods that could help you:

  • index(Function keyFunction) - the function used to produce the key for each value
  • transform(Function function) - applies {@code function} to each element of this fluent iterable

and there are a lot more.

Sign up to request clarification or add additional context in comments.

Comments

0

You'll have to iterate over the values. Ordinarily I'd suggest using a new style for loop. Something like:

for (ValuesType value : values) {
    // do your map and reduce here
}

The problem with that is you need to have access to more than one value at a time. (See discussion of .reduce(), below.) So perhaps the old style for would be better:

for (int i = 0; i < values.length - 1; i++) {
    // do something with values.get(i) or values[i] as needed
}

Point to ponder: why values.length - 1?

.map() simply transforms one thing into another. What's the transformation in this case? It's the .reduce()! So that should be easy enough.

The _ in _.reduce() is the equivalent of value in the for statement above. It's the one value that you're dealing with on this iteration.

.reduce() takes two values and does something to them to turn them into a single value. To make that work you'll need to deal with values.get(i) and values.get(i+1) somehow. And _ combine _, well, you tell me.

4 Comments

Thanks! The combine method is not a problem, but I don't really understand what the _ combine _ means as far as the underscores. It's supposed to be array[i].combine(array[i+1]), however this has not worked in practice.
_combine _ is the same as (x,y)=>x.combine(y) So that combine method has to come from somewhere, it's not in the scala standard library. So I hope the assignment defines that somewhere. What's the definition of Charge? (I'll bet combine turns out basically to be + since the intent of the code seems to be to summarise charges by credit card...)
I wrote the combine method and it works. Now it's just getting everything in Java to work properly, which seems to be literally impossible.
@m482 Difficult, sure. But Scala is built on the JVM, so it has to be possible. (Not exactly sound reasoning, but meant to be encouraging at least.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.