0% found this document useful (0 votes)
28 views

Interview Program

The document contains 3 Java programs: 1) A program that counts the number of characters in a string and stores them in a map. 2) A program that sorts a map by value in descending order and prints the sorted map. 3) A program that sorts a list of custom Student objects by name, age, and id using lambda expressions.

Uploaded by

Phyadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Interview Program

The document contains 3 Java programs: 1) A program that counts the number of characters in a string and stores them in a map. 2) A program that sorts a map by value in descending order and prints the sorted map. 3) A program that sorts a list of custom Student objects by name, age, and id using lambda expressions.

Uploaded by

Phyadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Program for counting no of charactor in String.

------------------------------------------------------

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class test {


public static void main(String[] args) {

String aa = "Protijayi Gini";


String[] stringarray = aa.split("");

Map<String , Long> map = Arrays.stream(stringarray)


.collect(Collectors.groupingBy(c -> c , Collectors.counting()));
map.forEach( (k, v) -> System.out.println(k + " : "+ v));
}

output: -
: 1
P : 1
a : 1
r : 1
t : 1
G : 1
y : 1
i : 4
j : 1
n : 1
o : 1

2. Program for Prnting Map data in Key- Assending/decending order or


in name alpha betic format.
----------------------------------------------------------------------

import java.util.*;

public class Coding {


public static void main(String[] args) {

Map<String, Integer> unsortMap = new HashMap<>();


unsortMap.put("z", 10);unsortMap.put("b", 5);unsortMap.put("a",
6);unsortMap.put("c", 20);unsortMap.put("d", 1);
unsortMap.put("e", 7);unsortMap.put("y", 8);unsortMap.put("n",
99);unsortMap.put("g", 50);unsortMap.put("m", 2);
unsortMap.put("f", 9);

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()));

System.out.println("Sorted Map : " + sortedMap);


}
}

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}

3. Shorting List of custom Object ?

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:");

//java 8 forEach for printing the list


studentlist.forEach((s)->System.out.println(s));

System.out.println("After Sorting the student data by Age:");

//Lambda expression for sorting by age


studentlist.sort((Student s1, Student s2)->s1.getAge()-s2.getAge());

//java 8 forEach for printing the list


studentlist.forEach((s)->System.out.println(s));

System.out.println("After Sorting the student data by Name:");


//Lambda expression for sorting the list by student name
studentlist.sort((Student s1, Student s2)-
>s1.getName().compareTo(s2.getName()));
studentlist.forEach((s)->System.out.println(s));
System.out.println("After Sorting the student data by Id:");

//Lambda expression for sorting the list by student id


studentlist.sort((Student s1, Student s2)->s1.getId()-s2.getId());
studentlist.forEach((s)->System.out.println(s));
}
}

output:

Before Sorting the student data:


Student[ Name:Jon Age: 22 Id: 1001]
Student[ Name:Steve Age: 19 Id: 1003]
Student[ Name:Kevin Age: 23 Id: 1005]
Student[ Name:Ron Age: 20 Id: 1010]
Student[ Name:Lucy Age: 18 Id: 1111]

After Sorting the student data by Age:

Student[ Name:Lucy Age: 18 Id: 1111]


Student[ Name:Steve Age: 19 Id: 1003]
Student[ Name:Ron Age: 20 Id: 1010]
Student[ Name:Jon Age: 22 Id: 1001]
Student[ Name:Kevin Age: 23 Id: 1005]

After Sorting the student data by Name:

Student[ Name:Jon Age: 22 Id: 1001]


Student[ Name:Kevin Age: 23 Id: 1005]
Student[ Name:Lucy Age: 18 Id: 1111]
Student[ Name:Ron Age: 20 Id: 1010]
Student[ Name:Steve Age: 19 Id: 1003]

After Sorting the student data by Id:

Student[ Name:Jon Age: 22 Id: 1001]


Student[ Name:Steve Age: 19 Id: 1003]
Student[ Name:Kevin Age: 23 Id: 1005]
Student[ Name:Ron Age: 20 Id: 1010]
Student[ Name:Lucy Age: 18 Id: 1111]

You might also like