Interview Program
Interview Program
Interview Program
------------------------------------------------------
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
output: -
: 1
P : 1
a : 1
r : 1
t : 1
G : 1
y : 1
i : 4
j : 1
n : 1
o : 1
import java.util.*;
System.out.println("Original...");
System.out.println(unsortMap);
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
unsortMap.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
output: -
Original...
: {a=6, b=5, c=20, d=1, e=7, f=9, g=50, y=8, z=10, m=2, n=99}
Sorted Map : {n=99, g=50, c=20, z=10, f=9, y=8, e=7, a=6, b=5, m=2, d=1}
import java.util.*;
class Student {
String name;
int age;
int id;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
Student(String n, int a, int i){
name = n;
age = a;
id = i;
}
@Override public String toString() {
return ("Student[ "+"Name:"+this.getName()+
" Age: "+ this.getAge() +
" Id: "+ this.getId()+"]");
}
}
public class Example {
public static void main(String[] args) {
List<Student> studentlist = new ArrayList<Student>();
studentlist.add(new Student("Jon", 22, 1001));
studentlist.add(new Student("Steve", 19, 1003));
studentlist.add(new Student("Kevin", 23, 1005));
studentlist.add(new Student("Ron", 20, 1010));
studentlist.add(new Student("Lucy", 18, 1111));
System.out.println("Before Sorting the student data:");
output: