Nov 8, 2017

Java Collections.singleton() Example

Java Collections.singleton() Example

public static <T> Set<T> singleton(T o)
Returns an immutable set containing only the specified object. The returned set is serializable.
o - the sole object to be stored in the returned set.
an immutable set containing only the specified object.
   // create an array of string objs
   String init[] = { "i88", "ca", "i88", "ca" };
   // create two lists
   List list1 = new ArrayList(Arrays.asList(init));
   List list2 = new ArrayList(Arrays.asList(init));

   // remove from list1
   System.out.println("List1 value: "+list1);

   // remove from list2 using singleton
   System.out.println("The SingletonList is :"+list2);
/* output
List1 value: [ca, i88, ca]
The SingletonList is :[ca, ca]

Post a Comment

Featured Post

DMARC is a type of email authentication protocol that leverages the widely used SPF and DKIM protocols to improve a sender’s understanding of how their email in circulation is processed.

Email claiming to be from their domain is analyzed by receiving organizations and a digest of acceptance/failures is sent back to the sende...