TreeSet elements adding

In this chapter you will learn:

  1. How to add elements to a TreeSet

Add elements to a TreeSet

  • boolean add(E e) Adds the specified element to this set if it is not already present.
  • boolean addAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set.
import java.util.TreeSet;
// j a  va  2  s .  c om
public class Main {
    public static void main(String args[]) {
        TreeSet<String> ts = new TreeSet<String>();

        ts.add("C");
        ts.add("A");
        ts.add("B");
        ts.add("E");
        ts.add("F");
        ts.add("java2s.com");

        System.out.println(ts);
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Get the head set and tail set from a TreeSet
  2. How to get a range of value from a TreeSet
Home » Java Tutorial » Set
HashSet
HashSet element adding
HashSet element removing and clearing
HashSet clone
HashSet iterator
HashSet properties
TreeSet
TreeSet elements adding
TreeSet subSet
NavigableSet
LinkedHashSet