About sorting array list - java

I am trying to ensure that the objects i insert in productDatabase have not been already inserted and that i sort my arraylist using method sortData but without using any comparators in method sortData
public class Application {
public static void main(String[] args) {
Product p = new Product(15,"test",3.45);
Product p2 = new Product(15,"test",3.45);
Product p3 = new Product(4716,"koukouroukou",1.25);
Product p4 = new Product(6002,"bananofatsoula",0.60);
ProductDatabase productDatabase = new ProductDatabase();
productDatabase.addProduct(p);
productDatabase.addProduct(p2);
productDatabase.addProduct(p3);
productDatabase.addProduct(p4);
productDatabase.printDatabase();
productDatabase.sortDatabase();
productDatabase.printDatabase();
}
public class Product {
private int code;
private String name;
private double price;
public Product(int code, String name, double price){
this.code = code;
this.name = name;
this.price = price;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString(){
return code+" , description: "+name+", price: "+price;
}
public int hashCode(){
return 31 * code + name.hashCode();
}
public boolean equals(Object o){
Product other = (Product)o;
if (this.code == other.code){
return true;
}
else{
return false;
}
}
import java.util.ArrayList;
import java.util.Collection;
public class ProductDatabase {
private ArrayList<Product> productDatabase;
public ProductDatabase(){
productDatabase = new ArrayList<Product>();
}
public void addProduct(Product p){
if(!productDatabase.contains(p)){
productDatabase.add(p);
}
}
public void printDatabase(){
for(Product product : productDatabase){
System.out.println(product);
}
}
public void sortDatabase(){
// ????????????????????????????????????????????????????????????
So my questions are
Does contain(p) is enough to ensure that the same product is not already in the list?
products are the same when they have the same code and name.if not what i have to do?
How i sort my withous using comparators in class ProductDatabase.maybe by a new method in product ?
Does productDatabase extends Product???

You can return bool value in order to know the product is already there or not . Code is used to ensure the differentiation of products if not add another code id.
Product instance will carry information of just one Product so Sort must be done in the member function of class having all the records of product, not just one.
No product_database does not extend product . It is a log of product class not a part .

your questions 1 and 2 are a little unclear. Can you re-write them? As for question 3. No ProductDatabase does not extend product, neither should it. ProductDatabase HAS products. ProductDatabase is not a product

Yes, contains() is enough (in our case) to ensure uniqueness
Since you implemented equals and hashCode - you're good
You don't need to sort if you don't have another purpose to do so, but since you're using an ArrayList every time contains() is called it iterates the whole list which is not very efficient. A better implementation would use Set (a HashSet for example)
ProductDatabase does not have to extend Product - it contains a list/set of products but it doesn't have any character/behavior like Product

Yes, contain(p) is enough to ensure that the same product is not already in the list, because you overrided "equals" method.
In "equals" you can use shorter construction:
Product other = (Product)o;
return this.code == other.code;
For sort ArrayList with java.util.Collections class two options possible:
Collections.sort(List list, Comparator c). You have to write own Comparator class and pass as second parameter.
Collections.sort(List list) and class Product must implement Comparable interface

Yes, Contain(p) is enough to ensure that the same product is not already in the list BUT that is NOT efficient. Use a Set instead of ArrayList.
For question 2, you have to decide the when the two products are equal and code that in your equals method like you did for 'product code'

Related

Set working incorrectly in JAVA

I just want to remove duplicate elements from list. To do this I have written a POJO class Student as :
class Student{
private String roll;
private String name;
public Student(String roll, String name) {
this.roll = roll;
this.name = name;
}
#Override
public boolean equals(Object obj) {
Student st = (Student) obj;
return st.getRoll().equals(roll);
}
public String getRoll() {
return roll;
}
public void setRoll(String roll) {
this.roll = roll;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return roll ;
}
}
And a test class as below :
public class TestMain {
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("19", "goutam kumar singh"),
new Student("20", "goutam kumar singh"),
new Student("11", "goutam kumar singh"),
new Student("19", "goutam kumar singh")
);
List<Student> arrayList = new CopyOnWriteArrayList<>(list);
Set<Student> set = new HashSet<>();
for(Student st : arrayList)
set.add(st);
System.out.println(set);
}
}
but in the output all the four elements in the set but i am expecting only three element as fourth element is duplicate and must be removed.
Where I am going wrong?
You have to override the hashCode() method too. Override hashCode() methods for those property for which you override equals() method.
While working with Collection it's useful to remember the contract between hashCode() and equals() method -
1. If two objects are equal, then they must have the same hash code.
2. If two objects have the same hashcode, they may or may not be equal.
For more information you may visit this link
A HashSet stores elements internally as keys in a HashMap. Because of this, it will use your Student object as the keys for that map, using the hash code for each object. Since you don't provide an implementation for this method hashCode(), the default one from Object is used and each of your students will have a different hash code.
You must extend this method in your class, being aware of the equals-hashCode contract. If two objects are equal, they must have the same hashCode (the reverse isn't allways true). For further details see this Object.hashCode()

Only Allow Objects with Unique Name - Java

I am making an inventory system.
I want to ensure that objects I am creating (Ingredients) all have unique names. In other words, I want to make sure that there are never two Ingredients that have the same name in the whole program. Currently I have the following class:
package ingredient;
import java.util.HashSet;
public class Ingredient {
private final String name;
private final double price;
private static HashSet<String> names = new HashSet<String> ();
private Ingredient(String ingr_name, double ingr_price) {
name = ingr_name;
price = ingr_price;
}
public static Ingredient createIngredient(String ingr_name, double ingr_price) {
if (names.contains(ingr_name)) {
return null;
} else {
names.add(ingr_name);
return new Ingredient(ingr_name, ingr_price);
}
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
Then, when I go to actually make new ingredients, I make statements such as :
Ingredient egg = Ingredient.createIngredient("egg", 1);
Is this okay design? I suppose I am concerned because returning "NULL" might not be the best practice here.
I cant comment, but whatever...
I would go about this by storing all of the Ingredients in a different class, then you wouldn't need all this static nonsense. In the class where you actually create a new Ingredient (Ingredient egg = Ingredient.createIngredient("egg", 1);) you could maybe create an ArrayList of ingredients like so:
ArrayList<Ingredient> ingredients = new ArrayList<>();
Then when you make a new Ingredient you would just have to make sure you add it to the ArrayListand when you do so, check that none of the ingredients are already there, maybe something like this:
createIngredient("egg", 1);
or
Ingredient egg = createIngredient("egg", 1);
...
private Ingredient createIngredient(String ingr_name, double ingr_price){
for(Ingredient i : ingredients){
if(i.getName().equals(ingr_name)){
return null;
}
}
Ingredient newing = new Ingredient(ingr_name, ingr_price);
ingredients.add(newing);
return newing;
}
Then the Ingredient class could be cut down to something like this:
package ingredient;
public class Ingredient {
private final String name;
private final double price;
public Ingredient(String ingr_name, double ingr_price) {
name = ingr_name;
price = ingr_price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
And then you could access each individual Ingredientwith a method to run through the ArrayList and find the Ingredient with the name your looking for:
public Ingredient findIngredient(String name){
for(Ingredient i : ingredients){
if(i.getName().equals(name)){
return i;
}
}
return null;
}
I would recommend either
A) returning the already created ingredient
Or if that would confuse the caller,
B) throwing an exception
This can be a simple IllegalArgumentsException, or depending on your needs, a custom exception class.

Create an object that holds objects?

I am attempting to create an inventory tracking system. I have a class (in Java) called "InventoryItem" with the properties of name and quantity.
This works fine for simple objects, but what if my inventory item contains other inventory items, for example, a server with RAM?
Should I be creating my own datatype, or is there a better way to do this (linked listed maybe)? should my class extend whatever that datatype is or should I not bother creating my own class?
My class so far:
public class InventoryItem {
private String name;
private int quantity;
private InventoryItem childInventoryItem;
// CONSTRUCTORS
public InventoryItem() {
}
public InventoryItem(int quantity, String name) {
this.quantity = quantity;
this.name = name;
}
//GETTERS
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
//SETTERS
public void setName(String name) {
this.name = name;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
A tree is usually what is involved in any parent-child relationship. If you aren't doing anything complicated, you can simply maintain an internal list that is basically List<InventoryItem> which contains any child items.
So all you would add to your class is something like this:
public class InventoryItem {
...
private List<InventoryItem> composingItems = new ArrayList<>(); //if still using Java 6 this must be new ArrayList<InventoryItem>();
...
public void addComposingItem(InventoryItem composingItem) {
composingItems.add(composingItems);
}
public List<InventoryItem> getComposingItems() {
//Enforce immutability so no one can mess with the collection. However
//this doesn't guarantee immutability for the objects inside the list;
//you will have to enforce that yourself.
return Collections.umodifiableList(composingItems);
}
}
There are many ways you can do this. I think the easiest way would be to create an array list.
ArrayList<InventoryItem> childInventory = new ArrayList<InventoryItem>();
Then create a setter that adds inventory items to this array
public void addChildItem(InventoryItem child)
childInventory.add(child);
This way you would have a list of all of the child items within the item. You could also make a method to return a list of all of the child items in either an array or an ArrayList.
public ArrayList<InventoryItem> getChildItems()
return childInventory;

How to apply range criteria for List of Objects?

I have one Object say
class ABC{
private String name;
private String code;
private BigDecimal priceM;
private BigDecimal priceL;
private BigDecimal priceO;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public BigDecimal getPriceM() {
return priceM;
}
public void setPriceM(BigDecimal priceM) {
this.priceM = priceM;
}
public BigDecimal getPriceL() {
return priceL;
}
public void setPriceL(BigDecimal priceL) {
this.priceL = priceL;
}
public BigDecimal getPriceO() {
return priceO;
}
public void setPriceO(BigDecimal priceO) {
this.priceO = priceO;
}
}
Now, say i have a list of ABC and at some point i want that list to filter according to searchCriteria.
Is there any efficient way to achieve this...???
Example ,
I have
AIM :
List and i want new List which will contain List of ABC with priceM values between 100 to 200
Google Guava have some classes around Matchers and Predicate maybe that can help
http://java.dzone.com/articles/google-guava-goodness-matching
Maybe you can use reflection to achieve automatic matching
The new APIs in JDK 8
List<ABC> items = asList(...);
Stream<ABC> stream = items.stream();
stream.filter( item -> item.getPrice() >= 100 && item.getPrice() <= 200 )
.forEach ( System.out::println );
Java provide a Collection.sort(list, comp) method which takes a list and Comparator object as argument
In that case pass your original list which need to be sort and create a Comparator object and override the compare method which is rules for sorting.
You can create multiple Comparator object based on your requirement.
Please refer below link for more help.
http://www.vogella.com/blog/2009/08/04/collections-sort-java/
How do I sort a list by different parameters at different timed

How to call a method from another class in Java

So, I have this class:
public class Product {
private String name, id, info ;
private int quantity;
public Product(String newName, String newID, String newInfo, Integer newQuantity){
setName(newName);
setID(newID);
setPrice(newInfo);
setQuantity(newQuantity);}
public void setName(String name) {
this.name = name; }
public void setID(String id) {
this.id = id; }
public void setPrice(String info) {
this.info = info; }
public void setQuantity(Integer quantity) {
this.quantity = quantity; }
public String getID( ) {
return id; }
public String getName( ) {
return name; }
public String getInfo( ) {
return info; }
public int getQuantity( ) {
return quantity; }
In another class i have this:
public class Invoice implements Group<Product> {
private HashMap<String, Product> prod = new HashMap<String, Product>( );
public Invoice(){ }
public void addProd(Product a) {
prod.put(??getID()??,new Product(??));
}
}
If this data was user generated rather than me, I would use the getID() method right?
So in my class invoice, how do i use the method getID(), so that I can use it in the parameter for my key value in the HashMap? Also is there a way to add 3 values (name info quan) to the hashmap without making a new class?
I see that you get Product object with ref "a" as parameter to your addProd method.
And you can get id by just using a.getID(). It should look as:
public void addProd(Product a) {
prod.put(a.getID(),a);
}
I didn't understand second part of your question.. I think you already have 3 values in your Product object and you put Product object to Map, So why do you require another way ?
Your class Product does not compile, because you have the name Item in your constructor. The constructor name must match the class name. So change that to Product. The same applies to Invoice vs ShoppingCart. Constructor and Class names must match.
As per your comment, you'd like to add four product values to a Map. The key being one of the values of the product itself. Try this:
Product p = new Product(name, id, info, quantity);
cart.addProd(p);
...
public void addProd(Product p) {
prod.put(p.getId(), p);
}
Maps can only map a single value to a single key, so you must have some sort of container for the values you wish to collate into one value. This can be an object (Product) or you could use a collection (e.g. List). I strongly recommend the former.
For your question about putting 3 values in your map, I don't think there's a way for you to put 3 values into one key without creating a class. An alternative is to store a Map<String, List<String>> assuming your 3 values are type String, or, Map<String, Map<String, String>>.

Categories

Resources