java中的remove函数,Java ConcurrentSkipListSet remove()用法及代码示例

java.util.concurrent.ConcurrentSkipListSet.remove()方法是Java中的内置函数,用于删除元素(如果此集合中存在)。

用法:

ConcurrentSkipListSet.remove(Object o)

参数:该函数接受单个参数,即要删除的对象。

返回值:成功删除对象后,该函数将返回true布尔值,否则返回false。

以下示例程序旨在说明ConcurrentSkipListSet.remove()方法:

示例1:集合中存在要删除的元素。

// Java Program Demonstrate remove()

// method of ConcurrentSkipListSet

import java.util.concurrent.ConcurrentSkipListSet;

class ConcurrentSkipListSetRemoveExample1 {

public static void main(String[] args)

{

// Initializing the set

ConcurrentSkipListSet set =

new ConcurrentSkipListSet();

// Adding elements to this set

for (int i = 1; i <= 5; i++)

set.add(i);

// Printing the elements of the set

System.out.println("The elements in the set are:");

for (Integer i : set)

System.out.print(i + " ");

// remove() method will remove the specified

// element from the set

set.remove(1);

set.remove(5);

// Printing the elements of the set

System.out.println("\nRemaining elements in set : ");

for (Integer i : set)

System.out.print(i + " ");

}

}

输出:

The elements in the set are:

1 2 3 4 5

Remaining elements in set :

2 3 4

示例2:集合中不存在要删除的元素。

// Java Program Demonstrate remove()

// method of ConcurrentSkipListSet

import java.util.concurrent.ConcurrentSkipListSet;

class ConcurrentSkipListSetRemoveExample2 {

public static void main(String[] args)

{

// Initializing the set

ConcurrentSkipListSet set =

new ConcurrentSkipListSet();

// Adding elements to this set

for (int i = 10; i <= 15; i++)

set.add(i);

// Printing the elements of the set

System.out.println("The elements in the set are:");

for (Integer i : set)

System.out.print(i + " ");

// remove() method will remove the specified

// element from the set

set.remove(1);

set.remove(5);

// Printing the elements of the set

System.out.println("\nRemaining elements in set : ");

for (Integer i : set)

System.out.print(i + " ");

}

}

输出:

The elements in the set are:

10 11 12 13 14 15

Remaining elements in set :

10 11 12 13 14 15

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值