Frequently Java8 Asked Programs

Veenarao
4 min readOct 22, 2023

--

JAVA8 Frequently Asked Programs
JAVA8 Frequently Asked Programs

Following are some of the famously asked Java8 Interview Questions.

  1. Print Even nos from the list
int arr[] = new int[]{9,10,15,8,49,25,98,32,10};
Arrays.stream(arr).filter(i->i%2==0).forEach(System.out::println);

2. Find First element in the list

int arr[] = new int[]{9,10,15,8,49,25,98,32,10}; 
Arrays.stream(arr).findFirst().ifPresent(System.out::println);

3. Find duplicates in the list

Set<Integer> set = new HashSet<>();
Arrays.stream(arr).filter(s->!set.add(s)).forEach(System.out::println);

4. Print nos that starts with 1 from the list

 Arrays.stream(arr).mapToObj(s->s+"").filter(s->s.startsWith("1")).
forEach(System.out::println);

5. Find minimum and maximum in the list

 System.out.println("Max in array"+Arrays.stream(arr).max().getAsInt());
System.out.println("Min in array"+Arrays.stream(arr).min().getAsInt());

6. Find first non repeated character

System.out.println("first non repeated character::"+
Stream.of(input.split("")).
collect(Collectors.groupingBy(Function.identity(),LinkedHashMap:: new ,
Collectors.counting())).entrySet().stream().
filter(entry -> entry.getValue()>=1).
map(entry -> entry.getKey()).findFirst().get());

7. Sort the elements in asc and desc

 Arrays.stream(arr).sorted().forEach(System.out::println);
Arrays.stream(arr).boxed().sorted(Collections.reverseOrder()).
forEach(System.out::println);

8. Perform cube on list elements and filter numbers greater than 50

list.stream().map(i->i*i*i).filter(i->i>50).
collect(Collectors.toList()).forEach(System.out::println);

9. To sort an array and then convert the sorted array into Stream

Arrays.parallelSort(arr);
Arrays.stream(arr).forEach(System.out::println);

10. Find unique nos from the list

List list= Arrays.asList(11,22,33,44,11,22);
System.out.println(list.stream().
filter(x-> Collections.frequency(list,x)==1).
collect(Collectors.toList()));

11. Optional Field usage

class  Nodes{
int id;
String tagName;
int count;

Nodes(int id,String tagName,int count){
this.id=id;
this.tagName=tagName;
this.count=count;
}
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTagName() {
return tagName;
}

public void setTagName(String tagName) {
this.tagName = tagName;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}
Nodes node1=new Nodes(1,"vee",1);
Nodes node2=new Nodes(2,"viv",2);
List<Nodes> nodesList=new ArrayList<>();
nodesList.add(node1);
nodesList.add(node2);

Optional.ofNullable(nodesList).
orElseGet(Collections::emptyList).
stream().filter(Objects::nonNull).
map(Nodes::getTagName).forEach(s->System.out.println("tag names is:"+s));

12 . To find only duplicate elements from the String ArrayList

List<String> list1 = Arrays.asList("abc", "def", "xyz", "mno", "pqr", 
"def", "xyz", "stu");
Set<String> set1=new LinkedHashSet<>();
list1.stream().filter(s->set1.add(s)).forEach(System.out::println);

13. To print the count of each character in a String

String s = "string data to count each character";
Map<String,Long> countMap= Arrays.stream(s.split("")).
map(String::toLowerCase).
collect(Collectors.groupingBy(str -> str,
LinkedHashMap::new,Collectors.counting()));
System.out.println(countMap);

14 . Print odd and Even list

int[] arr1= new int[]{1,2,4,6,7};
System.out.println(Arrays.stream(arr1).
mapToObj(ob -> Integer.valueOf(ob)).
collect(Collectors.groupingBy(t-> t%2 == 0 ?"ODD" : "EVEN")));

15. Print odd , Even sum

Map<String, Integer> map1 = Arrays.stream(arr1).boxed().
collect(Collectors.groupingBy(e -> e % 2 == 0 ? "OddSum" : "EvenSum",
Collectors.summingInt(Integer::intValue)));
System.out.println("map::"+map1);

16. Convert list to map

class Person{
int id;
String name;
Person(int id,String name){
this.id=id;
this.name=name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
List<Person> listPerson = new ArrayList<>();
listPerson.add(new Person(1,"vna"));
listPerson.add(new Person(2,"vid"));
Map<Integer,Person1> mapPerson = listPerson.stream().
collect(Collectors.toMap(Person1::getId,Function.identity()));
System.out.println(mapPerson);

17 . Find the count of words in a string

String  str = "this is word count for word is good";
Map<String,Long> mapStr=Arrays.stream(str.split(" ")).
filter(x->Collections.frequency(Arrays.asList(str.split(" ")),x)>=1).
collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,
Collectors.counting()));
System.out.println(mapStr);

o/p this -1, is-2 ,word-2,count-1,for-1

18. Print the word count in reverse order

Arrays.stream(str.split(" "))
.filter(x->Collections.frequency(Arrays.asList(str.split(" ")),x)>=1)
.collect(Collectors.groupingBy(Function.identity(),LinkedHashMap::new,Collectors.counting())).entrySet().stream()
.sorted(Map.Entry.<String, Long> comparingByValue().reversed()
.thenComparing(Map.Entry.comparingByKey())).forEach(System.out::println);

o/p is-2 ,word-2,this -1,count-1,for-1

19. Print Employee salary in Ascending order

class Employee{
private int id;
private String name;
private int age;
private long salary;
public Employee(int id, String name, int age, long salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
List < Employee > employees = new ArrayList < Employee > ();
employees.add(new Employee(10, "Ramesh", 30, 400000));
employees.add(new Employee(20, "John", 29, 350000));
employees.add(new Employee(30, "Tom", 30, 450000));
employees.add(new Employee(40, "Pramod", 29, 500000));
employees.add(new Employee(50, "Pramodini", 31, 500000));

List<Employee> resultAsc=employees.stream().
sorted(((o1, o2) -> (int)(o1.getSalary()- o2.getSalary()))).
collect(Collectors.toList());
System.out.println("Ascending order::"+resultAsc);
System.out.println(employees.stream().
sorted(Comparator.comparingLong(Employee::getSalary)).
collect(Collectors.toList()));

List<Employee> resultDesc=employees.stream().
sorted(((o1, o2) -> (int)(o2.getSalary()- o1.getSalary()))).
collect(Collectors.toList());
System.out.println("Descending order::"+resultDesc);
System.out.println(employees.stream().
sorted(Comparator.comparingLong(Employee::getSalary).reversed()).
collect(Collectors.toList()));

20 . Occurrence of elements

List<String> names = Arrays.asList("AA", "BB", "AA", "CC");
HashMap<String,Long> map = names.stream().
collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new,
Collectors.counting()));
System.out.println(map);
  • Thank you for reading this article. Please provide your valuable suggestions/ feedback.
  • Clap and Share if you liked the content.
  • 📰 Read more content on my Medium (on Java Developer interview questions)
  • 🔔 Follow me: LinkedIn

Please find my other useful articles on Java Developer interview questions

--

--