Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Need to find the common objects between to arraylist. Both of them are VarList type which is the simple POJO class. My comparison return all the elements inside the database list , Need to have a common elements (which as you can see they are objects) between two arraylists and add the them into the new list.
VarList class
public class VarList {
private int number;
private int age;
private String name;
public VarList(int number, int age, String name) {
super();
this.number = number;
this.age = age;
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
my solution which has a problem
//database arraylist
ArrayList<VarList> database=new ArrayList<VarList>();
database.add(new VarList(105,19,"b"));
database.add(new VarList(101,18,"c"));
database.add(new VarList(106,54,"database"));
//object array list
ArrayList<VarList> object=new ArrayList<VarList>();
object.add(new VarList(105,19,"b"));
database.add(new VarList(106,54,"database"));
List<VarList> resultList = new ArrayList<VarList>();
for(VarList user1 : database) {
for(VarList user2 : object) {
if(user1.getName().equals(user2.getName())) {
resultList.add(user2);
}
System.out.println(resultList);
}
}
Override equals & hashCode
You need to define overrides of equals and hashcode methods in your VarList class. Your class inherits these methods from Object.
Also, think harder about naming things. The VarList class is not a list, it is an element in your list. Surely it has an actual descriptive name. The second ArrayList you declare is named 'object'?!? Please don't name things object, ever. Call it testList or something.
Finally, your System.out is inside the resultList loop, so its output is likely to confuse you. Or maybe that was intentional. Just realize that it's going to output it once for every element in resultList.
Instead of implementing your own for loop to compare the elements of 2 ArrayList, you could use retainAll(list) method of the Collection class.
Here is the sample Usage:
SimplePojo.java
import java.util.Objects;
public class SimplePojo {
private String name;
private int age;
public SimplePojo(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public String toString() {
return "SimplePojo{" + "name='" + name + '\'' + ", age=" + age + '}';
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SimplePojo that = (SimplePojo) o;
return age == that.age && Objects.equals(name, that.name);
}
#Override
public int hashCode() {
return Objects.hash(name, age);
}
}
Client.java
import java.util.ArrayList;
import java.util.List;
public class ListCommonElements {
public static void main(String[] args) {
List<SimplePojo> list1 = new ArrayList<>();
list1.add(new SimplePojo("Dave",49));
list1.add(new SimplePojo("Smith",40));
list1.add(new SimplePojo("Johnson",32));
System.out.println("List 1: " +list1.toString());
List<SimplePojo> list2 = new ArrayList<>();
list2.add(new SimplePojo("Dave",49));
list2.add(new SimplePojo("Smith",40));
list2.add(new SimplePojo("Steve",32));
System.out.println("List2: "+list2.toString());
list1.retainAll(list2);
System.out.println("Common ELements: " + list1.toString());
}
}
Notice How I am overriding equals and hashCode method (You can autogenerate it if using IntelliJ ).
The reason to do it is your list is not of primitive type, so you need to tell what does equal mean to you (so you can get common elements ) and in the current implementation, I am saying that if both name and age are the same then only I would call 2 Object equal.
Related
I have a program I am working with to help me practice my coding skills. The program has the following scenario: there is a classroom of 20 students, where the record is taken of the students' names, surnames, and age. Half of these students take part in the school's athletics. Here, record is kept of their races that they have done and the ones they've won.
In this program, I have three classes:
runStudents - class with main method
Students (String name, String surname, int age) - parental class
AthleticStudents (String name, String surname, int age, int races, int victories) - sub class
The user should be able to add another race (and win) to the object. As seen by the code provided, an Array is created to store the 20 Students objects. I have to be able to access a method to alter the object in the array, but this method is not in the parental class (the class the objects are created from.
public class Students
{
private String name;
private String surname;
private int age;
public Students()
{
}
public Students(String name, String surname, int age)
{
this.name = name;
this.surname = surname;
this.age = age;
}
public String getName()
{
return this.name;
}
public String getSurname()
{
return this.surname;
}
public double getAge()
{
return this.age;
}
public void setName(String name)
{
this.name = name;
}
public void setSurname(String surname)
{
this.surname = surname;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return String.format("name\t\t: %s\nsurname\t\t: %s\nage\t\t: %s",
this.name, this.surname, this.age);
}
}
public class AthleticStudents extends Students
{
private int races;
private int victories;
public AthleticStudents()
{
}
public AthleticStudents(String name, String surname, int age, int
races, int victories)
{
super(name, surname, age);
this.races = races;
this.victories = victories;
}
public int getRaces()
{
return this.races;
}
public int getVictories()
{
return this.victories;
}
public void setRaces(int races)
{
this.races = races;
}
public void setVictories(int victories)
{
this.victories = victories;
}
public void anotherRace()
{
this.races = this.races + 1;
}
public void anotherWin()
{
this.victories = this.victories + 1;
}
public String toString()
{
return super.toString() + String.format("\nnumber of races\t:
%s\nnumber of wins\t: %s", this.races, this.victories);
}
}
public class runStudents
{
public static void main(String[]args)
{
Students[] myStudents = new Students[20];
myStudents[0] = new Students("John", "Richards", 15);
myStudents[1] = new AthleticStudents("Eva", "Grey", 14, 3, 1);
myStudents[2] = new Students("Lena", "Brie", 15);
for (int i = 0; i < 3; i++)
System.out.println(myStudents[i].toString() + "\n\n");
}
}
I want to be able to do the following:
AthleticStudents[1].anotherRace();
but cannot do so as the array object is derived from the parental class, and I declared the method in the sub class. How can I link the two?
I assume that you create an array of the parent class instances. Just cast the instance this way (you better check whether the element is the instance of a subclass):
if (AthleticStudents[1] instanceof AthleticStudents)
((AthleticStudents) AthleticStudents[1]).anotherRace();
I'm not sure if this is exactly what you're looking for but it worked well for me. Instead of trying to access AthleticStudents method anotherRace() like that, try this in your main method.
Students[] myStudents = new Students[20];
myStudents[0] = new Students("John", "Richards", 15);
myStudents[1] = new AthleticStudents("Eva", "Grey", 14, 3, 1);
myStudents[2] = new Students("Lena", "Brie", 15);
AthleticStudents addRace= (AthleticStudents)myStudents[1];
addRace.anotherRace(); //This will increment Eva's race count to 4
for (int i = 0; i < 3; i++)
System.out.println(myStudents[i].toString() + "\n\n");
All I did was cast the element into an object AthleticStudents named 'addRace'. By casting myStudents[1] to this new object you are able to access all of AthleticStudents methods.
I just saw the other answer posted which works just as well!
Hope this helps!
I’m not sure that i understand your question, because you are a bit inconsistent with your capitalization. runStudents is a class, while AthleticStudents is both a class and an array. But i’ll try.
IF i did understand your question, you have an array Student[] studentArray. Some Student objects in studentArray are AthleticStudents, others are not. You have a specific AthleticStudent eva which is in studentArray[] having let’s say index 1, and you want to add to her anotherRace(). Your call studentArray[1].anotherRace does not compile because the compiler treats that element as a Student and not as a AthleticStudent.
The trick is to cast the element to AthleticStudent. I omit the test of the element of being really an AthleticStudent; you will have to do that test in your code.
((AthleticStudent) studentArray[1]).anotherRace();
in a simple ArrayList, I can use contains like this :
ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Peter");
String find = "Peter";
if (names.contains(find))
System.out.println("I FOUND IT");
but how do I use contains (or something similar), if I have an ArraList of classes (custom data type)?
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name= name;
this.age= age;
}
public String getName() {
return this.name;
}
public String getAge() {
return this.age;
}
}
ArrayList<Person> somePerson = new ArrayList<Person>();
somePerson.add(new Person("John","25"));
somePerson.add(new Person("Peter","84"));
How do I find in this ArrayList just the name "Peter", or just the score of "84"?
I'm using loops and
if (somePerson.get(i).getName().contains(find))
but is there a way without loops, like in the simple ArrayList above?
To quote https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html:
Many methods in Collections Framework interfaces are defined in terms
of the equals method. For example, the specification for the
contains(Object o) method says: "returns true if and only if this
collection contains at least one element e such that (o==null ?
e==null : o.equals(e))."
[...]
Implementations are free to implement
optimizations whereby the equals invocation is avoided, for example,
by first comparing the hash codes of the two elements. (The
Object.hashCode() specification guarantees that two objects with
unequal hash codes cannot be equal.)
In other words, your custom classes need to implement the equals() and hashCode() methods according to the contract of those methods, which states that equal objects should have equal hash codes. (But equal hash codes do not necessarily mean that the objects are equal, though it should be avoided to have implementations that easily result in such collisions.)
You have to overide the equals method in the Person class as follows:
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name) {
this.name = name;
}
public Person(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
#Override
public boolean equals(Object other) {
if (!(other instanceof Person)) {
return false;
}
Person p = (Person) other;
try {
return this.name.equals(p.name) || this.age == p.age;
} catch (Exception e) {
return this.age == p.age;
}
}
#Override
public String toString() {
return name + " " + age;
}
}
Here is the main code
import java.util.ArrayList;
import org.apache.commons.lang3.ArrayUtils;
public class MainDemo {
public static void main(String[] args) {
ArrayList<Person> somePerson = new ArrayList<Person>();
Person person1 = new Person("John", 25);
Person person2 = new Person("Peter", 84);
somePerson.add(person1);
somePerson.add(person2);
System.out.println("Is John there?: "+ somePerson.contains(new Person("John"))); // true
System.out.println("Is Paul there?: "+ somePerson.contains(new Person("Paul"))); // false
System.out.println("Is age 84 (Peter) there?: "+ somePerson.contains(new Person(84))); //true
System.out.println("Is there a person with age 9 ?: "+ somePerson.contains(new Person(9))); //false
searchPersonByName(somePerson, "Peter");
searchPersonByName(somePerson, "John");
searchPersonByName(somePerson, "Paul"); // not found
searchPersonByAge(somePerson, 25); // will print John 25
searchPersonByAge(somePerson, 84); //will print Peter 84
searchPersonByAge(somePerson, 102); // not found
}
public static void searchPersonByName(ArrayList<Person> personsList, String name){
Person[] personArray = personsList.toArray(new Person[personsList.size()]); // converting arrayList to Array
String str = personsList.contains(new Person(name))?personsList.get(ArrayUtils.indexOf(personArray, new Person(name))).toString():"Not found";
System.out.println(str);
}
public static void searchPersonByAge(ArrayList<Person> personsList, int age){
Person[] personArray = personsList.toArray(new Person[personsList.size()]);
String str = personsList.contains(new Person(age))?personsList.get(ArrayUtils.indexOf(personArray, new Person(age))).toString():"Not found";
System.out.println(str);
}
}
Here you have the output:
Is John there?: true
Is Paul there?: false
Is age 84 (Peter) there?: true
Is there a person with age 9 ?: false
Peter 84
John 25
Not found
John 25
Peter 84
Not found
I'm new to Java and i've been bashing my head over the wall to solve this problem. Anyway below is a class that creates a Person and below that, is a class that creates a Phonebook using an ArrayList of type Person. I want to write the remove function (in order to remove a Person from the list) but my problem is that since i only get the name of the person i can't use the Indexof function (cause it requires object) to get at what position lies the name.
This is my first time using an ArrayList to store an Object so i'm not even sure
how my results would appear. I'm guessing that if the position of the name (in my list) is 10 then 11 would be the phone and 12 would be the address. Am i correct?
public class Person
{
private String name;
private String phone;
private String address;
public Person (String n, String p, String a)
{
this.name = n;
this.phone = p;
this.address = a;
}
public void setPhone(String newPhone)
{
this.phone = newPhone;
}
public String getName()
{
return this.name;
}
public String getPhone()
{
return this.phone;
}
public String getAddress()
{
return this.address;
}
public String print()
{
return "Name is : " + this.name + "\nPhone is : " + this.phone + "\nAddress is : " + this.address;
}
}
import java.util.*;
public class phoneBook
{
Scanner in = new Scanner ( System.in );
private ArrayList <Person> persons = new ArrayList <Person>();
private int i;
private boolean flag;
public void addPerson(Person p)
{
persons.add(p);
}
public void listPersons ()
{
System.out.println(persons);
}
public void lookUp (String theName)
{
flag = persons.contains(theName);
if ( flag == true )
{
System.out.println("That name exists!");
}
else
{
System.out.println("That name does not exist!");
}
}
public void remove (String theName)
{
}
Edit: I'm planning to use the Scanner in another function. Don't worry about it.
I'm not sure of if do you want to get the object of that array, but each object is indexed to that array (with full attributes), now you can remove it by using the following code,
public String removePerson(ArrayList<Person> arrayList,String name)
{
for(Person currentPerson:arrayList)
{
if(currentPerson.getName().equals(name))
{
arrayList.remove(currentPerson);
return "Removed successfully"
}
}
return "No record found for that person";
}
just pass the arrayList and the name of that person to this method
You should override the equals() and hashCode() methods in the Person class. This way you will define when two objects of this type will be considered equal. Then you can use list.contains(yourObject) to determine if that object is equal to any object in your list, this based on your equals() implementation.
Does this help you?
public void remove (String theName,ArrayList<Person> persons) {
for (int i = 0; i < persons.size();++i) {
if(persons[i].getName().equals(theName)) {
persons.remove(i);
}
}
}
Best regards, Nazar
Here is my code in make sure to add student information with name ,age and their address. In order to make sure the student is unique. I use the hashCode() and equals() to make sure data integrity. The same name of student will be considered as override.
Problem is: The same information is never be cleaned out, Anybody know why? It seems the hashCode() and equals() never work.
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name, int age){
this.name = name;
this.age = age;
}
public int hashcode(){
return name.hashCode() + age *34;
}
//override equals method
public boolean equals(Object obj){
if(!(obj instanceof Student))
throw new ClassCastException("The data type is not match!");
Student s = (Student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
#Override
public int compareTo(Student s) {
int num = new Integer(this.age).compareTo(new Integer(s.age));
if (num == 0)
return this.name.compareTo(s.name);
return num;
}
}
public class HashMapDemo1 {
public static void main (String[] agrs){
HashMap<Student,String> hs = new HashMap<Student,String>();
hs.put(new Student("James",27),"Texas");
hs.put(new Student("James",27), "California");
hs.put(new Student("James",27), "New mexico");
hs.put(new Student("Jack",22),"New York");
hs.put(new Student("John",25),"Chicago");
hs.put(new Student("Francis",26),"Florida");
Set<Student> Keyset = hs.keySet();
Iterator<Student> it = Keyset.iterator();
while(it.hasNext()){
Student stu = it.next();
String addr = hs.get(stu);
System.out.print(stu.getName()+stu.getAge()+"..." +addr+"\n");
}
}
hashcode != hashCode
Be sure to use the #Override annotation whenever you think that you are overriding a super-class's method, as this will allow the compiler to notify you if/when you are wrong. As you're finding out, it's much easier to fix errors at the compilation stage rather than the run-time stage.
Myself, I'd not use the age field as part of equals or hashCode since age can change for a student with time. I'd use Date birthDate or some other invariant instead.
And also I agree with Radiodef: the equals(...) method should not throw an exception. If the parameter object is not Student type, simply return false.
The method you have implemented is public int hashcode().
It should be public int hashCode().
I have a couple to class in which I'm getting and setting a few things and then finally calling it in my main method. But when I call my class in the main method it just gives me the object instead of name,address and age. I know this structure is very complicated but I want to keep this structure because later on I will be adding a lot of things to this. It would be AMAZING if someone could tell me how to do this. I would really appreciate this. Below is my code for all my classes
This is my first class
public class methodOne
{
public String getName()
{
String name = "UserOne";
return name;
}
public int getAge()
{
int age = 17;
return age;
}
public String getAddress()
{
String address = "United States";
return address;
}
}
This is my second class
public class methodTwo
{
String name;
String address;
int age;
public methodTwo(methodOne objectOne)
{
name=objectOne.getName();
address=objectOne.getAddress();
age=objectOne.getAge();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
This is my third class
public class methodThree {
private methodTwo methodTwoInMethodThree;
private methodOne methodOneInMethodThree;
public methodThree()
{
this.methodOneInMethodThree = new methodOne();
this.methodTwoInMethodThree = new methodTwo(methodOneInMethodThree);
}
public methodTwo getMethodTwoInMethodThree() {
return methodTwoInMethodThree;
}
public void setMethodTwoInMethodThree(methodTwo methodTwoInMethodThree) {
this.methodTwoInMethodThree = methodTwoInMethodThree;
}
}
This is my fourth class which is the method maker
public class methodMaker {
public methodThree brandNewFunction(methodTwo object)
{
methodThree thirdMethod = new methodThree();
thirdMethod.setMethodTwoInMethodThree(object);
return thirdMethod;
}
}
This is my main class which calls methodMaker. What I want to achieve is that when I print the value it should print the name,address and age but instead it just prints trial.methodThree#4de5ed7b
public class mainClass {
public static void main(String args[])
{
methodMaker makerOfMethods = new methodMaker();
methodOne one = new methodOne();
methodTwo object = new methodTwo(one);
System.out.println(makerOfMethods.brandNewFunction(object).toString());
}
}
What you need to do is to override the default implementation of the .toString() method in the objects you want to print out:
#Override
public String toString()
{
return "Name: " + this.name;
}
EDIT:
I do not know exactly where you are printing, and you naming convention doesn't really help out, but from what I am understanding, you would need to implement it in all of you classes since they all seem to be related to each other.
So, in your methodOne class (can also be applied to methodTwo):
#Override
public String toString()
{
return "Name: " + this.name + " Age: " + this.age + " Address: + " this.address;
}
In your methodThree class:
private methodTwo methodTwoInMethodThree;
private methodOne methodOneInMethodThree;
#Override
public String toString()
{
StringBulder sb = new StringBuilder();
if(this.methodTwoInMethodThree != null)
{
sb.append("Method 2:").append(methodTwoInMethodThree.toString());
}
if(methodOneInMethodThree != null)
{
sb.append("Method 1:").append(methodOneInMethodThree.toString());
}
return sb.toString();
}
When you call
MyClass myObject = new MyClass();
System.out.println(myObject);
Implicitly , java calls instead
System.out.println(myObject.toString());
So, if in MyClass, you override toString(), then whatever your toString method returns is what's gonna be printed.
Side note: are you confusing classes and methods? Methods are functions in your classes, classes are wrappers around a bunch of attributes and methods. Your naming is confusing.
try this code:
public class methodTwo
{
String name;
String address;
int age;
public methodTwo(methodOne objectOne)
{
name=objectOne.getName();
address=objectOne.getAddress();
age=objectOne.getAge();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return name+" "+address+" "+age;
}
}
Are you printing the object using println()?
From the docs, println():
calls at first String.valueOf(x) to get the printed object's string value
This string value is obtained from the object's toString() method, which:
returns a string consisting of the name of the class of which the object is an instance, the at-sign character `#', and the unsigned hexadecimal representation of the hash code of the object
So if you want to print anything other than this you have to override the toString() method in your object and return a string containing whatever you want.
Just google "override tostring java" and you will see a ton of examples.