只移除一个而不是所有项目在链表中。

huangapple go评论78阅读模式
英文:

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&lt;Item&gt; items = new LinkedList&lt;&gt;();

items.add(new Item(&quot;Movie1&quot;, &quot;2010&quot;, &quot;2&quot;));
items.add(new Item(&quot;Movie1&quot;, &quot;2010&quot;, &quot;2&quot;));

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) &amp;&amp; this.year.equals(other.year) &amp;&amp; this.id.equals(other.id);
}
@Override
public String toString() {
return &quot;Name: &quot; + name + &quot;, Year: &quot; + year + &quot;, ID: &quot; + id;
}
}
public class Main {
public static void main(String[] args) {
LinkedList&lt;Item&gt; items = new LinkedList&lt;Item&gt;();
Item item1 = new Item(&quot;Movie1&quot;, &quot;2010&quot;, &quot;2&quot;);
Item item2 = new Item(&quot;Movie2&quot;, &quot;2020&quot;, &quot;3&quot;);
items.add(item1);
items.add(item1);
items.add(item2);
System.out.println(&quot;Before: &quot;);
System.out.println(items);
remove(items, item1);
remove(items, item2);
System.out.println(&quot;After: &quot;);
System.out.println(items);
// Trying to remove item1 again
remove(items, item1);
System.out.println(&quot;Result: &quot;);
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&lt;Item&gt; items, Item item) {
for (int i = 0; i &lt; 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(&quot;Movie1&quot;, &quot;2010&quot;, &quot;2&quot;);
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.

huangapple
  • 本文由 发表于 2020年5月5日 20:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定