Task - Collections - Finding Duplicates Using equals() #46
Replies: 38 comments
-
|
BOOK CLASS--- DRIVER CLASS--- |
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.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.ArrayList; class Book { } public class BookCount { } |
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.
-
|
import java.util.ArrayList; class Book { } public class DuplicateBooks { `` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
` class Book1 { } public class BookDriver { } ` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.List;
import java.util.ArrayList;
class Book {
int id;
String name;
String author;
String publisher;
int quantity;
// parameterised constructor
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public String toString() {
return "Book [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result + ((name == null) ? 0 : 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;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class Discussion46 {
public static void main(String[] args) {
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b4 = new Book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
List<Book> Library = new ArrayList<>();
Library.add(b1);
Library.add(b2);
Library.add(b3);
Library.add(b4);
for (int i = 0; i < Library.size(); i++) {
for (int j = i + 1; j < Library.size(); j++) {
if (Library.get(i).equals(Library.get(j))) {
System.out.println(Library.get(i) + "\n" + Library.get(j) + "\n");
if (Library.get(i).quantity > Library.get(j).quantity) {
System.out.println("Duplicate book with highest quantity: \n" + Library.get(i));
} else if (Library.get(i).quantity < Library.get(j).quantity) {
System.out.println("Duplicate book with highest quantity: \n" + Library.get(j));
}
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task46 {
public static void main(String[] args) {
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b4 = new Book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
ArrayList<Book> library = new ArrayList<Book>();
library.add(b1);
library.add(b2);
library.add(b3);
library.add(b4);
ArrayList<Book> library2 = new ArrayList<Book>();
for (int i = 0; i < library.size() - 1; i++) {
for (int j = i + 1; j < library.size(); j++) {
if (library.get(i).equals(library.get(j))) {
library2.add(library.get(i));
}
}
}
System.out.println("quantity of duplicate books = " + library2.size());
library2.forEach(System.out::println);
System.out.println();
int qmax = 0;
for (int i = 0; i < library.size(); i++) {
if (library.get(i).quantity > library.get(qmax).quantity) {
qmax = i;
}
}
System.out.println("quantity of non-duplicate books = " + qmax);
library.forEach(System.out::println);
System.out.println();
}
}
class Book {
int id;
String name;
String author;
String publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public String toString() {
return "Book [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : 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;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
class Book {
int id;
String name;
String author;
String publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public String toString() {
return "Book [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
String imp() {
return "id=" + this.id + ", name=" + this.name + ", auther=" + this.author;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : 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;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
else if (!name.equals(other.name))
return false;
return true;
}
}
public class d46 {
public static void main(String[] args) {
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b4 = new Book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
ArrayList<Book> library = new ArrayList<>();
library.add(b1);
library.add(b2);
library.add(b3);
library.add(b4);
int duplicateBook = 0;
ArrayList<Book> duplicateList = new ArrayList<>();
for (int i = 0; i < library.size(); i++) {
for (int j = i + 1; j < library.size(); j++) {
if (library.get(j).equals(library.get(i))) {
duplicateBook++;
duplicateList.add(library.get(j));
library.remove(j);
library.remove(i);
}
}
}
System.out.println("duplicate count:" + duplicateBook);
for (Book b : duplicateList) {
System.out.println("duplicate book Imp.detail:" + b.imp());
}
int max_q = Integer.MIN_VALUE;
int i = 0;
for (; i < library.size(); i++) {
if (library.get(i).quantity > max_q) {
max_q = library.get(i).quantity;
}
}
System.out.println("max quantity:(non duplicate)" + library.get(i - 1));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
class book {
int id;
String name;
String author;
String publisher;
int quantity;
public book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public String toString() {
return "book [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((publisher == null) ? 0 : publisher.hashCode());
result = prime * result + quantity;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
book other = (book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (publisher == null) {
if (other.publisher != null)
return false;
} else if (!publisher.equals(other.publisher))
return false;
if (quantity != other.quantity)
return false;
return true;
}
}
public class books {
public static void main(String[] args) {
book b1 = new book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
book b2 = new book(233, "Operating System", "Galvin", "Wiley", 6);
book b3 = new book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
book b4 = new book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
List<book> library = new ArrayList<>();
library.add(b1);
library.add(b2);
library.add(b3);
library.add(b4);
List<book> duplicate = new ArrayList<>();
for (int i = 0; i < library.size() - 1; i++) {
for (int j = i + 1; j < library.size(); j++) {
if (library.get(j).equals(library.get(i))) {
duplicate.add(library.get(j));
library.remove(j);
library.remove(i);
}
}
}
System.out.println("No of duplicate books " + duplicate.size());
duplicate.forEach(System.out::println);
int max = 0;
for (int i = 1; i < library.size(); i++) {
if (library.get(i).quantity > library.get(max).quantity)
max = i;
}
System.out.println(library.get(max));
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.ArrayList;
import java.util.List;
public class D46 {
public static void main(String[] args) {
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b4 = new Book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
List<Book> library = new ArrayList<>();
library.add(b1);
library.add(b2);
library.add(b3);
library.add(b4);
List<Book> duplicate = new ArrayList<>();
for (int i = 0; i < library.size() - 1; i++) {
for (int j = i + 1; j < library.size(); j++) {
if (library.get(j).equals(library.get(i))) {
duplicate.add(library.get(j));
library.remove(j);
library.remove(i);
}
}
}
System.out.println("No of duplicate books " + duplicate.size());
duplicate.forEach(System.out::println);
int maxIndex = 0;
for (int i = 1; i < library.size(); i++) {
if (library.get(i).quantity > library.get(maxIndex).quantity)
maxIndex = i;
}
System.out.println("Non duplicate book of highest quantity: " + library.get(maxIndex));
}
}
class Book {
int id;
String name;
String author;
String publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public int hashCode() {
int prime = 31;
int result = 1;
result = prime * result + (name == null ? 0 : name.hashCode()) + ((author == null ? 0 : author.hashCode()))
+ id;
return result;
}
@Override
public String toString() {
return "Book [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (id != other.id)
return false;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
} |
Beta Was this translation helpful? Give feedback.
-
Bookimport java.util.Objects;
public class Book {
int id;
String name;
String author;
String publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
super();
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", publisher=" + publisher
+ ", quantity=" + quantity + "]";
}
@Override
public int hashCode() {
int prime = 31 ;
int result = 0 ;
result = result * prime + id ;
return result ;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book obj1 =(Book) obj ;
if(this.id != obj1.id) {
return false ;
}
if(this.author != obj1.author) {
return false ;
}
return true ;
}
}Driver Classimport java.util.*;
public class Driver {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Book> library = new ArrayList<>() ;
Book b1 = new Book(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Book b2 = new Book(233, "Operating System", "Galvin", "Wiley", 6);
Book b3 = new Book(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Book b4 = new Book(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
library.add(b1);
library.add(b2);
library.add(b3);
library.add(b4);
List<Book> notDup = new ArrayList<>();
boolean isSame = false ;
int duplicate = 0 ;
int notDuplicate = 0 ;
int quantity = 0 ;
for(int i = 0 ; i < library.size()-1 ; i++) {
isSame = false ;
for(int k = i+1 ; k < library.size() ; k++) {
if(library.get(i).equals(library.get(k))) {
duplicate++ ;
System.out.println("No. of Duplicate Book :" + duplicate);
isSame = true ;
System.out.println(library.get(i));
}
}
if(!isSame) {
notDuplicate++ ;
notDup.add(library.get(i));
if(quantity < library.get(i).quantity) {
quantity = library.get(i).quantity ;
}
}
}
for(Book k : notDup) {
if(k.quantity == quantity) {
System.out.println(k);
}
}
}
}OUTPUT |
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.
-
Duplicate Books in a Library
Define a class called Book.
The skeleton of the Book class is given below:
Write the implementation for hashCode() and equals() manually. (Decide which fields are important)
In the driver class, create four instances of Book class as follows:
Add the book objects to an ArrayList called
library.Print the important details of duplicate books in the list, along with a count of the number of duplicate books.
Print the details of the non-duplicate books with the highest quantity.
Expected Output:
Beta Was this translation helpful? Give feedback.
All reactions