Awesome q2a theme



0 votes
13 views
1. NavigableMap Example

package com.example.collection;

import java.util.NavigableMap;

import java.util.TreeMap;

public class NavigableMapDemo {

  public static void main(String[] args) {

NavigableMap<String,Integer> navigableMap=new TreeMap<String, Integer>();

navigableMap.put("X", 500);

navigableMap.put("B", 600);

navigableMap.put("A", 700);

navigableMap.put("T", 800);

navigableMap.put("Y", 900);

navigableMap.put("Z", 200);

System.out.printf("Descending Set  : %s%n",navigableMap.descendingKeySet());

System.out.printf("Floor Entry  : %s%n",navigableMap.floorEntry("L"));

System.out.printf("First Entry  : %s%n",navigableMap.firstEntry());

System.out.printf("Last Key : %s%n",navigableMap.lastKey());

System.out.printf("First Key : %s%n",navigableMap.firstKey());

System.out.printf("Original Map : %s%n",navigableMap);

System.out.printf("Reverse Map : %s%n",navigableMap.descendingMap());

  }

}

Output

Descending Set  : [Z, Y, X, T, B, A]

Floor Entry  : B=600

First Entry  : A=700

Last Key : Z

First Key : A

Original Map : {A=700, B=600, T=800, X=500, Y=900, Z=200}

Reverse Map : {Z=200, Y=900, X=500, T=800, B=600, A=700}

2. NavigableSet Example

package com.example.collection;

import java.util.Arrays;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetDemo {

 public static void main(String[] args) {

    NavigableSet<String> navigableSet = new TreeSet<String>(Arrays.asList(
            "X", "B", "A", "Z", "T"));

    Iterator<String> iterator = navigableSet.descendingIterator();

    System.out.println("Original Set :");
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

    iterator = navigableSet.iterator();

    System.out.println("Sorted Navigable Set :");

    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

    System.out.printf("Head Set : %s.%n", navigableSet.headSet("X"));

    System.out.printf("Tail Set : %s.%n", navigableSet.tailSet("T", false));

    System.out.printf("Sub Set : %s.%n",
            navigableSet.subSet("B", true, "X", true));

    System.out.printf("Last Element : %s%n", navigableSet.last());

    System.out.printf("First Element : %s%n", navigableSet.first());

    System.out.printf("Reverse Set : %s%n", navigableSet.descendingSet());

    System.out.printf("Original Set : %s%n", navigableSet);

 }

}

Output

Original Set :
Z
X
T
B
A
Sorted Navigable Set :
A
B
T
X
Z
Head Set : [A, B, T].
Tail Set : [X, Z].
Sub Set : [B, T, X].
Last Element : Z
First Element : A
Reverse Set : [Z, X, T, B, A]
Original Set : [A, B, T, X, Z]
asked Sep 11 in Java Interview Questions by rajesh | 13 views



Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

Related Questions



Welcome to Knowledge Boostr, where you can ask questions and receive answers from other members of the community.
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers http://boostr.in Q&A for professional and enthusiast programmers


23,444 questions
40,127 answers
12 comments
23,597 users