java singleton_Java Collections.singleton()用法及代码示例

java.util.Collections.singleton()方法是java.util.Collections类的方法。它在单个指定的元素上创建一个不可变的集合。此方法的一个应用是从Collection中删除一个元素,例如List和Set。

用法:

public static Set singleton(T obj)

and

public static List singletonList(T obj)

参数: obj:the sole object to be stored in

the returned list or set.

返回: an immutable list/set containing only the

specified object.

Example:

myList:{"Geeks", "code", "Practice", " Error", "Java",

"Class", "Error", "Practice", "Java" }

To remove all "Error" elements from our list at once, we use

singleton() method

myList.removeAll(Collections.singleton("Error"));

After using singleton() and removeAll, we get following.

{"Geeks", "code", "Practice", "Java", "Class", "Practice", "Java" }

// Java program to demonstrate

// working of singleton()

import java.util.*;

class GFG {

public static void main(String args[])

{

String[] geekslist = { "1", "2", "4", "2", "1", "2",

"3", "1", "3", "4", "3", "3" };

// Creating a list and removing

// elements without use of singleton()

List geekslist1 = new ArrayList(Arrays.asList(geekslist));

System.out.println("Original geeklist1:" + geekslist1);

geekslist1.remove("1");

System.out.println("geekslist1 after removal of 1 without"

+ " singleton " + geekslist1);

geekslist1.remove("1");

System.out.println("geekslist1 after removal of 1 without"

+ " singleton " + geekslist1);

geekslist1.remove("2");

System.out.println("geekslist1 after removal of 2 without"

+ " singleton " + geekslist1);

/* Creating another list and removng

its elements using singleton() method */

List geekslist2 = new ArrayList(Arrays.asList(geekslist));

System.out.println("\nOriginal geeklist2:" + geekslist2);

// Selectively delete "1" from

// all it's occurences

geekslist2.removeAll(Collections.singleton("1"));

System.out.println("geekslist2 after removal of 1 with "

+ "singleton:" + geekslist2);

// Selectively delete "4" from

// all it's occurences

geekslist2.removeAll(Collections.singleton("4"));

System.out.println("geekslist2 after removal of 4 with "

+ "singleton:" + geekslist2);

// Selectively delete "3" from

// all it's occurences

geekslist2.removeAll(Collections.singleton("3"));

System.out.println("geekslist2 after removal of 3 with"

+ " singleton:" + geekslist2);

}

}

输出:

Original geeklist1 [1, 2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]

geekslist1 after removal of 1 without singleton [2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]

geekslist1 after removal of 1 without singleton [2, 4, 2, 2, 3, 1, 3, 4, 3, 3]

geekslist1 after removal of 2 without singleton [4, 2, 2, 3, 1, 3, 4, 3, 3]

Original geeklist2 [1, 2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]

geekslist2 after removal of 1 with singleton [2, 4, 2, 2, 3, 3, 4, 3, 3]

geekslist2 after removal of 4 with singleton [2, 2, 2, 3, 3, 3, 3]

geekslist2 after removal of 3 with singleton [2, 2, 2]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值