简体   繁体   中英

Scala comparison error

I am trying to compare an item from a List of type Strings to an integer. I tried doing this but I get an error saying that:

'value < is not a member of List[Int]'

The line of code that compares is something similar to this:

if(csvList.map(x => x(0).toInt) < someInteger)

Besides the point of why this happens, I wondered why I didn't get an error when I used a different type of comparison, such as ' == '.

So if I run the line:

if( csvList.map(x => x(0).toInt) == someInteger)

I don't get an error. Why is that?

You can refer to Why == operator and equals() behave differently for values of AnyVal in Scala

Every class support operator ==, but may not support <,> these operators.

in your code

csvList.map(x => x(0).toInt)

it returns a List<int> , and application use it to compare with a int, so it may process a implicit type conversion. Even the compiler doesn't report it as a error. Generally, it's not good to compare value with different types.

csvList.map(x => x(0).toInt) converts the entire csvList to a List[Int] and then tries to apply the operator < to List[Int] and someInteger which does not exist. This is essentially what the error message is saying.

There is no error for == since this operator exists for List though List[T] == Int will always return false.

Perhaps what you are trying to do is compare each item of the List to an Int . If that is the case, something like this would do:

scala> List("1","2","3").map(x => x.toInt < 2)
res18: List[Boolean] = List(true, false, false)

The piece of code csvList.map(x => x(0).toInt) actually returns a List[Int] , that is not comparable with a integer (not sure what it would mean to say that List(1,2) < 3 ). If you want to compare each element of the list to your number, making sure they are all inferior to it, you would actually write if(csvList.map(x => x.toInt).forall { _ < someInteger })

Let's start with some introductions before answering the questions Using the REPL you can understand a bit more what you are doing

scala> List("1", "2", "3", "33").map(x => x(0).toInt)
res1: List[Int] = List(49, 50, 51, 51)

The map function is used to transform every element, so x inside the map will be "1" the first time, "2" the second, and so on.

When you are using x(0) you are accessing the first character in the String.

scala> "Hello"(0)
res2: Char = H

As you see the type after you have mapped your strings is a List of Int. And you can compare that with an Int, but it will never be equals.

scala> List(1, 2, 3) == 5
res0: Boolean = false

This is very much like in Java when you try

"Hello".equals(new Integer(1));

If you want to know more about the reasons behind the equality problem you can check out Why has Scala no type-safe equals method?

Last but not least, you get an error when using less than because there is no less than in the List class.

Extra: If you want to know if the second element in the list is smaller than 2 you can do

scala> val data = List("1", "10", "20")
data: List[String] = List(1, 10, 20)
scala> 5 < data(1).toInt
res2: Boolean = true

Although it is a bit strange, maybe you should transform the list of string is something a bit more typed like a case class and then do your business logic with a more clear data model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM