Last active
August 29, 2015 14:22
-
-
Save asarkar/6daf5f137e1924772d52 to your computer and use it in GitHub Desktop.
Scala Collections Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Employee { | |
private Address addr; | |
private int age; | |
// Getters and setters | |
} | |
class Address { | |
private String streetAddr1; | |
private String streetAddr2; | |
private String city; | |
private int zipCode; | |
private String state; | |
// Getters and setters | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HR { | |
private List<Employee> allEmployees = // HR knows how to find you | |
public static List<Employee> findAllEmployeesLivingInZipCode(int zipCode) { | |
List<Employee> employees = new ArrayList<>(); | |
for (Employee e : allEmployees) { | |
if (e.getAddr().getzipCode() == zipCode) { | |
employees.add(e); | |
} | |
} | |
return employees; | |
} | |
public static int getAverageAge() { | |
long sum = 0; | |
for (Employee e : allEmployees) { | |
sum += e.getAge(); | |
} | |
return sum / allEmployees.size(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HR { | |
List[Employee] allEmployees = // HR knows how to find you | |
def findAllEmployeesLivingInZipCode(zipCode: Int) = { | |
allEmployees.filter(_.addr.zipCode == zipCode) | |
} | |
def getAverageAge = { | |
allEmployees.map(_.age).sum / allEmployees.size | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment