Core Java

Core Java

Share

for learning purpose core Java

01/08/2023

important with solution.

Hello, guys! 👋 Welcome to my page! 🎉

I'm excited to have you here on this platform where we'll explore the fascinating world of coding and programming together! 💻🌐

I have writing here some logical program using java 8 feature Stream API.
-----------------------------------------------------------------------
Q.1 Write a program to sort number using stream.

List list= Arrays.asList(1,4,2,5,6,3,10);

List sortedlist=list.stream().sorted().collect(Collectors.toList());
System.out.println(sortedlist);//[1, 2, 3, 4, 5, 6, 10]
-----------------------------------------------------------------------
Q.2 write a program to print even number from list

List evenNumber=list.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println(evenNumber); //[4, 2, 6,10]

-----------------------------------------------------------------------
Q.3 write a program to print even number from list and multiply with 5

List evenNumber=list.stream().filter(i->i%2==0).map(m->m*5).collect(Collectors.toList());
System.out.println(evenNumber); //[20, 10, 30, 50]
-----------------------------------------------------------------------
Q.3 Write a program to count number in stream

System.out.println(list.stream().count()); //7
-----------------------------------------------------------------------
Q.4 write a program to print number start with 1

List startwithone= list.stream().filter(f->f.toString().startsWith("1")).collect(Collectors.toList());
System.out.println(startwithone); //[1, 10]
-----------------------------------------------------------------------
Q.5 write a program to find duplicate element from list using stream api.

List list = Arrays.asList(1, 2, 3, 2, 4,2, 5, 6, 4, 7, 8, 8, 9);

Set set= new HashSet();
list.stream().filter(i>!set.add(i)).distinct().forEach(System.out::println);

-----------------------------------------------------------------------
Q.6 write a program to sort employee thier name

List employees = Arrays.asList(new Employee(1,"John", 4500),new Employee(2,"Alice", 6000),
new Employee(3,"Bob", 3500),new Employee(4,"Jane", 8000));
employees.sort(Comparator.comparing(Employee::getName));

for(Employee e1:employees) {
System.out.println("empName==>"+e1.getName());
}
-----------------------------------------------------------------------
Q.7 write a program to get employee where sal greater than 5000

List filteredEmployees = employees.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList());

for (Employee e1: filteredEmployees) {
System.out.println("Name: " + e1.getName() + ", Salary: " + e1.getSalary());
}
-----------------------------------------------------------------------
Q.8 write a program to count the occurrences of each character and print the character along with its frequency.

String inputstr="programming";

Map map = inputstr.chars().mapToObj(c->(char)c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(map);
}

----------------------------------------------------------------------
Q,9 write a program to print asc order in java
List list=Arrays.asList("pritam","rakesh","rajput","deepak");

Collections.sort(list,Comparator.naturalOrder());
System.out.println(list);
-----------------------------------------------------------------------
Q,10 . write a program to print desc order

List list= Arrays.asList(1,2,3,4,5,71,7,0);

Collections.sort(list,Comparator.reverseOrder());
System.out.println(list);
----------------------------------------------------------------------

23/06/2023

write a program to move all zero right side and all number on left side.
Input : arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output : arr[] = {1, 2, 4, 3, 5, 0, 0, 0};

Input : arr[] = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};

public class Main {
public static void moveZeros(int[] arr) {
int n = arr.length;
int count = 0; // Count of non-zero elements

// Traverse the array
for (int i = 0; i < n; i++) {
if (arr[i] != 0) {
arr[count] = arr[i];
count++;
}
}

// Fill remaining elements with 0
while (count < n) {
arr[count] = 0;
count++;
}
}

public static void main(String[] args) {
int[] arr = {1, 2, 0, 4, 3, 0, 5, 0};
moveZeros(arr);

// Print the modified array
for (int num : arr) {
System.out.print(num + " ");
}
}
}

21/06/2023

Hi all,
Use below to get highest salary of employee like 1st ,2nd ,5th ---- nth (oracle database).

Type 1:
=====
SELECT MAX(SAL) FROM EMP;

Type 2:
=====
SELECT SAL FROM (SELECT SAL, RANK() OVER ( ORDER BY SAL DESC) AS RANK FROM EMP ) T WHERE RANK=5;

In place of 5 you can passed any number based on requirement.

16/06/2023

2023

mostly time below repeated question they will asked on coding round.

1.write a program to print Fibonacci series in java.
o/p = 1,1,2,3,5,8,13,21,34 - - - -

2. write a program to find some of digits of given number.
n=123
o/p =6

3. write a program to print unique number in array in java.
{1,2,2,3,4,4,5,6,6,7,8,8,9}
o/p = 1,3,5,7,9

4. write a Logic to find 2nd highest element in array.

Int a[] ={ 1,2,3,4,5};

5. lets suppose you have a string "CAT" and you have right down all the combinations with the letters;
e.g.
CAT
ATC
CTA
TAC
ACT
TCA
CAT -> CAT , ATC, CTA, TAC

6. write a logic to print palindrome in java.

7. write a logic to print factorial of 5.

8. write a program to showing polymorphism in java.

16/06/2023

important Question
with answer.

Hello All, below question are asked to me in my interview as 3+ experienced in

===========================================
Q.1 Is it possible to change the port of the embedded tomcat sever in spring boot

ans. yes it is possible by using server.port in the application.properties file
server.port=9090

===========================================
Q.2 can we override or replace the Embedded tomcat server in spring boot.

ans. yes we can replace the embedded tomcat server with any server by using the Starter dependency in the pom.xml file.

1. Exclude the default embedded Tomcat dependency from your pom.xml file if you are using Maven.



org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-tomcat




2.Include the dependency for the desired embedded server. For example, if you want to use Jetty,
include the following dependencies:



org.springframework.boot
spring-boot-starter-jetty


===========================================
3.what is auto-configuration in spring boot ? how does it help ? why spring boot is called opinionated.
ans. auto configuration is spring boot magic features. it automatically configures a lot of things based upon what is
present in the classpath.
for example spring boot starter web,

===========================================
4. what is the difference between and annotation.
ans. :

It is a convenience annotation that combines three commonly used annotations: , , and .

It is typically used to annotate the main class of a Spring Boot application.
The annotation indicates that the class declares one or more Spring beans.
The annotation triggers the auto-configuration process in Spring Boot.
The annotation scans the specified packages for Spring components, such as controllers, services, and repositories.

:
==================

It is a standalone annotation used to enable auto-configuration in a Spring application.
It triggers the automatic configuration of Spring beans based on the classpath and environment.
When is present, Spring Boot uses its algorithm to scan the classpath and configure beans based on the libraries and frameworks.

===========================================
5. how to disable a specific auto configuration class.
ans. we can use exclude attribute of annotation to exclude specific class.

(exclude={className})

===========================================
6. what is the different between and in spring boot.
ans. Map of the model object to view or template and make it human readable.
simply return the object and object data is directly written in HTTP response as JSON or XML.
= +

===========================================
7. what is different between an
ans. can be used with GET ,POST, PUT and many other request methods using the method attribution the annotation.

where as is only an extensions of with GET method which helps you to improve on clarity on request.

(value="/user/{userid}", method=RequestMethod.GET)
(value="/user/{userid}")

===========================================
8.what is different b/w an embedded container and a war ?

The main different between an embedded container and a war file that you can spring boot application as a jar from the command
prompt without setting up a web server

but to run a war file , you need to first set up a web server like tomcat which has servlet container and then you need to
deployee war file.
===========================================

9. what is the use of in Spring Boot.
ans. Spring has the provision of Profiles to keep the daparate configuation of environments.
Profiles provide a way to customize the behavior of an application based on different environments, such as development, testing, production, or specific deployment scenarios.

1. The annotation can be applied to classes, methods, or individual beans to associate them with specific profiles.
2. Profiles can be defined using the spring.profiles.active property in the application.properties or application.yml file,
or by setting the SPRING_PROFILES_ACTIVE environment variable.
3.("profileName") - ("Developement"),("production") ,("Testing")

===========================================
10. what is spring acutator what are its advantages.
ans. Provides special feature to monitor and manage your application when you push it to production.

for example - check health beans, information, performation ,shutdown application.



org.springframework.boot
spring-boot-starter-actuator


management.endpoints.web.exposure.include=*

======================================
11. what is use of annotation in spring framework.
Qualifier =The annotation is used along with to resolve ambiguity when multiple beans of
the same type are present. It allows you to specify a specific bean to be autowired when multiple beans of
the same type are available.


public PizzaController(("vegPizza") Pizza pizza) {
super();
this.pizza = pizza;
}

===========================================

"

16/06/2023

new features in Core Java 1 page.

16/12/2022

// write a program to find square's sum of odd number. Using java 8 feture Stream API.

package interview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo {

public void SquareSumofOddNumber(List list) {
int sum;
sum = list.stream().filter(n -> n % 2 == 1).map(n -> n * n).reduce(0, Integer::sum);

System.out.println("sum =" + sum); // 1x1 + 3x3 + 5x5= 1+9+25= 35 (sum=35)
}

public static void main(String[] args) {
Demo d = new Demo();

List list = new ArrayList(Arrays.asList(1, 2, 3, 4, 5));
d.SquareSumofOddNumber(list);

}
}
//output sum = 35

28/11/2022

Map Collection

27/11/2022

Map Collection Type in Java.

27/11/2022

Collection in Java.

12/04/2021

//Bubble Sort logic for string or program to Sort String in java as ascending order.
import java.util.*;
public class Bubble_Sort_ForString{

public static void main(String args[]){
String a[]={"deepak" ,"amit","deepash","vironika","rahul"};
String temp;

for(int i=0;i

12/04/2021

// in Java
public class Bubble_Sort{

public static void main(String args[]){
int a[]={36,19,29,12,5};
int temp;

for(int i=0;i

Want your business to be the top-listed Computer & Electronics Service in Pune?
Click here to claim your Sponsored Listing.

Website

Address


Pune