{"id":26060488,"url":"https://github.com/saravanan81java/java-stream-api-samples","last_synced_at":"2025-12-24T04:28:47.328Z","repository":{"id":278841352,"uuid":"936937014","full_name":"saravanan81java/java-stream-api-samples","owner":"saravanan81java","description":"Java Stream API Examples","archived":false,"fork":false,"pushed_at":"2025-02-22T02:08:09.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T02:29:42.540Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/saravanan81java.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-22T00:48:22.000Z","updated_at":"2025-02-22T02:08:12.000Z","dependencies_parsed_at":"2025-02-22T02:39:49.440Z","dependency_job_id":null,"html_url":"https://github.com/saravanan81java/java-stream-api-samples","commit_stats":null,"previous_names":["saravanan81java/java-stream-api-samples"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanan81java%2Fjava-stream-api-samples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanan81java%2Fjava-stream-api-samples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanan81java%2Fjava-stream-api-samples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saravanan81java%2Fjava-stream-api-samples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saravanan81java","download_url":"https://codeload.github.com/saravanan81java/java-stream-api-samples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242559216,"owners_count":20149326,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-08T14:08:28.595Z","updated_at":"2025-12-24T04:28:47.282Z","avatar_url":"https://github.com/saravanan81java.png","language":"Java","readme":"# java-stream-api-samples\n\n**Java Stream API Examples** - How to convert List of objects to another List of objects using Java streams?\nIn this section, we will show you how to convert a List of objects to another List of objects in Java using the Java streams map(). The ‘map’ method maps each element to its corresponding result.\n\n## Java Stream API \nThe Java Stream API provides a functional programming approach over the object oriented capabilities of Java. It is used for processing collections of objects. The Stream in Java can be defined as a sequence of elements from a source List, Set or Array. Most of the stream operations return a Stream. This avails engender a chain of stream operations(stream pipe-lining). The streams withal support the aggregate or terminal operations on the elements. for example, finding the sum or maximum element or finding the average etc...Stream operations can either be executed sequentially or parallel. when performed parallelly, it is called a parallel stream. \n\n## Stream map() Method \nThe Java 8 Stream map() is an intermediate operation. It converts Stream\u003cobj1\u003e to Stream\u003cobj2\u003e. For each object of type obj1, a new object of type obj2 is created and put in the new Stream. The map() operation takes a Function, which is called for each value in the input stream and produces one result value, which is sent to the output stream. The stream map method takes Function as an argument that is a functional interface.\n\n## Example 1- Convert List of Employee to List of EmployeeDTO\n\n\u003cdetails\u003e\n\u003csummary\u003eCode Section\u003c/summary\u003e\n\n## Employee.java \n\npackage com.sample.entity;\npublic class Employee {\n    private int id;\n    private String name;\n    private String email;\n    private String phone;\n    private Department department;\n    \n    public Employee(int id, String name, String email, String phone, Department department) {\n    \tthis.id = id;\n        this.name = name;\n        this.email = email;\n        this.phone = phone;\n        this.department =  department;\n\t}\n\n\tpublic int getId() {\n\t\treturn id;\n\t}\n\n\tpublic void setId(int id) {\n\t\tthis.id = id;\n\t}\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\n\tpublic String getEmail() {\n\t\treturn email;\n\t}\n\n\tpublic void setEmail(String email) {\n\t\tthis.email = email;\n\t}\n\n\tpublic String getPhone() {\n\t\treturn phone;\n\t}\n\n\tpublic void setPhone(String phone) {\n\t\tthis.phone = phone;\n\t}\n\n\tpublic Department getDepartment() {\n\t\treturn department;\n\t}\n\n\tpublic void setDepartment(Department department) {\n\t\tthis.department = department;\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Employee [id=\" + id + \", name=\" + name + \", email=\" + email + \", phone=\" + phone + \", department=\"+ department + \"]\";\n\t}\n}\n \n## Department\n\npackage com.sample.entity;\npublic class Department {\n    private int  departmentId;\n    private String departmentName;\n    \n    public Department(int departmentId, String departmentName) {\n    \tthis.departmentId =  departmentId;\n    \tthis.departmentName = departmentName;\n\t}\n\n\tpublic int getDepartmentId() {\n\t\treturn departmentId;\n\t}\n\n\tpublic void setDepartmentId(int departmentId) {\n\t\tthis.departmentId = departmentId;\n\t}\n\n\tpublic String getDepartmentName() {\n\t\treturn departmentName;\n\t}\n\n\tpublic void setDepartmentName(String departmentName) {\n\t\tthis.departmentName = departmentName;\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"Department [departmentId=\" + departmentId + \", departmentName=\" + departmentName + \"]\";\n\t}\t\n}\n\n## EmployeeDTO\n\npackage com.sample.DTO;\npublic class EmployeeDTO {\n\t\n    private int id;\n    private String name;\n    private String email;\n    private String phone;\n    private DepartmentDTO departmentDTO;\n    \n    public EmployeeDTO( int id, String name, String email, String phone, DepartmentDTO department) {\n    \tthis.id = id;\n        this.name = name;\n        this.email = email;\n        this.phone = phone;\n        this.departmentDTO =  department;\n\t}\n\n\tpublic int getId() {\n\t\treturn id;\n\t}\n\n\tpublic void setId(int id) {\n\t\tthis.id = id;\n\t}\n\n\tpublic String getName() {\n\t\treturn name;\n\t}\n\n\tpublic void setName(String name) {\n\t\tthis.name = name;\n\t}\n\n\tpublic String getEmail() {\n\t\treturn email;\n\t}\n\n\tpublic void setEmail(String email) {\n\t\tthis.email = email;\n\t}\n\n\tpublic String getPhone() {\n\t\treturn phone;\n\t}\n\n\tpublic void setPhone(String phone) {\n\t\tthis.phone = phone;\n\t}\n\n\tpublic DepartmentDTO getDepartmentDTO() {\n\t\treturn departmentDTO;\n\t}\n\n\tpublic void setDepartmentDTO(DepartmentDTO departmentDTO) {\n\t\tthis.departmentDTO = departmentDTO;\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"EmployeeDTO [id=\" + id + \", name=\" + name + \", email=\" + email + \", phone=\" + phone + \", departmentDTO=\"\n\t\t\t\t+ departmentDTO + \"]\";\n\t}\n}\n\n## DepartmentDTO\n\npackage com.sample.DTO;\npublic class DepartmentDTO {\n    private int  departmentId;\n    private String departmentName;\n    \n    public DepartmentDTO(int departmentId, String departmentName) {\n    \tthis.departmentId =  departmentId;\n    \tthis.departmentName = departmentName;\n\t}\n\n\tpublic int getDepartmentId() {\n\t\treturn departmentId;\n\t}\n\n\tpublic void setDepartmentId(int departmentId) {\n\t\tthis.departmentId = departmentId;\n\t}\n\n\tpublic String getDepartmentName() {\n\t\treturn departmentName;\n\t}\n\n\tpublic void setDepartmentName(String departmentName) {\n\t\tthis.departmentName = departmentName;\n\t}\n\n\t@Override\n\tpublic String toString() {\n\t\treturn \"DepartmentDTO [departmentId=\" + departmentId + \", departmentName=\" + departmentName + \"]\";\n\t}\n}\n\n## Mapper\n\npackage com.sample.mapper;\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Collectors;\nimport com.sample.DTO.DepartmentDTO;\nimport com.sample.DTO.EmployeeDTO;\nimport com.sample.entity.Employee;\n\npublic class Mapper {\n    private List\u003cEmployee\u003e employee = new ArrayList\u003cEmployee\u003e();\n    public Mapper(List\u003cEmployee\u003e employees) {\n        this.employee = employees;\n    }\n\n    public List\u003cEmployeeDTO\u003e map() {\n    \t// id,  name,  email,  phone, Department department - departId, departName\n        List\u003cEmployeeDTO\u003e employeeDTO = employee\n        \t\t.stream()\n        \t\t.map(o -\u003e new EmployeeDTO(o.getId(),o.getName(),o.getEmail(),o.getPhone(),\n        \t\t\t\tnew DepartmentDTO(o.getDepartment().getDepartmentId(), o.getDepartment().getDepartmentName())))\n                .collect(Collectors.toList());\n        return employeeDTO;\n    }\n}\n\n## StreamApiSample\n\nimport java.util.Arrays;\nimport java.util.List;\nimport com.sample.entity.Department;\nimport com.sample.entity.Employee;\nimport com.sample.mapper.Mapper;\n\npublic class StreamApiSample {\n\tpublic static void main(String arg[]) {\n\t\tSystem.out.println(\"Convert List of Employee to List of EmployeeDTO\");\n\t\t\n\t\tList\u003cEmployee\u003e listOfEmployee = Arrays.asList(\n\t\t\t\tnew Employee(1, \"Saravanan\", \"Saravanan@test.com\", \"01234567890\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(2, \"Seenu\", \"Seenu@test.com\", \"12345678900\", new Department(2, \"Civil Engineering\")),\n\t\t\t\tnew Employee(3, \"Ramu\", \"Ramu@test.com\", \"23456789001\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(4, \"Shankar\", \"Shankar@test.com\", \"34567890012\", new Department(3, \"Mechanical Engineering\")),\n\t\t\t\tnew Employee(5, \"Seyon\", \"Seyon@test.com\", \"45678900123\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(6, \"Tharun\", \"Tharun@test.com\", \"56789001234\", new Department(2, \"Civil Engineering\")),\n\t\t\t\tnew Employee(7, \"Arun\", \"Arun@test.com\", \"67890012345\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(8, \"Karthik\", \"Karthik@test.com\", \"78900123456\", new Department(4, \"EEE Engineering\")),\n\t\t\t\tnew Employee(9, \"Kumar\", \"Kumar@test.com\", \"89001234567\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(10, \"Vignesh\", \"Vignesh@test.com\", \"90012345678\", new Department(4, \"EEE Engineering\"))\n\t\t\t\t); \n\t\t\n\t\tMapper mapper = new Mapper(listOfEmployee);\n\t\tmapper.map().stream().forEach(emp -\u003e System.out.println(emp));\n\t}\n}\n\n## Output\n\nConvert List of Employee to List of EmployeeDTO\n\nEmployeeDTO [id=1, name=Saravanan, phone=01234567890, departmentDTO=DepartmentDTO]\u003c/br\u003e\nEmployeeDTO [id=2, name=Seenu, phone=12345678900, departmentDTO=DepartmentDTO]\u003c/br\u003e\nEmployeeDTO [id=3, name=Ramu, phone=23456789001, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=4, name=Shankar, phone=34567890012, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=5, name=Seyon, phone=45678900123, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=6, name=Tharun, phone=56789001234, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=7, name=Arun, phone=67890012345, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=8, name=Karthik, phone=78900123456, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=9, name=Kumar, phone=89001234567, departmentDTO=DepartmentDTO \u003c/br\u003e\nEmployeeDTO [id=10, name=Vignesh, phone=90012345678, departmentDTO=DepartmentDTO \u003c/br\u003e\n\u003c/details\u003e\n\n## Example 2- Convert List of String to List of Integer\n\u003cdetails\u003e\n\t\u003csummary\u003eCode Section\u003c/summary\u003e\n\n## StreamApiSample\n \nimport java.util.Arrays;\nimport java.util.List;\nimport com.sample.entity.Department;\nimport com.sample.entity.Employee;\nimport com.sample.mapper.Mapper;\npublic class StreamApiSample {\n\tpublic static void main(String arg[]) {\n\t\tSystem.out.println(\"Convert List of String to List of Integer\");\n\t\tMapper mapper = new Mapper(listOfEmployee);\n\t\tmapper.map().stream().forEach(emp -\u003e System.out.println(emp));\n\t\tList\u003cString\u003e list = Arrays.asList( \"8\" , \"7\", \"36\", \"2\" );\n\t        List\u003cInteger\u003e intList = list.stream().map(s -\u003e Integer.parseInt(s)).collect(Collectors.toList());\n\t        intList.stream().forEach(obj -\u003e System.out.println(obj));    \n\t}\n}\n\n\n\n## Output\n\nConvert List of String to List of Integer\n8\n7\n36\n2\n\u003c/details\u003e\n\n## Example 3- List of Employees, Count by Department\n\u003cdetails\u003e\n\t\u003csummary\u003eCode Section\u003c/summary\u003e\n\n## StreamApiSample\n \nimport java.util.Arrays;\nimport java.util.List;\nimport java.util.Map;\nimport java.util.stream.Collectors;\nimport com.sample.DTO.EmployeeDTO;\nimport com.sample.entity.Department;\nimport com.sample.entity.Employee;\nimport com.sample.mapper.Mapper;\npublic class StreamApiSample {\n\tpublic static void main(String arg[]) {\n\t\tSystem.out.println(\"Convert List of Employee to List of EmployeeDTO\");\n\t\tList\u003cEmployee\u003e listOfEmployee = Arrays.asList(\n\t\t\t\tnew Employee(1, \"Saravanan\", \"Saravanan@test.com\", \"01234567890\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(2, \"Seenu\", \"Seenu@test.com\", \"12345678900\", new Department(2, \"Civil Engineering\")),\n\t\t\t\tnew Employee(3, \"Ramu\", \"Ramu@test.com\", \"23456789001\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(4, \"Shankar\", \"Shankar@test.com\", \"34567890012\", new Department(3, \"Mechanical Engineering\")),\n\t\t\t\tnew Employee(5, \"Seyon\", \"Seyon@test.com\", \"45678900123\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(6, \"Tharun\", \"Tharun@test.com\", \"56789001234\", new Department(2, \"Civil Engineering\")),\n\t\t\t\tnew Employee(7, \"Arun\", \"Arun@test.com\", \"67890012345\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(8, \"Karthik\", \"Karthik@test.com\", \"78900123456\", new Department(4, \"EEE Engineering\")),\n\t\t\t\tnew Employee(9, \"Kumar\", \"Kumar@test.com\", \"89001234567\", new Department(1, \"Computer Science\")),\n\t\t\t\tnew Employee(10, \"Vignesh\", \"Vignesh@test.com\", \"90012345678\", new Department(4, \"EEE Engineering\"))\n\t\t\t\t); \n\t\tMapper mapper = new Mapper(listOfEmployee);\n\t\tmapper.map().stream().forEach(emp -\u003e System.out.println(emp));\n\t\tMap\u003cString, List\u003cEmployeeDTO\u003e\u003e listOfEmpByDep = mapper.map().stream()\n\t\t\t\t.collect(Collectors.groupingBy(empDTO -\u003e empDTO.getDepartmentDTO().getDepartmentName()));\n\t\tSystem.out.println(\"------List of Employees by department------\");\n\t\tlistOfEmpByDep.forEach((departmentName, empList) -\u003e System.out.println(departmentName +\", \"+ empList));\n\t\tSystem.out.println(\"------Employees count based department-----\");\n\t\tMap\u003cString, Long\u003e listOfEmpByDepCount = mapper\n\t\t\t\t.map()\n\t\t\t\t.stream()\n\t\t\t\t.collect(Collectors.groupingBy(empDTO -\u003e \n\t\t\t\tempDTO.getDepartmentDTO().getDepartmentName(), \n\t\t\t\tCollectors.counting()));\n\t\tlistOfEmpByDepCount.forEach((departmentName, empCount) -\u003e System.out.println(departmentName +\", \"+ empCount));\t\n\t}\n}\n\n# Output\n\n------List of Employees by department------\nCivil Engineering, [EmployeeDTO [id=2, name=Seenu, phone=12345678900, departmentDTO=DepartmentDTO], EmployeeDTO [id=6, name=Tharun, phone=56789001234, departmentDTO=DepartmentDTO]]\nComputer Science, [EmployeeDTO [id=1, name=Saravanan, phone=01234567890, departmentDTO=DepartmentDTO], EmployeeDTO [id=3, name=Ramu, phone=23456789001, departmentDTO=DepartmentDTO], EmployeeDTO [id=5, name=Seyon, phone=45678900123, departmentDTO=DepartmentDTO], EmployeeDTO [id=7, name=Arun, phone=67890012345, departmentDTO=DepartmentDTO], EmployeeDTO [id=9, name=Kumar, phone=89001234567, departmentDTO=DepartmentDTO]]\nEEE Engineering, [EmployeeDTO [id=8, name=Karthik, phone=78900123456, departmentDTO=DepartmentDTO], EmployeeDTO [id=10, name=Vignesh, phone=90012345678, departmentDTO=DepartmentDTO]]\nMechanical Engineering, [EmployeeDTO [id=4, name=Shankar, phone=34567890012, departmentDTO=DepartmentDTO]]\n------Employees count based department-----\nCivil Engineering, 2\nComputer Science, 5\nEEE Engineering, 2\nMechanical Engineering, 1\n\u003c/details\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaravanan81java%2Fjava-stream-api-samples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaravanan81java%2Fjava-stream-api-samples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaravanan81java%2Fjava-stream-api-samples/lists"}