How do I search an ArrayList of Objects - java

I am creating an ArrayList of Accounts (an object) and the Account constructor is
public Account(String name, int accNum, int balance)
{
myName = name;
myAccountNum = accNum;
myBalance = balance;
}
I want to know how to check the ArrayList to determine if a given accountNumber exists in it, and if it does, return true
private static ArrayList<Account> accounts = new ArrayList<Account>();
My initial thought was this, but I do not think that this works
if(accounts.contains(tempAccNum))
{
//executes code that I have
}

For the Java ArrayList, contains performs an object equality comparison. To use .contains, you would need to
Implement the .equals() method for the Account class, and have it check only the this.myAccountNum property against the account number of the input Account.
Create a dummy Account with the desired account number to pass into contains.
A better method would involve evaluating an iterator, and checking the account numbers at each step. Here I assume that myAccountNum is a public property of the Account class.
Iterator<E> it = Accounts.iterator();
while (it.hasNext()) {
Account acc = it.next();
if(acc.myAccountNum == tempAccNum)
return true;
}
For ArrayList specifically, using .get with an index is not too bad:
for(int index = 0; index < Accounts.size(); ++index) {
if(Accounts.get(index).myAccountNum == tempAccNum)
return true;
For other List types, using indices can be very poor.
Ways to iterate over a list in Java

I will suggest you to make it simple by just implementing an equals() method for account class.
#Override
public boolean equals(Object o){
if(o==null)return false;
if(o.getClass()!=this.getClass())return false;
Account demo = (Account)o;
if(!demo.myName.equals(this.myName))return false;
if(demo.myAccountNum != this.myAccountNum)return false ;
if(demo.myBalance = this.myBalance)return false ;
}
then use contains method

First add getter for accNum in Account model
Then try this
public boolean containsAcc(int accno) {
for(int i=0;i<accounts.size();i++) {
if(accounts!= null && accounts.get(i).getMyAccountNum()==acno) {
return true;
}
}
return false;
}
Here getMyAccountNum() is the getter declared in Account model(shown below)
Then check this
if(containsAcc(tempAccNum))
{
//your code
}
Your Account model should be like this
public class Account {
String myName;
int myAccountNum;
int myBalance;
public Account(String name, int accNum, int balance)
{
this.myName = name;
this.myAccountNum = accNum;
this.myBalance = balance;
}
public int getMyAccountNum() {
return myAccountNum;
}
}

Related

Ordering the results of an file with ArrayList and compareTo

I have to order an arrayList that contains lines from a file by account ID and then by salary to get this result:
CuentaAhorro : 11111111A (Alicia) Saldo 111,11
CuentaAhorro : 12345678A (Lucas) Saldo 5100,00
CuentaCorriente: 22222222B (Peio) Saldo 222,22
CuentaAhorro : 33333333C (Isabel) Saldo 4433,33
CuentaCorriente: 33333333C (Isabel) Saldo 3333,33
CuentaAhorro : 87654321A (Asier) Saldo 3000,00
My arrayList calls the compareTo method from Bank.java.
public void ordenarCuentas() {
Collections.sort(cuentas);
}
The call is to the method compareTo in an abstract class called Account with the comparable interface:
#Override
public int compareTo(Cuenta unaCuenta) {
Cliente unTitular = unaCuenta.titular;
if(unTitular.toString().equals(unaCuenta.titular.toString()) == true) {
return 0;
// if(saldo < unaCuenta.saldo) {
// return -1;
// } else if (saldo > unaCuenta.saldo) {
// return 1;
// } else {
// return 0;
// }
}
return -1;
}
I need to check if the object 'Cuenta unaCuenta' passed as a parameter has the same account number as another and then sort by the amount of money in the account, however I am not sure how to get the condition right, as you can see, with the commented if I get the salary in the right descending order but not the account IDs.
The object Cuenta unaCuenta contains titular which contains account number and name.
The object Cliente unTitular contains the account number and name.
Could somebody lend me a hand please?
I am not able understand it very clearly because of language barrier
But if you have a arraylist you can call sort method on it an pass a comparator to get the desired sorting , something like below.
It is just to give you an idea
ArrayList<String> list = new ArrayList<>();
list.sort(new Comparator() {
#Override
public int compare(Object o1, Object o2) {
if(o1.account.equals(o2.account)) return 0;
return o1.amount - o2.amount;
}
});
as Lambda
ArrayList<String> list = new ArrayList<>();
list.sort((o1,o2) ->
if(o1.account.equals(o2.account)) return 0;
return o1.amount - o2.amount;
});
Thank you everyone for the comments. Next time i'll translate the Spanish code to English. I'll post my solution incase someone comes across this question.
(in my case I had to use a comparable interface and a compareTo method).
#Override
public int compareTo(Account anAccount) {
String b = this.title.toString();
Client aTitle = anAccount.title;
String c = aTitle.toString();
if(b.compareTo(c) == 0) {
if(balance == anAccount.balance) {
return 0;
} else if (balance < anAccount.balance) {
return 1;
} else {
return -1;
}
}
return b.compareTo(c);
}
As stated, I had to compare both object values first, if they are the same I then check the condition of the balance to change the order.
-1 = object is less than the parameter.
0 = when both objects are the same.
1 = the object is more than the parameter.
And I called the method from the Bank.java class with:
Collections.sort(cuentas);
Where cuentas is the ArrayList.

searching custom objects in arrays and arraylists in java

I have written an employee class which has a display method and the constructor.
public class employee {
public int empid;
public String name;
public employee(int id, String name){
empid = id;
this.name = name;
}
public void display (){
System.out.println("Employee id: " +empid +"\nEmployee name: "+name);
}
}
I then created three objects of employee and stored them in an array in the main class. I created an if block which will check user guess and print the employee details if he exists or will throw an exception if the data is not present. The if block was enclosed in an enhanced for loop which loops through the array.
public static void main(String[] args) {
// TODO code application logic here
employee priya = new employee (001, "Priya");
employee tamizh = new employee (002, "Tamizh");
employee hari = new employee (003, "hari");
employee[] list = new employee[3];
list[0] = priya;
list[1] = tamizh;
list[2] = hari;
int userGuess = 002;
for (employee l : list){
if (userGuess == l.empid)
{
l.display();
break;
}
else
{
throw new InputMismatchException ("employee doesnot exist");
}
}
}
}
The trouble is that the program throws the exception even if the guess is correct. I tried the int variable empid and then the String variable name, but both the == and .equals didn't work. I searched stackoverflow and the solution suggested was to override the hashcode and equals method in the employee class. I did that.
public class employee {
public int empid;
public String name;
public employee(int id, String name){
empid = id;
this.name = name;
}
public void display (){
System.out.println("Employee id: " +empid +"\nEmployee name: "+name);
}
#Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + empid;
result = prime * result + name.hashCode();
return result;
}
#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;
if (this.empid != other.empid)
return false;
if (this.name.equals(other.name))
return false;
return true;
}
}
Now when I give the input as 001, the code works fine. But for any other input (including the existing empids 002 and 003), the exception is being thrown.
What had I done wrong in the overriding? Also I don't understand the code I had written to override the two methods. Can someone explain the logic and where I went wrong? Thanks.
Edit: Thanks guys. I have realized my mistake in the for loop and I have edited it. It works perfectly now.
int userGuess = 002;
boolean found = false;
for (employee l : list){
if (userGuess == l.empid)
{
l.display();
found = true;
break;
}
}
if(found == false){
try{
throw new InputMismatchException ("employee doesnot exist");
}
catch(InputMismatchException e){
System.out.println("Employee doesnot exist.");
}
}
Thanks a lot guys. Can someone explain what I have done in equals and hashcode methods? I copied the code from an answer and I couldn't find an explanation for it. Thanks again.
You iterate through your whole array starting at the first entry.
So if you compare the first entry with your user input (lets say its 002) the statement will be false. So it will throw an exception.
To solve this issue you would have to check if an entry has been found AFTER iterating through your array.
int userGuess = 002;
boolean userFound = false;
for (employee l : list)
{
if (userGuess == l.empid)
{
userFound = true;
l.display();
break;
}
}
if(!userFound)
{
throw new InputMismatchException ("employee doesnot exist");
}
To answer your second question:
i dont think you'll need the equals() and hashCode() method.
The equals checks if two objects are the same (see https://msdn.microsoft.com/de-de/library/bsc2ak47(v=vs.110).aspx)
the hashCode() method generates a "unique" value for an object (see https://msdn.microsoft.com/de-de/library/system.object.gethashcode(v=vs.110).aspx)
This should work for you:
boolean found = false;
for (employee l : list){
if (userGuess == l.empid) {
l.display();
found = true;
break;
}
}
if(!found){
throw new InputMismatchException ("employee doesnot exist");
}
Your existing code will not work because the first in the loop is always 001.

Removing Duplicate Entries in Array - Java

For Java practice, I am trying to create a method inside my EmployeesDirectory Class that:
Removes Duplicate entries from the array
The array should be the same length after removing duplicates
Non-Empty entries should be making a contiguous sequence at the beginning of the array - and the actualNum should keep a record of the entries
Duplicate Means: Same Name, Position and Salary
Here is my Current Code:
I am unsure on how to implement this - any help would be appreciated
class EmployeeDirectory {
private Employee dir[];
private int size;
private int actualNum;
public EmployeeDirectory(int n) {
this.size = n;
dir = new Employee[size];
}
public boolean add(String name, String position, double salary) {
if (dir[size-1] != null) {
dir[actualNum] = new Employee(name, position, salary);
actualNum++;
return true;
} else {
return false;
}
}
}
I'd rather you did not write a distinct method for removing duplicates. If I were you, I would search for duplicates in add method and then instantly decide whether I need to add Employee.
Also, why don't you use Sets (link for HashSet) instead of arrays for your purpose? Sets by their own definition disallow adding duplicates, so they seem to be appropriate as a solution
First of all, Override equals and hashCode methods in Employee class as follow
#Override
public boolean equals(Object other) {
if(this == other) return true;
if(other == null || (this.getClass() != other.getClass())){
return false;
}
Employee guest = (Employee) other;
return Objects.equals(guest.name, name)
&& Objects.equals(guest.position, position)
&& Objects.equals(guest.salary, salary);
}
#Override
public int hashCode() {
return Arrays.hashCode(new Object[] {
name,
position,
salary
});
}
Then you can use Stream API distinct method to remove duplicates
Returns a stream consisting of the distinct elements (according to
Object.equals(Object)) of this stream.
You can do it like so
Employee e1 = new Employee("John", "developer", 2000);
Employee e2 = new Employee("John", "developer", 2000);
Employee e3 = new Employee("Fres", "designer", 1500);
Employee[] allEmployees = new Employee[100];
allEmployees[0] = e1;
allEmployees[1] = e2;
allEmployees[2] = e3;
allEmployees = Arrays.asList(allEmployees).stream().distinct()
.toArray(Employee[]::new);
Arrays.asList(allEmployees).forEach(System.out::println);
Output: (keeping both empty and non-empty entries)
John developer 2000.0
Fres designer 1500.0
null
Unfortunately, I have not got the Employee class to verify my code, but try this:
void removeDuplicates() {
int length = dir.length;
HashSet set = new HashSet(Arrays.asList(dir));
dir = new Employee[length];
Employee[] temp = (Employee[]) set.toArray();
for (int index = 0; index < temp.length; index++)
dir[index] = temp[index];
}
The code must remain the size of array after deletion the duplicates. At the beginning of array there must be valid Employees, at the end - nulls.
And don't forget to add this at the beginning of your .java file
import java.util.Arrays;
import java.util.HashSet;
If your task states as "remove duplicates from array" (i. e. you cannot use ArrayList or control when adding items), you can use the following approach:
public void removeDuplicates() {
Set<Employee> d = new HashSet<>(); // here to store distinct items
int shift = 0;
for (int i = 0; i > dir.length; i++) {
if (d.contains(dir[i])) { // duplicate, shift += 1
shift++;
} else { // distinct
d.add(dir[i]); // copy to `d` set
dir[i - shift] = dir[i]; // move item left
}
}
for (int i = d.size(); i < dir.length; i++)
dir[i] = null; // fill rest of array with nulls
actualNum = d.size();
}
Here, shift variable stores number of duplicates found in the array so far. Every distinct item is moved to shift positions left in order to make sequence continuous while keeping initial ordering. Then remaining items are altered to nulls.
To make hash-based collections work with Employee instances correctly, you also need to override hashCode() and equals() methods as follows:
public class Employee {
//...
#Override
public int hashCode() {
return Objects.hash(name, position, salary);
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!o.getType().equals(this.getType()) return false;
Employee e = (Employee) o;
return Objects.equals(e.name, name)
&& Objects.equals(e.position, position)
&& Objects.equals(e.salary, salary); // or e.salary == salary, if it primitive type
}
}

how to make a connected list of elements in Java

i want to make a connected list of Student elements. The class Student must have only two fields(name and codeNumber).I also created a method to compare the names and if the names are equals then i compare codeNumbers. this is because i must insert the elements low to high.
I created a class like:
class Student
private String name;
private int codeNumber;
public Student(String name, int AM){
this.name = name;
this.codeNumber = codeNumber;
}
public int compareTo(Student other){
int result;
if(other.name.compareTo(this.name) == 0){
result = 0;
return result;
}
if(other.name.compareTo(this.name) < 0){
result = 1;
return result;
}
if(other.name.compareTo(this.name) > 0){
result = -1;
return result;
}
if(other.codeNumber > this.codeNumber){
result = 1;
return result;
}
if(other.codeNumber < this.codeNumber){
result = -1;
return result;
}
}
public String getName(){
return name;
}
public int getcodeNumber(){
return codeNumber;
}
}
And here is the problem. I need a StudentList class to make my own list.
So i created the class but im not sure if i have the right fields in this class.
I created 4 fields:
1.private Student studentElement;
2.private StudentList next = null; a reference to the next element
3.private StudenList head; a reference to the start of the list
4.private int size = 0; to know the number of the elements
And here is my code:
class StudentList{
private Student studentElement;
private StudentList next = null;
private StudentList head;
private int size = 0;
public StudentList(Student listEl){
listElement = listEl;
}
public boolean containsElement(Student p){
StudentList position = head;
while(position != null){
if(StudentElement.getName().equals(p.getName()) && studentElement.getAM() == p.getAM()){
return true;
}
position = position.getNext();
}
return false;
}
}
This is my code. Can anyone tell me if i have the right types of fields in my class and help me creating an insert method.
Don't reinvent the wheel.
Prefer using java.util.LinkedList
and add the Student instances in the way you need. This will preserve the order in
which you add to the list
Use one of the Java library list implementations such as ArrayList or LinkedList which do precisely what you want, no point in rolling-your-own.
Also learn about Collections.sort() if needed.
Your Student class should also implement the Comparable interface which (by a stroke of luck) has the same signature as your existing compareTo method.

Compare arraylist objects in java

I have list of objects in an arraylist and I need to compare every objects with other objects available in the arraylist;
For Example:
Class Employee {
private String empname;
private Long empid;
private boolean empsex;
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public Long getEmpid() {
return empid;
}
public void setEmpid(Long empid) {
this.empid = empid;
}
public boolean isEmpsex() {
return empsex;
}
public void setEmpsex(boolean empsex) {
this.empsex = empsex;
}
}
public list<Employee> getEmpList() {
List<Employee> empList = new ArrayList<Employee>();
Employee emp = new Employee();
for(...) {
//insert values to emp object for n number of times;
}
empList.add(emp); //add emp.object to empList;
return empList;
}
Now while inserting these values to UI; Need to compare objects in the list; where any two or more objects matches with each other or not?
Based on the assumption that you want to eliminate the duplicates from the list and do not show duplicates on GUI.
Use Set collection, it automatically takes care of duplicates.
A collection that contains no duplicate elements. More formally, sets
contain no pair of elements e1 and e2 such that e1.equals(e2), and at
most one null element. As implied by its name, this interface models
the mathematical set abstraction.
Override equals() and hashcode() methods.
References:
On equals and hashcode in Java
Overriding equals and hashcode
You could override equals method for that class to compare the objects of the same class the way you want.
Default equals method:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
Link about overriding equals:
http://javarevisited.blogspot.com/2011/02/how-to-write-equals-method-in-java.html
One simple method of doing this is to just do a double for loop.
public static List<Employee> getList(List<Employee> oldList)
{
List<Employee> empList = new ArrayList<Employee>();
for (int i = 0; i < oldList.size; i++)
{
for (int j = 0; j < oldList.size; j++)
{
//compare oldList.get(i) with oldList.get(j)
//if match, set some boolean
}
//if duplicate found, delete one copy, or add one to new list, etc
}
This allows you to go through each element in the outer loop, and compare to every other element in the inner loop.
I guess employee id (empid) is unique for each emplyee, yes?
If so, use a hash instead where empid is the key and employee object is the value.
Map<Long, Employee> empmap = new HashMap<Long, Employee>();
empmap.put(currentEmployee.getEmpid(), currentEmployee)

Categories