JAVA - Coding
JAVA - Coding
stream()
.filter(e -> e.getAge() > 40) // filter employees with age > 40
.map(Employee::getName) // obtain each employee's name
.forEach(System.out::println); // finally, print it
class Employee {
private String name;
private int age;
private List<Address> addresses;
//getter and setter
}
class Address {
private String city;
private String state;
private String country;
//getter and setter
}
Using java 8 filter I want to print all employees who are having city starting with P
Distinct Strings
Collection<String> list = Arrays.asList("A", "B", "C", "D", "A", "B", "C");
Distinct integers
List<Integer> input = Arrays.asList(3,12,11,45,12,5,3,9,20,11,3);
System.out.print("Input:");
input.forEach(num -> System.out.print(num+" "));
System.out.println();
System.out.print("Output:");
List<Integer> distinctInput = input.stream().distinct().collect(Collectors.toList());
distinctInput.forEach(num -> System.out.print(num+" "));
##=s+I; Add the sum till 50
Int s=0;
For(int I =0;i<50;i++){
S = s+I;
IntStream.rangeClosed(1, 8)
.map(i -> 2 * i - 1)
.forEach(System.out::println);
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.*;
import java.util.Optional;
int sum1 = 0;
for (int value : integers) {
sum1 += value;
int s=0;
for(int i =0;i<50;i++){
s=s+i;
Input - 1
Assume you are given a list of numbers like {1,7,8,9,5,2,36,4,78,222,24,9}
Answers
1) Given a list of numbers, return the sum of all numbers.
output
2) Given a list of numbers, return the average of all numbers
output
3) Given a list of numbers, square them and filter the numbers which are
greater 100 and then find the average of them
If we look at the numbers, only 36, 78, 222, 24 squares are greater than 100.
output
4) Given a list of numbers, return the even and odd numbers separately
Approach: We can use filter() and Collectors.toList() to get both even and odd
numbers as two separate lists.
Even Numbers:
output
Odd Numbers
output
5) Given a list of numbers, find out all the numbers starting with 2
output
6) Given a list of numbers, print the duplicate numbers
output
duplicates: [9]
duplicates: [9]
Approach: Using max() and min() we can get maximum and minimum values from
a list along with Comparator.comparing().
Maximum Value:
output
Minimum Value:
output
Minimum Value: 1
8) Given a list of numbers, sort them in ASC and DESC order and print
ASC Order
output
DESC Order
output
9) Given a list of numbers, return the first 5 elements and their sum
First 5 Elements
output
First 5 Elements Sum
output
first5elementsSum: 30
10) Given a list of numbers, skip the first 5 numbers and return the sum of the
remaining numbers
output
Cubes: [1, 343, 512, 729, 125, 8, 46656, 64, 474552, 10941048, 13824, 729]
Normal string scan is possible using scnnaer , when I add int to String ,we need to use
bufferedreader
import java.util.*;
import java.io.*;
import java.lang.*;
int n=Integer.parseInt(br.readLine());
double d=Double.parseDouble(br.readLine());
String name=br.readLine();
System.out.println("String: "+name);
System.out.println("Double: "+d);
System.out.println("Int: "+n);
/** Scanner scan = new Scanner(System.in);
int i=0; ;
Double d = 0.0;
// String s = "";
// Write your code here.
while (scan.hasNextInt()) {
i= scan.nextInt();
}
while (scan.hasNextDouble()) {
d= scan.nextDouble();
}
String s = "";
while(scan.hasNext()){
s = s.concat(scan.next());
if(scan.hasNext()){
s = s.concat(" ");
}
}
//String s = scan.nextLine();
System.out.println("String: " + s );
System.out.println("Double: " + d);
System.out.println("Int: " + i);
scan.close();
**/
}
}
String s1=sc.next();
int x=sc.nextInt();
//Complete this line
System.out.printf("%-15s%03d%n",s1,x);
• Good knowledge of Java, Dependency Injection (Spring), Hibernate and Web Services.
Working on new enhancements in Aggrisk, which is used for displaying Profit and Loss, and Risk
data in Live , intraday and EOD mode of foreign exchange and local market using Ag-grid and
following the regulatory components.
Eod we will send an email to team and BOD regd, the trade which are completed ,@risk , what si
the profit n loss incurred for the day .
Portfolio maintenance – IAM security and Identity management – users , roles, access to
websites, copy from emp ,
PAM (Privileged access management ), IGA identity governance solution.
, Spectre,
Service Now request – application – filter out the message in core dir queue many request
we will get like iam, core dir,
1. Webservice sampl
2. Docker file template
3. Rest sample
4. Java 8,9 ,11, 17 features
Java 9
1.ArrayList to toArray before we use for each-
2. New utility methods in String class – repeat,isLines, isBlank – check for
whitespaces and empty string – return true,
stripLeading(): It is used to remove the white space at beginning of string
Optional.isEmpty(): This method returns true if the value of any object is null and
else returns false.
TimeUnit Conversion: This method is used to convert the given time to a unit like
DAY, MONTH, YEAR, and for time too
System.out.println(c1.convert(Duration.ofDays(3)));
Output:4320
Hope you understand above example like wise you can convert minutes to
hours ,seconds to days and many more scenarios which ever is possible.
It has introduced the following methods for reading and writing to/from the files
Files.writeString(Path.of(“D:\test.txt”), “AnilAppana”)
Using above code we are able to insert some text into file at that location but what
if we want to read data from it check below snippet
String s1 = Files.readString(Path.of("D:\\test.txt"));
Console Output:
isSameFile(): This method is used to know whether two paths locate the same file
or not, isSameFile() returns boolean value if files are sae it returns true if files are
no matched it returns false
System.out.println(str);
System.out.println(str.lines().collect(Collectors.toList())
}}
Console Output:
Before Java 11, if we have private members in our code then the compiler creates
accessibility-broadening bridge methods that increase the size of the deployed
applications and may lead to confusion. That’s why Java improved nest-based
access control.
Java 11 allows classes and interfaces to be nested within each other. These nested
type can be private fields, methods, and constructors
7.HTTP Client:
The new HTTP client from the java.net.http package was introduced in Java 9. It
has now become a standard feature in Java 11.
The new HTTP API improves overall performance and provides support for both
HTTP/1.1 and HTTP/2
I am running a series, where I share interesting topics that every developer should know about.
( To follow - https://lnkd.in/d62YuzpW )
1. @SpringBootApplication
- This annotation is a combination of @Configuration, @EnableAutoConfiguration, &
@ComponentScan.
- It is used to configure a Spring Boot application, including its dependencies, beans, and
configuration.
2. @RestController
- This annotation is used to create a RESTful web service that is capable of handling HTTP
requests and sending HTTP responses.
3. @Autowired
- This annotation is used to inject dependencies into a class or a bean.
4. @Service
- This annotation is used to indicate that a class is a service layer component.
5. @Repository
- This annotation is used to indicate that a class is a data access layer component.
6. @Controller
- This annotation is used to indicate that a class is a web controller.
7. @Configuration
- This annotation is used to define a class as a configuration class. It is used to configure beans
and other settings in the Spring application context.
8. @Value
- This annotation is used to inject values from properties files or other sources into a bean.
9. @ComponentScan
- This annotation is used to specify the base package(s) for component scanning.
10. @ConditionalOnProperty
- This annotation is used to conditionally enable/disable certain features based on properties.
11. @Scheduled
- This annotation is used to schedule tasks in a Spring Boot application.
12. @EnableScheduling
- This annotation is used to enable scheduling in a Spring Boot application.
13. @Transactional
- This annotation is used to indicate that a method or a class should be executed within a
transaction.
14. @Cacheable
- This annotation is used to cache the results of expensive method calls.
15. @PostConstruct
- This annotation is used to annotate a method that should be executed after a bean is initialized.
16. @PreDestroy
- This annotation is used to annotate a method that should be executed before a bean is
destroyed.
17. @ExceptionHandler
- This annotation is used to handle exceptions in a RESTful web service.
18. @ResponseBody
- This annotation is used to indicate that a method should return the HTTP response body.
19. @PathVariable
- This annotation is used to extract a variable from a URL path.
20. @RequestBody
- This annotation is used to extract the request body from an HTTP request.
21. @EnableConfigurationProperties
- This annotation is used to enable configuration properties binding to a bean.
22. @Bean: This annotation is used to define a bean in the Spring application context.
23. @ResponseStatus
- This annotation is used to specify the HTTP response status code for an exception.
24. @ConfigurationProperties
- This annotation is used to bind externalized configuration properties to a bean.
👉 Doc by jrebel