Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e7482d6

Browse filesBrowse files
Java Stream Sorting Example
1 parent 529adc6 commit e7482d6
Copy full SHA for e7482d6

File tree

Expand file treeCollapse file tree

10 files changed

+359
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+359
-0
lines changed
Open diff view settings
Collapse file

‎JavaStreamSortingExample/.idea/encodings.xml‎

Copy file name to clipboardExpand all lines: JavaStreamSortingExample/.idea/encodings.xml
+4Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎JavaStreamSortingExample/.idea/misc.xml‎

Copy file name to clipboardExpand all lines: JavaStreamSortingExample/.idea/misc.xml
+6Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎JavaStreamSortingExample/.idea/modules.xml‎

Copy file name to clipboardExpand all lines: JavaStreamSortingExample/.idea/modules.xml
+8Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎JavaStreamSortingExample/.idea/workspace.xml‎

Copy file name to clipboardExpand all lines: JavaStreamSortingExample/.idea/workspace.xml
+143Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Collapse file
+102Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.javadevzone;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
/**
9+
* Created by java developer zone on 5/21/2017.
10+
*/
11+
public class Employee implements Comparable<Employee>{
12+
private int no;
13+
private String name;
14+
private String designation;
15+
private String gender;
16+
private Set<String> languages;
17+
18+
public Set<String> getLanguages() {
19+
return languages;
20+
}
21+
22+
public Employee(int no, String name, String designation, String gender , String [] languages) {
23+
this.no = no;
24+
this.name = name;
25+
26+
this.designation = designation;
27+
this.gender = gender;
28+
this.languages = new HashSet<>(Arrays.asList(languages));
29+
}
30+
31+
public void addLanguage(String language){
32+
this.languages.add(language);
33+
}
34+
35+
public int getNo() {
36+
return no;
37+
}
38+
public void setNo(int no) {
39+
this.no = no;
40+
}
41+
public String getName() {
42+
return name;
43+
}
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
public String getDesignation() {
48+
return designation;
49+
}
50+
public void setDesignation(String designation) {
51+
this.designation = designation;
52+
}
53+
public String getGender() {
54+
return gender;
55+
}
56+
public void setGender(String gender) {
57+
this.gender = gender;
58+
}
59+
@Override
60+
public String toString() {
61+
return this.no + " : " + this.name + " : " + gender + " : " + designation;
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
int hashno = 7;
67+
hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
68+
return hashno;
69+
}
70+
71+
@Override
72+
public boolean equals(Object obj) {
73+
if (obj == null) {
74+
return false;
75+
}
76+
final Employee std = (Employee) obj;
77+
if (this == std) {
78+
return true;
79+
} else {
80+
return (this.name.equals(std.name)
81+
&& (this.no == std.no)) && (this.designation.equals(std.designation))
82+
&& (this.gender.equals(std.gender));
83+
}
84+
}
85+
86+
public static java.util.List<Employee> getEmployee() {
87+
java.util.List<Employee> employees = new ArrayList<>();
88+
employees.add(new Employee(1, "Bob", "Developer", "Male",new String[]{"java","scala"}));
89+
employees.add(new Employee(2, "Joy", "Sr. Developer", "Male",new String[]{"java"}));
90+
employees.add(new Employee(3, "John", "CEO", "Male",new String[]{"python","ruby"}));
91+
employees.add(new Employee(4, "Bat", "Developer", "Male",new String[]{"scala"}));
92+
employees.add(new Employee(5, "Jolly", "Developer", "Female",new String[]{"C","C++"}));
93+
employees.add(new Employee(6, "Bobby", "Developer", "Female",new String[]{".Net","VB"}));
94+
return employees;
95+
}
96+
97+
@Override
98+
public int compareTo(Employee o) {
99+
return name.compareTo(o.getName());
100+
}
101+
102+
}
Collapse file
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.javadevzone;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
6+
import java.util.stream.Stream;
7+
8+
/**
9+
* Created by java developer zone on 5/21/2017.
10+
*/
11+
public class StreamSortingExample {
12+
13+
public void primitiveSort(){
14+
15+
//Build IntStream
16+
IntStream intStream = IntStream.of(3, 2, 1, 2, 5);
17+
System.out.println("Sort IntStream");
18+
intStream.sorted().forEach(x -> System.out.print(x+","));//natural order sort & print
19+
20+
//Build list of numbers
21+
List<Integer> numbers = Arrays.asList(3, 2, 1, 2, 5);
22+
System.out.println(System.lineSeparator()+"Sort list of int numbers");
23+
numbers.stream().sorted().forEach(x -> System.out.print(x+",")); //natural order sort & print
24+
25+
26+
}
27+
28+
public void reverseSorting(){
29+
30+
//Build list of numbers
31+
List<Integer> numbers = Arrays.asList(3, 2, 1, 2, 5);
32+
System.out.println(System.lineSeparator()+"Sort list of int numbers");
33+
numbers.stream().sorted(Comparator.reverseOrder()).forEach(x -> System.out.print(x+",")); //reverse order sort & print
34+
}
35+
36+
public static void main(String[] args) {
37+
StreamSortingExample streamSortingExample = new StreamSortingExample();
38+
//streamSortingExample.primitiveSort();
39+
//streamSortingExample.reverseSorting();
40+
41+
//streamSortingExample.listSorting();
42+
//streamSortingExample.setSorting();
43+
//streamSortingExample.MapSorting();
44+
streamSortingExample.userDefindObjectSorting();
45+
46+
}
47+
48+
public void userDefindObjectSorting(){
49+
List<Employee> employeeList = Employee.getEmployee();
50+
System.out.println("---Natural Sorting by No---");
51+
List<Employee> slist = employeeList.stream().sorted(Comparator.comparing(Employee::getNo)).collect(Collectors.toList());
52+
slist.forEach(e -> System.out.println("Id:"+ e.getNo()+", Name: "+e.getName()));
53+
}
54+
55+
public void listSorting(){
56+
List<Employee> employeeList = Employee.getEmployee();
57+
58+
System.out.println("---Sorting using Comparator by Gender--");
59+
List<Employee> slist = employeeList.stream().sorted(Comparator.comparing(Employee::getGender)).collect(Collectors.toList());
60+
slist.forEach(e -> System.out.println("Id:"+ e.getNo()+", Name: "+e.getName()+", Gender:"+e.getGender()));
61+
}
62+
63+
public void setSorting(){
64+
HashSet<Employee> employeeSet = new HashSet<>(Employee.getEmployee());
65+
66+
System.out.println("---Sorting using Comparator by Designation---");
67+
List<Employee> slist= employeeSet.stream().sorted(Comparator.comparing(Employee::getDesignation)).collect(Collectors.toList());
68+
slist.forEach(e -> System.out.println("Id:"+ e.getNo()+", Name: "+e.getName()+", Designation:"+e.getDesignation()));
69+
}
70+
71+
public void MapSorting(){
72+
Map<Integer, String> map = new HashMap<>();
73+
map.put(2, "John");
74+
map.put(1, "gim");
75+
map.put(3, "orton");
76+
77+
System.out.println("---Sort by Map Value---");
78+
map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
79+
.forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
80+
81+
System.out.println("---Sort by Map Key---");
82+
map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
83+
.forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
84+
}
85+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.