Scala

Scala Snippet: How to filter a list in Scala

In Scala, filtering and processing collections is easy and elegant. There are many filtermethods available, but the most used will probably the basic filter method.

Here’s a code example of some filtering on my (ex)camera collection.
The filter method will not only work on Lists, but on any Scala collection.

object MyCameraCollection02 {

  class Camera(_brand: String, _model: String, _sensorType: String, _yearBought: Int) {
    val brand: String = _brand
    val model: String = _model
    val yearBought = _yearBought
    val sensorType = _sensorType

    override def toString(): String = {
      return "%s %s \t\t %s \t(%d)" format(brand, model, sensorType, yearBought)
    }

  }

  def main(args: Array[String]) {

    val canon5dmarkIII = new Camera("Canon", "5D MkIII", "FF", 2013)
    val canon5dmarkII = new Camera("Canon", "5D MkII", "FF", 2009)
    val canon6d = new Camera("Canon", "6D", "FF", 2014)
    val canon550d = new Camera("Canon", "550D", "APS-C", 2010)
    val canon40d = new Camera("Canon", "40D", "APS-C", 2008)
    val canonIXUS330 = new Camera("Canon", "IXUS 330", "1/2.7", 2001)
    val canonIXUSZ90 = new Camera("Canon", "IXUS Z90", "APS-C", 1999)
    val panasonicGM1 = new Camera("Panasonic", "GM1", "M43", 2014)
    val panasonicFZ20 = new Camera("Panasonic", "FZ20", "1/2.5", 2005)
    val sonyrx100 = new Camera("Sony", "DSC-RX100", "1\"", 2013)
    val sonynex5 = new Camera("Sony", "NEX-5", "APS-C", 2011)
    val sonyr1 = new Camera("Sony", "DSC-R1", "APS-C", 2005)

    val myCameras = List(canon5dmarkIII, canon5dmarkII, canon6d, canon550d, canon40d, canonIXUS330, canonIXUSZ90, panasonicGM1, panasonicFZ20, sonyrx100, sonynex5, sonyr1)
    val canonCameras = myCameras.filter(x => x.brand == "Canon") // Every Canon camera I ever owned
    val sonyCameras = myCameras.filter(x => x.brand == "Sony") // Every Sony camera I ever owned
    val pansonicCameras = myCameras.filter(x => x.brand == "Panasonic") // Every Panasonic camera I ever owned


	// apscCamera's only 
    val apscCameras = myCameras.filter(cam => cam.sensorType == "APS-C")
    println("==APS-C camera's owned==")
    apscCameras foreach (cam => println(cam))
    println


	// Canon camera's which are not full frame. You can filter, filtered lists.
    val canonNonFF = myCameras.filter(x => x.brand == "Canon").filter(x => x.sensorType != "FF")
    println("==Non-FF camera's owned==")
    canonNonFF foreach (cam => println(cam))
    println
	
	// Filter by boolean expressions on class properties
    val apsBefore2012 = apscCameras.filter(cam => cam.yearBought < 2012)
    println("==APS-C camera's bought before 2012 owned==")
    apsBefore2012.foreach(cam => println(cam))
    println

	// Filter by combining boolean expressions.
    val ffcamerasBefore2012 = myCameras.filter(cam => cam.yearBought < 2012 && cam.sensorType == "FF")
    println("==Every FF Camera I ever owned before 2012==")
    ffcamerasBefore2012.foreach(cam => println(cam))
    println
  }

}

Running this example will give you the following result:

==APS-C camera's owned==
Canon 550D 		 APS-C 	(2010)
Canon 40D 		 APS-C 	(2008)
Canon IXUS Z90 		 APS-C 	(1999)
Sony NEX-5 		 APS-C 	(2011)
Sony DSC-R1 		 APS-C 	(2005)

==Non-FF camera's owned==
Canon 550D 		 APS-C 	(2010)
Canon 40D 		 APS-C 	(2008)
Canon IXUS 330 		 1/2.7 	(2001)
Canon IXUS Z90 		 APS-C 	(1999)

==APS-C camera's bought before 2012 owned==
Canon 550D 		 APS-C 	(2010)
Canon 40D 		 APS-C 	(2008)
Canon IXUS Z90 		 APS-C 	(1999)
Sony NEX-5 		 APS-C 	(2011)
Sony DSC-R1 		 APS-C 	(2005)

==Every FF Camera I ever owned before 2012==
Canon 5D MkII 		 FF 	(2009)
Reference: Scala Snippet: How to filter a list in Scala from our JCG partner Arthur Arts at the JDriven blog.

Arthur Arts

Arthur Arts is an experienced Enterprise Java software engineer and hands-on Agile Coach and Scrum Master. He has a strong focus on agile engineering principles and practices and loves to share his knowledge and learn from others. He has his own company Coded Value, is a passionate photographer and writes for agilearts.nl.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Lukas
Lukas
9 years ago

why your code is so ugly? :(

http://pastebin.com/Xzcbekz4

Back to top button