I am trying to check if types of item is full or not in list.
private LinkedList<Item> items = new LinkedList<>();
items.add(new Item("Movie1", "2020", "2"));//2 right there's the amount
items.add(new Item("Movie2", "2012", "3"));
items.add(new Item("Movie3", "2011", "3"));
items.add(new Item("Movie4", "2013", "3"));
I make a limit of 3. So, if the amount is 3, then it's full.
when I say three is the amount in Movie2,2020, 3
. I mean that three. Sry for misunderstanding.
I only know how to check if a one of the item is full. But there are multiple are full.
I am struggle to check them.
Can anyone help?
Thanks.
Define a method as follows to add elements to the list:
static void add(LinkedList<Item> items, Item item) {
boolean full = false;
for (Item e : items) {
if (e.equals(item) && e.getId().equals(String.valueOf(LIMIT)) && Integer.valueOf(item.getId()) >= LIMIT) {
full = true;
}
}
if (full) {
System.out.println("There is no room for " + item + " in the list");
} else {
items.add(item);
}
}
Demo:
import java.util.LinkedList;
import java.util.Objects;
class Item {
String name, year, id;
public Item(String name, String year, String id) {
this.name = name;
this.year = year;
this.id = id;
}
public String getName() {
return name;
}
public String getYear() {
return year;
}
public String getId() {
return id;
}
@Override
public int hashCode() {
return Objects.hash(name, year);
}
@Override
public boolean equals(Object obj) {
Item other = (Item) obj;
return this.name.equals(other.name) && this.year.equals(other.year);
}
@Override
public String toString() {
return "[Name: " + name + ", Year: " + year + ", ID: " + id + "]";
}
}
public class Main {
final static int LIMIT = 3;
public static void main(String[] args) {
LinkedList<Item> items = new LinkedList<Item>();
add(items, new Item("Movie1", "2010", "1"));
add(items, new Item("Movie1", "2010", "2"));
add(items, new Item("Movie1", "2010", "3"));
add(items, new Item("Movie1", "2010", "3"));
add(items, new Item("Movie1", "2010", "4"));
add(items, new Item("Movie2", "2020", "2"));
items.stream().forEach(System.out::println);
}
static void add(LinkedList<Item> items, Item item) {
boolean full = false;
for (Item e : items) {
if (e.equals(item) && e.getId().equals(String.valueOf(LIMIT)) && Integer.valueOf(item.getId()) >= LIMIT) {
full = true;
}
}
if (full) {
System.out.println("There is no room for " + item + " in the list");
} else {
items.add(item);
}
}
}
Output:
There is no room for [Name: Movie1, Year: 2010, ID: 3] in the list
There is no room for [Name: Movie1, Year: 2010, ID: 4] in the list
[Name: Movie1, Year: 2010, ID: 1]
[Name: Movie1, Year: 2010, ID: 2]
[Name: Movie1, Year: 2010, ID: 3]
[Name: Movie2, Year: 2020, ID: 2]
Note: This solution is based on the requirement that the id
field is not computed as part of equals()
ie two items are equal if their name
and year
are equal.
@Override
public int hashCode() {
return Objects.hash(name, year);
}
@Override
public boolean equals(Object obj) {
Item other = (Item) obj;
return this.name.equals(other.name) && this.year.equals(other.year);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.