» » Check if a particular value exists in Java TreeSet example

1.Basic Java program to check particular value present in TreeSet or not?

  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetDemo{ 
  3.  
  4. public static void main(String args[]) {
  5. //create TreeSet object
  6.  TreeSet<String> treeSet = new TreeSet<String>();
  7.        
  8.  //add elements to TreeSet
  9.   treeSet.add("1");
  10.   treeSet.add("3");
  11.   treeSet.add("2");
  12.   treeSet.add("5");
  13.   treeSet.add("6");
  14.   treeSet.add("7");
  15.   treeSet.add("8");
  16.   treeSet.add("9");
  17.   treeSet.add("10");
  18.         
  19.  boolean isExists = treeSet.contains("5");
  20.  System.out.println("5 exists in treeSet ?  " + isExists); 

  21. }
  22. }



Output:


  1. 5 exists in treeSet ? :true

1.Basic Java program to add elements ans display all elements



  1. package com.TreeSetExamplePrograms;
  2. import java.util.Scanner;
  3. public Class TreeSetDemo{ 
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  TreeSet<Integer> treeSet = new TreeSet<Integer>();
  8.        
  9.  //add elements to TreeSet
  10.   treeSet.add(1);
  11.   treeSet.add(3);
  12.   treeSet.add(2);
  13.   treeSet.add(5);
  14.   treeSet.add(6);
  15.   treeSet.add(7);
  16.   treeSet.add(8);
  17.   treeSet.add(9);
  18.   treeSet.add(10);
  19.         

  20.  System.out.println(treeset); 

  21. }
  22. }

Output:


  1. [1, 2, 3, 5, 6, 7, 8, 9, 10]

treeset add elements in java example program

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu