12:04 pm - Wednesday January 18, 2017

Add elements to treeset with List add()

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to Add elements to treeset using list add() method.

create TreeSet with other collection.

By passing another collection to the TreeSet constructor,

you can copy entire collections elements to the TreeSet.

Example

import java.util.ArrayList;

import java.util.List;

import java.util.TreeSet;

public class MySetWithCollection

{

   public static void main(String a[])

{

       List<String> li = new ArrayList<String>();

       li.add("one");

       li.add("two");

       li.add("three");

       li.add("four");

       System.out.println("List: "+li);

      TreeSet<String> myset = new TreeSet<String>(li);

       System.out.println("Set: "+myset);

   }

}

Output

           List: [one, two, three, four]

           Set: [four, one, three, two]

BACK

Filed in: Java Examples

Comments are closed.