package com.pse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Java8Test {
static List<Employee> employeeList = new ArrayList<Employee>();
static {
employeeList.add(new Employee(111, "Jiya Brein", 32, "Female", "HR", 2011, 25000.0));
employeeList.add(new Employee(122, "Paul Niksui", 25, "Male", "Sales And Marketing", 2015, 13500.0));
employeeList.add(new Employee(133, "Martin Theron", 29, "Male", "Infrastructure", 2012, 18000.0));
employeeList.add(new Employee(144, "Murali Gowda", 28, "Male", "Product Development", 2014, 32500.0));
employeeList.add(new Employee(155, "Nima Roy", 27, "Female", "HR", 2013, 22700.0));
employeeList.add(new Employee(166, "Iqbal Hussain", 43, "Male", "Security And Transport", 2016, 10500.0));
employeeList.add(new Employee(177, "Manu Sharma", 35, "Male", "Account And Finance", 2010, 27000.0));
employeeList.add(new Employee(188, "Wang Liu", 31, "Male", "Product Development", 2015, 34500.0));
employeeList.add(new Employee(199, "Amelia Zoe", 24, "Female", "Sales And Marketing", 2016, 11500.0));
employeeList.add(new Employee(200, "Jaden Dough", 38, "Male", "Security And Transport", 2015, 11000.5));
employeeList.add(new Employee(211, "Jasna Kaur", 27, "Female", "Infrastructure", 2014, 15700.0));
employeeList.add(new Employee(222, "Nitin Joshi", 25, "Male", "Product Development", 2016, 28200.0));
employeeList.add(new Employee(233, "Jyothi Reddy", 27, "Female", "Account And Finance", 2013, 21300.0));
employeeList.add(new Employee(244, "Nicolus Den", 24, "Male", "Sales And Marketing", 2017, 10700.5));
employeeList.add(new Employee(255, "Ali Baig", 23, "Male", "Infrastructure", 2018, 12700.0));
employeeList.add(new Employee(266, "Sanvi Pandey", 26, "Female", "Product Development", 2015, 28900.0));
employeeList.add(new Employee(277, "Anuj Chettiar", 31, "Male", "Product Development", 2012, 35700.0));
}
public static void main(String[] args) {
// Query 3.1 : How many male and female employees are there in the organization?
employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.2 : Print the name of all departments in the organization?
employeeList.stream().map(Employee::getDepartment).distinct().forEach(System.out::println);
System.out.println();
// Query 3.3 : What is the average age of male and female employees?
employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingInt(Employee::getAge)))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.4 : Get the details of highest paid employee in the organization?
Optional<Employee> employee = employeeList.stream()
.collect(Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary)));
System.out.println(employee);
// Or
Optional<Employee> employee2 = employeeList.stream().max(Comparator.comparingDouble(Employee::getSalary));
System.out.println(employee2);
System.out.println();
// Query 3.5 : Get the names of all employees who have joined after 2015?
employeeList.stream()
.filter(e -> e.getYearOfJoining() > 2015)
.map(Employee::getName)
.forEach(System.out::println);
System.out.println();
// Query 3.6 : Count the number of employees in each department?
employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.counting()))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.7 : What is the average salary of each department?
employeeList.stream()
.collect(Collectors.groupingBy(Employee::getDepartment, Collectors.averagingDouble(Employee::getSalary)))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.8 : Get the details of youngest male employee in the product
// development department?
Optional<Employee> youngestMaleEmployeeInProductDevelopmentWrapper = employeeList.stream()
.filter(e -> e.getGender().equals("Male") && e.getDepartment().equals("Product Development"))
.min(Comparator.comparingInt(Employee::getAge));
System.out.println(youngestMaleEmployeeInProductDevelopmentWrapper);
// Or
Optional<Employee> youngestMaleEmployeeInProductDevelopmentWrapper2 = employeeList.stream()
.filter(e -> e.getGender().equals("Male") && e.getDepartment().equals("Product Development"))
.collect(Collectors.minBy(Comparator.comparingInt(Employee::getAge)));
System.out.println(youngestMaleEmployeeInProductDevelopmentWrapper2);
System.out.println();
// Query 3.9 : Who has the most working experience in the organization?
Optional<Employee> mostWorkingExperienceEmployee = employeeList.stream()
.min(Comparator.comparingInt(Employee::getYearOfJoining));
System.out.println(mostWorkingExperienceEmployee);
// OR
Optional<Employee> mostWorkingExperienceEmployee2 = employeeList.stream()
.sorted(Comparator.comparingInt(Employee::getYearOfJoining))
.findFirst();
System.out.println(mostWorkingExperienceEmployee2);
System.out.println();
// Query 3.10 : How many male and female employees are there in the sales and
// marketing team?
employeeList.stream()
.filter(e -> e.getDepartment().equals("Sales And Marketing"))
.collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.11 : What is the average salary of male and female employees?
employeeList.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getSalary)))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.12 : List down the names of all employees in each department?
employeeList.stream().collect(Collectors.groupingBy(Employee::getDepartment)).entrySet().forEach(e -> {
System.out.println(e.getKey());
for (Employee emp : e.getValue()) {
System.out.println(emp);
}
});
System.out.println();
// Query 3.13 : What is the average salary and total salary of the whole
// organization?
DoubleSummaryStatistics doubleSummaryStatics = employeeList.stream()
.collect(Collectors.summarizingDouble(Employee::getSalary));
System.out.println(doubleSummaryStatics.getAverage());
System.out.println(doubleSummaryStatics.getSum());
System.out.println();
// Query 3.14 : Separate the employees who are younger or equal to 25 years from
// those employees who are older than 25 years.
employeeList.stream()
.collect(Collectors.partitioningBy(e -> e.getAge() > 25))
.entrySet()
.forEach(System.out::println);
System.out.println();
// Query 3.15 : Who is the oldest employee in the organization? What is his age
// and which department he belongs to?
employeeList.stream().max(Comparator.comparingInt(Employee::getAge)).ifPresent(System.out::println);
//Query 3.16 : Sort employee in descending order based on salary
employeeList.stream().sorted(Comparator.comparingDouble(Employee::getSalary).reversed()).toList();
//Query 3.17 : Find 10th highest salary employee
employeeList.stream()
.sorted(Comparator.comparingDouble(Employee::getSalary).reversed())
.skip(9)
.limit(1)
.toList();
//Find max number in the list of integers
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
Integer maxObj = numbers.stream().collect(Collectors.maxBy(Comparator.naturalOrder())).get();
//OR
Integer maxObj2 = numbers.stream().max(Comparator.naturalOrder()).get();
System.out.println(maxObj);
System.out.println(maxObj2);
//OR
Integer i = numbers.stream().sorted(Comparator.reverseOrder()).findFirst().get();
System.out.println(i);
//How do you find the frequency of element in an array
List<String> listOfStrings = Arrays.asList("Pen", "Eraser", "Note Book", "Pen", "Pencil", "Pen", "Note Book", "Pencil");
Map<String, Long> elementCountMap = listOfStrings.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
//Or
IntStream.of(1, 2, 3, 2, 1, 2, 3, 4, 2, 2, 2)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.forEach(System.out::println);
//Sum number in a list
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
Integer sum = integers.stream().mapToInt(Integer::intValue).sum();
//OR
integers.stream().collect(Collectors.summingInt(Integer::intValue));
//Or
integers.stream().reduce(0, Integer::sum);
//Find only duplicate numbers
List<Integer> numbers3 = Arrays.asList(new Integer[]{1, 2, 1, 3, 4, 4});
numbers.stream().filter(i2 -> Collections.frequency(numbers, i2) > 1)
.collect(Collectors.toSet()).forEach(System.out::println);
//OR
List<Integer> duplicates = IntStream.of(1, 2, 3, 2, 1, 2, 3, 4, 2, 2, 2)
.boxed()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.filter(p -> p.getValue() > 1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
System.out.println(duplicates);
//Find distinct numbers in an array
int[] distinctArr = Arrays.stream(new int[]{1, 2, 3, 2, 1, 2, 3, 4, 2, 2, 2}).distinct().toArray();
Arrays.stream(distinctArr).forEach(System.out::println);
//Find distinct numbers in a list
Arrays.asList(new Integer[]{1, 2, 3, 2, 1, 2, 3, 4, 2, 2, 2}).stream()
.distinct().forEach(System.out::println);
//Sort a map based on value
Map<String, Integer> wordCounts = new HashMap<>();
wordCounts.put("USA", 100);
wordCounts.put("jobs", 200);
wordCounts.put("software", 50);
wordCounts.put("technology", 70);
wordCounts.put("opportunity", 200);
Map<String, Integer> sortedByCount = wordCounts.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
}
}
class Employee {
int id;
String name;
int age;
String gender;
String department;
int yearOfJoining;
double salary;
public Employee(int id, String name, int age, String gender, String department, int yearOfJoining, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.yearOfJoining = yearOfJoining;
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getDepartment() {
return department;
}
public int getYearOfJoining() {
return yearOfJoining;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Id : " + id + ", Name : " + name + ", age : " + age + ", Gender : " + gender + ", Department : "
+ department + ", Year Of Joining : " + yearOfJoining + ", Salary : " + salary;
}
}
Program(m)ing is so easy!
AddThis
Thursday, 5 September 2024
Solving real time queries using java 8 features stream api with examples
Monday, 11 September 2023
100 AWS Services in Just One Line Each
100 AWS Services in Just One Line Each
- Amazon EC2: Virtual servers in the cloud.
- Amazon S3: Scalable object storage service.
- Amazon RDS: Managed relational database service.
- AWS Lambda: Serverless computing service.
- Amazon VPC: Virtual private cloud for network isolation.
- Amazon Route 53: Scalable DNS and domain registration service.
- Amazon CloudFront: Global content delivery network (CDN)
- AWS Elastic Beanstalk: Platform-as-a-Service (PaaS) for deploying applications.
- Amazon SNS: Pub/Sub messaging and mobile notifications service.
- Amazon SQS: Managed message queuing service.
- AWS Step Functions: Serverless workflow orchestration.
- Amazon DynamoDB: Managed NoSQL database service.
- AWS Fargate: Serverless compute engine for containers.
- Amazon ECR: Docker container registry.
- Amazon ECS: Container orchestration service.
- AWS CodeStar: Managed software development service.
- AWS CodeCommit: Private Git repository hosting.
- AWS CodeBuild: Managed build service.
- AWS CodeDeploy: Automated deployment service.
- AWS CodePipeline: Continuous delivery service.
- Amazon API Gateway: Create, publish, and manage APIs using this service.
- AWS AppConfig: Feature flag and application configuration service.
- Amazon Sagemaker: Managed machine learning platform.
- Amazon Comprehend: Natural language processing service.
- Amazon Lex: Conversational interfaces for applications.
- Amazon Polly: Text-to-speech service.
- Amazon Rekognition: Image and video analysis service.
- Amazon Textract: OCR and text extraction service.
- AWS Translate: Real-time text translation service.
- AWS Transcribe: Automatic speech recognition service.
- Amazon Connect: Cloud-based contact center service.
- Amazon WorkSpaces: Managed, secure Desktop-as-a-Service (DaaS).
- Amazon AppStream 2.0: Application streaming service.
- AWS Organizations: Centralized management of AWS accounts.
- AWS Control Tower: Automated landing zone setup and governance.
- AWS Config: Track resource inventory and changes.
- AWS CloudTrail: API activity logging and auditing.
- Amazon CloudWatch: Monitoring and observability for AWS resources.
- AWS Trusted Advisor: Infrastructure optimization recommendations.
- AWS Personal Health Dashboard: Personalized view of AWS service health.
- AWS Security Hub: Centralized view of security and compliance.
- Amazon Macie: Sensitive data discovery and protection.
- AWS Shield: Managed DDoS protection.
- AWS WAF: Web application firewall.
- Amazon GuardDuty: Continuous security monitoring and threat detection.
- AWS Key Management Service (KMS): Create and manage encryption keys.
- Amazon Inspector: Automated security assessment service.
- AWS Certificate Manager: Provision and manage SSL/TLS certificates.
- Amazon Cognito: User and identity management.
- AWS Directory Service: Managed Microsoft Active Directory.
- Amazon Elastic File System (EFS): Fully managed file storage service.
- Amazon Elastic Block Store (EBS): Block-level storage volumes for EC2 instances.
- AWS Storage Gateway: Hybrid cloud storage service.
- Amazon FSx: Fully managed file systems (Windows & Lustre).
- AWS Glue: Managed ETL (extract, transform, load) service.
- Amazon Redshift: Fully managed data warehouse service.
- Amazon EMR: Managed big data processing service.
- AWS Data Pipeline: Data movement and transformation service.
- AWS Lake Formation: Data lake management service.
- Amazon Athena: Serverless, interactive query service for data in S3.
- Amazon Kinesis: Real-time data streaming and analytics.
- Amazon Managed Streaming for Apache Kafka (MSK): Fully managed Apache Kafka service.
- AWS IoT Core: Managed IoT service for device connectivity and control.
- AWS IoT Device Management: Device organization and tracking for IoT.
- AWS IoT Analytics: IoT data analysis and processing.
- AWS IoT Greengrass: Local compute, messaging, and sync for IoT devices.
- AWS IoT Events: Event detection and response for IoT applications.
- AWS IoT SiteWise: Industrial data collection and monitoring.
- Amazon Elastic Kubernetes Service (EKS): Managed Kubernetes service.
- AWS Batch: Batch computing service for large-scale workloads.
- AWS Outposts: Extend AWS infrastructure and services to on-premises.
- AWS Snowball: Data transfer device for large-scale migrations.
- AWS Snowmobile: Exabyte-scale data transfer service.
- AWS Direct Connect: Dedicated network connection to AWS.
- AWS VPN: Virtual private network to securely access AWS resources.
- Amazon QuickSight: Business intelligence and visualization service.
- AWS Backup: Centralized backup service for AWS resources.
- AWS License Manager: Software license management service.
- AWS Service Catalog: Service catalogue for organizing and governing resources.
- Amazon Managed Blockchain: Managed blockchain network service.
- Amazon Quantum Ledger Database (QLDB): Ledger database with verifiable history.
- Amazon Elastic Inference: GPU-powered deep learning inference acceleration.
- Amazon Lightsail: Simplified compute, storage, and networking for basic applications.
- AWS Ground Station: Satellite data processing and analysis service.
- AWS AppConfig: Application configuration and feature flag management.
- AWS Chatbot: Monitor and interact with AWS resources via chat.
- AWS Cost Explorer: Visualize, understand, and manage AWS costs.
- AWS Marketplace: Marketplace for third-party software and services.
- AWS Migration Hub: Centralized service for application migration.
- AWS PrivateLink: Private access to services hosted on AWS.
- Amazon Elastic Transcoder: Media file transcoding service.
- AWS App Runner: Automatically build, deploy, and scale containerized applications.
- AWS Proton: Managed application delivery service for container and serverless applications.
- AWS RoboMaker: Robotics application development and simulation service.
- AWS Well-Architected Tool: Review workloads against best practices.
- AWS X-Ray: Distributed application tracing and analysis.
- Amazon Honeycode: Build web and mobile applications without programming.
- Amazon Chime: Communications service for meetings and chat.
- Amazon Pinpoint: Customer engagement and communication service.
- Amazon Kendra: Intelligent search service powered by machine learning.
Wednesday, 7 April 2021
LDAP / LDIF fields / attributes meaning
LDAP attributes to field names
LDAP Attributes | Field Names |
---|---|
buildingname | Building |
c | Country |
cn | Full Name |
co | Country |
comment | Comment |
commonname | Full Name |
company | Company |
description | Description |
distinguishedname | Distinguished Name |
dn | Distinguished Name |
department | Department |
displayname | Full Name |
facsimiletelephonenumber | Facsimile |
fax | Facsimile |
friendlycountryname | Country |
givenname | First Name |
homephone | Home Telephone |
homepostaladdress | Home Address |
info | Information |
initials | Middle Initial |
ipphone | IP Telephone |
l | City |
Email Address | |
mailnickname | User ID |
rfc822mailbox | Email Address |
mobile | Mobile Telephone |
mobiletelephonenumber | Mobile Telephone |
name | Full Name |
othertelephone | Other Telephone |
ou | Organizational Unit |
pager | Pager |
pagertelephonenumber | Pager |
physicaldeliveryofficename | Office |
postaladdress | Address |
postalcode | Zip Code |
postofficebox | Post Office Box |
samaccountname | User ID |
serialnumber | Serial Number |
sn | Last Name |
surname | Last Name |
st | State |
stateorprovincename | State |
street | Street |
streetaddress | Street |
telephonenumber | Telephone |
title | Title |
uid | User ID |
url | Other Web Page |
userprincipalname | User ID |
wwwhomepage | Main Web Page |
Field names to LDAP attributes
Field Names | LDAP Attributes |
---|---|
Address | postaladdress |
Address, Home | homepostaladdress |
Building | buildingname |
City | l |
Comment | comment |
Company | company |
Country | cco friendlycountryname |
Department | department |
Description | description |
Distinguished Name | distinguishedname dn |
Email Address | mailrfc822mailbox |
Facsimile | facsimiletelephonenumberfax |
Information | info |
Middle Initial | initials |
Name, First | givenname |
Name, Full | displayname name cn commonname name |
Name, Last | snsurname |
Office | physicaldeliveryofficename |
Organizational Unit | ou |
Pager | pager pagertelephonenumber |
Post Office Box | postofficebox |
Serial Number | serialnumber |
State | st stateorprovincename |
Street | street streetaddress |
Telephone | telephonenumber |
Telephone, Home | hometelephone |
Telephone, IP | ipphone |
Telephone, Mobile | mobile mobiletelephonenumber |
Telephone, Other | othertelephone |
Title | title |
User ID | samaccountname uid userprincipalname mailnickname |
Web Page, Main | wwwhomepage |
Web Page, Other | url |
Zip Code | postalcode |
Solving real time queries using java 8 features stream api with examples
package com.pse; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java...
-
In java 8 default capacity of ArrayList is 0 until we add at least one object into the ArrayList object (You can call it lazy initializati...
-
Our Browser is showing a lot of content and there’s not a trace of it in index.html. How is this happening? What’s with this weird <a...
-
When I first started down the path of CSS preprocessors I was overwhelmed by the different options (sass, less, stylus), the differences in...