Task - Collections - Employee List #45
Replies: 63 comments 15 replies
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
package March4Collections; import java.util.; class Employee { } public class DriverClass { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Task-Collections-Employee ListEmployeeList.javaimport java.util.ArrayList;
import java.util.Scanner;
class Employee
{
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public boolean isEqual(Employee other)
{
if(this==null || other==null)
return false;
else
return (this.getEmployeeId()==other.employeeId && this.getFirstName().equals(other.firstName));
}
}
public class EmployeeList {
public static void main(String[] args) {
long employeeId=0;String firstName="";String lastName="";
Scanner sc=new Scanner(System.in);
System.out.println("How many employee details you want to add ");
int n=sc.nextInt();
Employee e=null,e1;
ArrayList<Employee> list=new ArrayList<>();
for(int i=0; i<n; i++)
{
System.out.println("Enter the employee id");
employeeId=sc.nextLong();
System.out.println("Enter the firstName");
firstName=sc.next();
System.out.println("Enter the lastName");
lastName=sc.next();
e=new Employee(employeeId, firstName, lastName);
list.add(e);
}
e1=new Employee(2,"maha","pattan");
list.forEach(System.out::println);
for(int i=0; i<n; i++){
System.out.println("Employee "+(i+1)+" "+" Matched(true/false) : "+list.get(i).isEqual(e1));
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
class Employee1 {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee1(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
boolean isequals(Employee1 o) {
if (this == o || (this.employeeId == o.employeeId && this.firstName.equals(o.firstName)))
return true;
else
return false;
}
}
public class Discussion45 {
public static void main(String[] args) {
List<Employee1> employees = new ArrayList<>();
employees.add(new Employee1(1, "Karan", "Seth"));
employees.add(new Employee1(2, "Shivam", "Khanna"));
employees.add(new Employee1(3, "Sagar", "Chaurasia"));
employees.add(new Employee1(1, "Karan", "Seth"));
employees.forEach(System.out::println);
System.out.println();
System.out.println(employees.get(0).isequals(employees.get(3)));
}
} |
Beta Was this translation helpful? Give feedback.
-
package Discussion45;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(long employeeId, String firstName, String lastName) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public int hashCode() {
return Objects.hash(employeeId, firstName, lastName);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return employeeId == other.employeeId && Objects.equals(firstName, other.firstName)
&& Objects.equals(lastName, other.lastName);
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
public class EmployeeList {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Employee> emp=new ArrayList<Employee>();
emp.add(new Employee(1, "Jack", "william"));
emp.add(new Employee(2, "Bill", "Thomas"));
emp.add(new Employee(1, "Jack", "william"));
emp.add(new Employee(3, "Tom", "Thomas"));
emp.forEach(System.out::println);
System.out.println(emp.get(0).equals(emp.get(2)));
}
} |
Beta Was this translation helpful? Give feedback.
-
package java_programs;
import java.util.ArrayList;
import java.util.List;
class EmployeesData {
protected long employeeId;
protected String firstName;
protected String lastName;
public EmployeesData(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
boolean isEqual(EmployeesData other) {
if (this.employeeId != other.employeeId && this.firstName != other.firstName)
return false;
else
return true;
}
}
public class BasicCollection {
public static void main(String[] args) {
EmployeesData employees1 = new EmployeesData(123, "john", "jack");
EmployeesData employees2 = new EmployeesData(132, "jack", "John");
EmployeesData employees3 = new EmployeesData(143, "james", "johnJack");
List<EmployeesData> employeeList = new ArrayList<>();
employeeList.add(employees1);
employeeList.add(employees2);
employeeList.add(employees3);
employeeList.forEach(System.out::println);
employeeList.forEach(e -> System.out.println(e));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class D45 {
public static void main(String[] args) {
ArrayList<Employee3> emp = new ArrayList<Employee3>();
Employee3 e1 = new Employee3(101, "John", "Adams");
Employee3 e2 = new Employee3(102, "James", "Williams");
Employee3 e3 = new Employee3(103, "Jamie", "Doe");
Employee3 e4 = new Employee3(101, "John", "Adams");
emp.add(e1);
emp.add(e2);
emp.add(e3);
emp.add(e4);
emp.forEach(System.out::println);
System.out.println(e1.isEqual(e3));
System.out.println(e1.isEqual(e4));
}
}
class Employee3 {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee3(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee employeeId: " + employeeId + "\nfirstName: " + firstName + "\nlastName: " + lastName + "\n";
}
public boolean isEqual(Employee3 other) {
if (other == null)
return false;
if (!(other instanceof Employee3))
return false;
return (this.employeeId == other.employeeId && (this.firstName.equals(other.firstName)));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
class Employee45 {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee45(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isEqual(Employee45 e) {
if (e == null)
return false;
if (this == e)
return true;
if (!(e instanceof Employee45))
return false;
return this.employeeId == e.employeeId && this.firstName == e.firstName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
public class discussion45 {
public static void main(String[] args) {
Employee45 e1 = new Employee45(101, "Amit", "Saini");
Employee45 e2 = new Employee45(102, "Satyam", "Kumar");
Employee45 e3 = new Employee45(103, "Kareena", "Rao");
List<Employee45> employees = new ArrayList<>();
employees.add(e1);
employees.add(e2);
employees.add(e3);
employees.forEach(System.out::println);
System.out.println(e1.isEqual(e2));
System.out.println(e1.isEqual(e4));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
class EmployeesData {
protected long employeeId;
protected String firstName;
protected String lastName;
public EmployeesData(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
boolean isEqual(EmployeesData other) {
if (this.employeeId != other.employeeId && this.firstName != other.firstName)
return false;
else
return true;
}
}
public class d45 {
public static void main(String[] args) {
EmployeesData e1 = new EmployeesData(1, "ABC", "DEF");
EmployeesData e2 = new EmployeesData(2, "PQR", "STU");
EmployeesData e3 = new EmployeesData(3, "XYZ", "KLM");
List<EmployeesData> employeeList = new ArrayList<>();
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
employeeList.forEach(System.out::println);
employeeList.forEach(e -> System.out.println(e));
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task45 {
public static void main(String[] args) {
List<Emps> e = new ArrayList<>();
Emps e1 = new Emps(1122, "rahul", "khanna");
Emps e2 = new Emps(1345, "rohan", "singh");
Emps e3 = new Emps(1022, "john", "smith");
e.add(e1);
e.add(e2);
e.add(e3);
e.forEach(System.out::println);
System.out.println(e1.isEqual(e2));
}
}
class Emps {
protected long employeeId;
protected String firstName;
protected String lastName;
public Emps(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Emps [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public boolean isEqual(Emps other) {
if (this.employeeId != other.employeeId && this.firstName != other.firstName && this.lastName != other.lastName) {
return false;
} else {
return true;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
class employee1 {
protected long employeeId;
protected String firstname;
protected String lastname;
public employee1(long employeeId, String firstname, String lastname) {
this.employeeId = employeeId;
this.firstname = firstname;
this.lastname = lastname;
}
public long getemployee1Id() {
return employeeId;
}
public void setemployee1Id(long employee1Id) {
this.employeeId = employee1Id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public boolean isEqual(employee1 other) {
if (other == null || this == null) {
return false;
}
if (this.employeeId == other.employeeId && this.firstname == other.firstname) {
return true;
}
return false;
}
@Override
public String toString() {
return "employee1 [employee1Id=" + employeeId + ", firstname=" + firstname + ", lastname=" + lastname + "]";
}
}
public class Collections_45 {
public static void main(String[] args) {
List<employee1> e = new ArrayList<employee1>();
e.add(new employee1(1, "Riya", "Singhal"));
e.add(new employee1(2, "Raj", "Sharma"));
e.add(new employee1(1, "Riya", "Singhal"));
e.forEach(System.out::println);
System.out.println(e.get(0).isEqual(e.get(1)));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class D45 {
public static void main(String[] args) {
ArrayList<Employee3> emp = new ArrayList<Employee3>();
Employee3 e1 = new Employee3(101, "John", "Adams");
Employee3 e2 = new Employee3(102, "James", "Williams");
Employee3 e3 = new Employee3(103, "Jamie", "Doe");
Employee3 e4 = new Employee3(101, "John", "Adams");
emp.add(e1);
emp.add(e2);
emp.add(e3);
emp.add(e4);
emp.forEach(System.out::println);
System.out.println(e1.isEqual(e3));
System.out.println(e1.isEqual(e4));
}
}
class Employee3 {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee3(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee employeeId: " + employeeId + "\nfirstName: " + firstName + "\nlastName: " + lastName + "\n";
}
public boolean isEqual(Employee3 other) {
if (other == null)
return false;
if (!(other instanceof Employee3))
return false;
return (this.employeeId == other.employeeId && (this.firstName.equals(other.firstName)));
}
} |
Beta Was this translation helpful? Give feedback.
-
package abcde;
import java.util.ArrayList;
public class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
boolean isEqual(Employee other)
{
if (other == null)
return false;
else if (this.getClass() != other.getClass())
return false;
else if (this == other)
return true;
else if ((this.employeeId == other.employeeId) && (this.firstName == other.firstName)
&& (this.lastName == other.lastName))
return true;
else
return false;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Employee> a= new ArrayList<Employee>();
Employee e= new Employee();
Employee e1= new Employee();
Employee e2= new Employee();
System.out.println(e.equals(e2));
a.add(e);
a.add(e1);
a.add(e2);
a.forEach(System.out::println);
}
} |
Beta Was this translation helpful? Give feedback.
-
Codeimport java.util.ArrayList;
import java.util.List;
class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public Employee(long employeeId, String firstName, String lastName) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public boolean isEqual(Employee e) {
if(e==null)
return false;
return this.employeeId == e.employeeId && this.firstName == e.firstName; }
}
public class Main {
public static void main(String[] args) {
List<Employee> employee = new ArrayList<Employee>();
employee.add(new Employee(103, "Shravan", "K V"));
employee.add(new Employee(103, "Shravan", "K V"));
employee.add(new Employee(101, "Pavan", "K S"));
employee.add(new Employee(104, "Anil", "K A"));
employee.forEach(t -> System.out.println(t));
System.out.println(employee.get(0).isEqual(employee.get(1)));
}
} |
Beta Was this translation helpful? Give feedback.
-
codeimport java.util.*;
class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee(long employeeId, String firstName, String lastName) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public boolean isEqual(Employee obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return ((this.employeeId == other.employeeId && this.firstName == other.firstName) && this.lastName == other.lastName);
}
}
public class Discussion45{
public static void main(String[] args) {
Employee e1 = new Employee(123, "RAM", "PANDEY");
Employee e2 = new Employee(123, "RAM", "PANDEY");
Employee e3 = new Employee(1234, "SHYAM", "SHARMA");
ArrayList<Employee> res = new ArrayList<>();
res.add(e1);
res.add(e2);
res.add(e3);
res.forEach(System.out::println);
if(e1.isEqual(e2)){
System.out.println("E1 and E2 are equal");
}else{
System.out.println("E1 and E2 are not Equal");
}
if(e1.equals(e3)){
System.out.println("E1 and E3 are Equals");
}else{
System.out.println("E1 and E3 are not Equal");
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Employee Classpublic class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public Employee(long employeeId, String firstName, String lastName) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
}
public long getEmployeeId() {
return employeeId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
public boolean isEqual(Employee E) {
if(this.employeeId != E.employeeId) {
return false ;
}
if(this.getClass() != E.getClass()) {
return false ;
}
if(this.firstName != E.firstName) {
return false ;
}
if(this.lastName != E.lastName) {
return false ;
}
return true;
}
}Driver Classimport java.util.*;
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee rakesh = new Employee(1 , "Rakesh" , "Rajvanshi");
Employee navneet = new Employee(2 , "Navneet" , "Singh") ;
Employee pankaj = new Employee(3 , "Pankaj" , "Jha");
List<Employee> empList = new ArrayList<>() ;
empList.add(rakesh) ;
empList.add(navneet) ;
empList.add(pankaj) ;
empList.forEach(System.out::println);
System.out.println(rakesh.isEqual(navneet));
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Define a class named
Employee. The skeleton of the Employee class is given below:Implement getters and setters. Override
toString()method forEmployeeclass.Define a method
isEqual(Employee other)in theEmployeeclass. The method returnstrueif this employee is the same as the other employee. Else, it returnsfalse.In the driver class, create three employee instances and add them to an ArrayList. Print the list using the
forEach()method.Beta Was this translation helpful? Give feedback.
All reactions