JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

NavigableSet – OCPJP 6

February 2, 2014 by Krishna Srinivasan Leave a Comment

NavigableSet API is included in the ocpjp 6 certification exam. This article explains few important methods with simple example program.NavigableSet is the subinterface of SortedSet. This interface defines methods for finding the element in a list. For example lower() method used for finding the element which is less than the given value. Look into the following example:

[code lang=”java”]
package javabeat.net;

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetExample1 {
public static void main(String args[]){
List<Integer> list = new ArrayList<Integer>();
list.add(3);
list.add(4);
list.add(1);
list.add(8);
list.add(7);
list.add(10);

NavigableSet navigableSet = new TreeSet(list);
System.out.println(navigableSet.lower(8));
System.out.println(navigableSet.higher(8));

}
}

[/code]

In the above code, NavigableSet.lower() method is used for reteriving the value which is less than ‘8’ in the list. Same way NavigableSet.higher() is used for reteriving the value greater than ‘8’ in the list. The output for the above program would be:

[code]
7
10
[/code]

Other than finding less than or greater than values, NavigableSet provides few more useful methods to print the values in the desired order. Look into the following code snippet:

[code lang=”java”]
package javabeat.net;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetExample2 {
public static void main(String args[]){
List<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(6);
list.add(9);
list.add(5);
NavigableSet navigableSet = new TreeSet(list);
Iterator<Integer> iterator = (Iterator)navigableSet.descendingIterator();
while (iterator.hasNext()){
Integer value = iterator.next();
System.out.println(value);
}

}
}
[/code]

Above code demonstrates how to print the values in decending order using the NaviogableSet API

4) What will be the output of the following program?

[code lang=”java”]
package navset;
import java.util.TreeSet;
public class Ques02 {
public static void main(String[] args) {
MyNavigableSet objects = new MyNavigableSet();
objects.add(new Object());
System.out.println(objects);
}
}
class MyNavigableSet extends TreeSet<String>{
public boolean add(Object value){
return super.add(value);
}
}
[/code]

  1. The program will compile and run fine to print the contents of the set.
  2. The program will raise a run-time exception telling that it is not possible to add objects of type ‘java.lang.Object’ in place of ‘java.lang.String’.
  3. The program will raise a compile-time error.

Get more questions on NavigableSet

Answer

4) c.

A compile-time error telling that a name-clash has happened for the overridden method taking Object as argument.

Filed Under: Certifications Tagged With: OCPJP, OCPJP 6

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved