Task - Collections - Map Basics - Book Map #51
Replies: 61 comments 1 reply
-
package week_5;
import java.util.*;
class Books {
int id;
String name;
String author;
String publisher;
int quantity;
public Books(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 "Books [author=" + author + ", id=" + id + ", name=" + name + ", publisher=" + publisher + ", quantity="
+ quantity + "]";
}
@Override
public int hashCode() {
int prime = 31;
int result = 1;
result = prime * result + 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;
Books other = (Books) obj;
if (id != other.id)
return false;
return true;
}
}
public class Maps {
public static void main(String[] args) {
Books b1 = new Books(121, "Let us C", "Yashwant Kanetkar", "BPB", 8);
Books b2 = new Books(233, "Operating System", "Galvin", "Wiley", 6);
Books b3 = new Books(101, "Data Communications & Networking", "Forouzan", "Mc Graw Hill", 4);
Books b4 = new Books(121, "Let us C", "Yashwant Kanetkar", "Mc Graw Hill", 11);
Map<Integer, Books> map = new HashMap<>();
map.put(b1.id, b1);
map.put(b2.id, b2);
map.put(b3.id, b3);
map.put(b4.id, b4);
map.forEach((k, v) -> {
System.out.println(v);
});
System.out.println("After doubling the quantity!!!");
map.forEach((k, v) -> {
v.quantity *= 2;
map.put(k, v);
System.out.println(v);
});
// if (map.containsKey(233)) {
// map.get(233).publisher += " USA";
// map.replace(233, map.get(233));
// }
System.out.println("After merging!!");
map.merge(233, map.get(233), (oldBook, newBook) -> {
newBook.publisher += " USA";
return newBook;
});
System.out.println(map.get(233));
}
} |
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 JavaMaps; import java.util.; class Book { } public class MapImplementation { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package Maps;
import java.util.HashMap;
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() {
final int prime = 37;
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 (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "[Author=" + author + ", Id=" + id + ", Name=" + name + ", Publisher=" + publisher + ", Quantity="
+ quantity + "]";
}
}
public class MapMerge {
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);
HashMap<Integer, Book> books = new HashMap<>();
books.put(b1.id, b1);
books.put(b2.id, b2);
books.put(b3.id, b3);
books.put(b4.id, b4);
System.out.println("Contents of Book");
books.values().stream().forEach(System.out::println);
books.values().stream().forEach(i -> i.quantity *= 2
);
System.out.println("After doubling");
books.values().stream().forEach(System.out::println);
books.merge(233, books.get(233), (oldvalue, newvalue) -> {
newvalue.publisher += " USA";
return newvalue;
});
System.out.println("After merging");
books.values().stream().forEach(System.out::println);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
|
package MapsModule; import java.util.*; class Book { } public class Bibliophiles { } |
Beta Was this translation helpful? Give feedback.
-
|
`package EDU; import java.util.Objects; public class Book { } package EDU; import java.util.HashMap; public class Driver { } |
Beta Was this translation helpful? Give feedback.
-
|
src.zip |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Objects; } } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.HashMap; class Book { } public class BookDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
package edureka; import java.util.HashMap; public class Book { } |
Beta Was this translation helpful? Give feedback.
-
|
`// Online Java Compiler class Book { public Book(int id, String name, String author, String publisher, int quantity) { @OverRide @OverRide @OverRide public static void main(String[] args) { }}` |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.HashMap; class Book { } public class Main { |
Beta Was this translation helpful? Give feedback.
-
|
package collections; import java.util.HashMap; class Book { } public class BookDriver { } |
Beta Was this translation helpful? Give feedback.
-
|
@akash-coded |
Beta Was this translation helpful? Give feedback.
-
|
@akash-coded : Below is the solution. package demo.practice; import java.util.HashMap; public class BookMap { } } |
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; class Book { } public class Main { } |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.HashMap; class Book { } public class Bookdriver { } |
Beta Was this translation helpful? Give feedback.
-
|
package collections; import java.util.HashMap; class Book { } public class BookStats { } |
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.
-
A Map for Bibliophiles
Define a class called Book.
The skeleton of the Book class is given below:
Write the implementation for hashCode() and equals() manually. (Important fields:
id)In the driver class, create four instances of Book class as follows:
Create a
HashMap<Integer, Book>calledbooksand insert all the book objects as values using theiridas the key.After insertion of all books, display all the values stored in books.
Double the
quantityof all the books.Write a
merge()function to check if a book with the id233exists or not. If it does, add " USA" to itspublishernameBeta Was this translation helpful? Give feedback.
All reactions