В Scala фильтровать и обрабатывать коллекции легко и элегантно. Существует много доступных методов фильтрации, но наиболее часто используемым будет, вероятно, основной метод фильтрации.
Вот пример кода для фильтрации моей (бывшей) коллекции камер.
Метод filter будет работать не только со списками, но и с любой коллекцией Scala.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
object MyCameraCollection 02 { 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 canon 5 dmarkIII = new Camera( "Canon" , "5D MkIII" , "FF" , 2013 ) val canon 5 dmarkII = new Camera( "Canon" , "5D MkII" , "FF" , 2009 ) val canon 6 d = new Camera( "Canon" , "6D" , "FF" , 2014 ) val canon 550 d = new Camera( "Canon" , "550D" , "APS-C" , 2010 ) val canon 40 d = new Camera( "Canon" , "40D" , "APS-C" , 2008 ) val canonIXUS 330 = new Camera( "Canon" , "IXUS 330" , "1/2.7" , 2001 ) val canonIXUSZ 90 = new Camera( "Canon" , "IXUS Z90" , "APS-C" , 1999 ) val panasonicGM 1 = new Camera( "Panasonic" , "GM1" , "M43" , 2014 ) val panasonicFZ 20 = new Camera( "Panasonic" , "FZ20" , "1/2.5" , 2005 ) val sonyrx 100 = new Camera( "Sony" , "DSC-RX100" , "1\"" , 2013 ) val sonynex 5 = new Camera( "Sony" , "NEX-5" , "APS-C" , 2011 ) val sonyr 1 = new Camera( "Sony" , "DSC-R1" , "APS-C" , 2005 ) val myCameras = List(canon 5 dmarkIII, canon 5 dmarkII, canon 6 d, canon 550 d, canon 40 d, canonIXUS 330 , canonIXUSZ 90 , panasonicGM 1 , panasonicFZ 20 , sonyrx 100 , sonynex 5 , sonyr 1 ) 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 apsBefore 2012 = apscCameras.filter(cam = > cam.yearBought < 2012 ) println( "==APS-C camera's bought before 2012 owned==" ) apsBefore 2012 .foreach(cam = > println(cam)) println // Filter by combining boolean expressions. val ffcamerasBefore 2012 = myCameras.filter(cam = > cam.yearBought < 2012 && cam.sensorType == "FF" ) println( "==Every FF Camera I ever owned before 2012==" ) ffcamerasBefore 2012 .foreach(cam = > println(cam)) println } } |
Запуск этого примера даст вам следующий результат:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
== 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 550 D APS-C ( 2010 ) Canon 40 D APS-C ( 2008 ) Canon IXUS 330 1 / 2.7 ( 2001 ) Canon IXUS Z 90 APS-C ( 1999 ) == APS-C camera's bought before 2012 owned == Canon 550 D APS-C ( 2010 ) Canon 40 D APS-C ( 2008 ) Canon IXUS Z 90 APS-C ( 1999 ) Sony NEX- 5 APS-C ( 2011 ) Sony DSC-R 1 APS-C ( 2005 ) == Every FF Camera I ever owned before 2012 == Canon 5 D MkII FF ( 2009 ) |
Ссылка: | Scala Snippet: Как отфильтровать список в Scala от нашего партнера JCG Артура Артса в блоге JDriven . |