英文:
Remove only one not all items in linkedlist
问题
我正在尝试从链表中移除一个项目。
private LinkedList<Item> items = new LinkedList<>();
items.add(new Item("Movie1", "2010", "2"));
items.add(new Item("Movie1", "2010", "2"));
我尝试检查项目是否存在于列表中。
如果项目存在,则移除该项目。
但它却移除了所有项目。
我只想移除一个项目。
因此,在移除后的结果应该是:
[Movie1, 2010, 2];
有没有办法可以做到这一点?
谢谢。
英文:
I am trying to remove one of the item in linkedlist.
private LinkedList<Item> items = new LinkedList<>();
items.add(new Item("Movie1", "2010", "2"));
items.add(new Item("Movie1", "2010", "2"));
I try to check if the item exists in the list.
If the item exists then remove the item.
Just it removing all the item.
I just want to remove one item
So, the result should be after removing.
[Movie1, 2010, 2];
Is there any way I can do that?
Thanks.
答案1
得分: 1
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;
}
@Override
public int hashCode() {
return Objects.hash(name, year, id);
}
@Override
public boolean equals(Object obj) {
Item other = (Item) obj;
return this.name.equals(other.name) && this.year.equals(other.year) && this.id.equals(other.id);
}
@Override
public String toString() {
return "Name: " + name + ", Year: " + year + ", ID: " + id;
}
}
public class Main {
public static void main(String[] args) {
LinkedList<Item> items = new LinkedList<Item>();
Item item1 = new Item("Movie1", "2010", "2");
Item item2 = new Item("Movie2", "2020", "3");
items.add(item1);
items.add(item1);
items.add(item2);
System.out.println("Before: ");
System.out.println(items);
remove(items, item1);
remove(items, item2);
System.out.println("After: ");
System.out.println(items);
// Trying to remove item1 again
remove(items, item1);
System.out.println("Result: ");
System.out.println(items);
}
/**
* If list contains more than one occurrence of item, removes the first
* occurrence of item from list
*
* @param items from which the first occurrence of item has to be removed
* @param item
*/
static void remove(LinkedList<Item> items, Item item) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).equals(item)) {
if (items.subList(i + 1, items.size()).contains(items.get(i))) {
items.remove(i);
break;
}
}
}
}
}
Output:
Before:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
After:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
Result:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
<details>
<summary>英文:</summary>
Use [remove(int index)][1]
===
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;
}
@Override
public int hashCode() {
return Objects.hash(name, year, id);
}
@Override
public boolean equals(Object obj) {
Item other = (Item) obj;
return this.name.equals(other.name) && this.year.equals(other.year) && this.id.equals(other.id);
}
@Override
public String toString() {
return "Name: " + name + ", Year: " + year + ", ID: " + id;
}
}
public class Main {
public static void main(String[] args) {
LinkedList<Item> items = new LinkedList<Item>();
Item item1 = new Item("Movie1", "2010", "2");
Item item2 = new Item("Movie2", "2020", "3");
items.add(item1);
items.add(item1);
items.add(item2);
System.out.println("Before: ");
System.out.println(items);
remove(items, item1);
remove(items, item2);
System.out.println("After: ");
System.out.println(items);
// Trying to remove item1 again
remove(items, item1);
System.out.println("Result: ");
System.out.println(items);
}
/**
* If list contains more than one occurrence of item, removes the first
* occurrence of item from list
*
* @param items from which the first occurrence of item has to be removed
* @param item
*/
static void remove(LinkedList<Item> items, Item item) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).equals(item)) {
if (items.subList(i + 1, items.size()).contains(items.get(i))) {
items.remove(i);
break;
}
}
}
}
}
**Output:**
Before:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
After:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
Result:
[Name: Movie1, Year: 2010, ID: 2, Name: Movie2, Year: 2020, ID: 3]
[1]: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(int)
</details>
# 答案2
**得分**: 0
你还可以使用列表的 Collections 包含方法,然后将其移除。
```java
Item item1 = new Item("Movie1", "2010", "2");
if (items.contains(item1)) {
items.remove(item1);
}
另外,为了使上述代码正常工作,你需要在 Item POJO 中重写 hashcode 和 equals 方法。
英文:
You can also use the list Collections contains method and then remove it.
Item item1 = new Item("Movie1", "2010", "2");
if(items.contains(item1){
items.remove(item1)
}
Also, for the above code to work you need to override hashcode and equals method in Item POJO.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论