LinkedList boolean remove(Object o) method Example Program


Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.remove(Object o) method removes the
 *              first occurrence of the specified element from this list, if it
 *              is present. If this list does not contain the element, it is
 *              unchanged.
 */
public class LinkedListRemoveObject {
	public static void main(String[] args) {
		LinkedList list = new LinkedList();
		list.add("anandh");
		list.add("hari");
		list.add("vinoth");
		list.add("mohan");
		list.add("kamal");
		System.out.println("LinkedList:" + list);
		System.out.println("kamal is in the list:" + list.remove("kamal"));
		System.out.println("LinkedList:" + list);
	}
}

Output

LinkedList:[anandh, hari, vinoth, mohan, kamal]
kamal is in the list:true
LinkedList:[anandh, hari, vinoth, mohan]

Explanation

public boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Specified by:
remove in interface Collection<E>

Specified by:
remove in interface Deque<E>

Specified by:
remove in interface List<E>

Overrides:
remove in class AbstractCollection<E>

Parameters:
o - element to be removed from this list, if present

Returns:
true if this list contained the specified element


Related Post

Comments


©candidjava.com