Home arrow Java arrow Page 3 - What`s New in Java 1.5 Tiger?
JAVA

What`s New in Java 1.5 Tiger?


Call it Java 1.5, 2.0, Java 5, Tiger, or what have you -- this version of Java has a lot to offer. This article covers just some of the new features. It is excerpted from chapter one of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
May 19, 2005
TABLE OF CONTENTS:
  1. · What`s New in Java 1.5 Tiger?
  2. · Using Queues
  3. · Ordering Queues Using Comparators
  4. · Overriding Return Types
  5. · Taking Advantage of Better Unicode
  6. · Adding StringBuilder to the Mix

print this article
SEARCH DEVARTICLES

What`s New in Java 1.5 Tiger? - Ordering Queues Using Comparators
(Page 3 of 6 )

While FIFO is a useful paradigm, there are times when youíll want a queue-like structure, ordered by another metric. This is exactly the purpose of PriorityQueue, another Queue implementation. You provide it a Comparator, and it does the rest.

How do I do that?

PriorityQueue works just as any other Queue implementation, and you donít even need to learn any new methods. Instead of performing FIFO ordering, though, a PriorityQueue orders its items by using the Comparator interface. If you create a new queue and donít specify a Comparator, you get whatís called natural ordering, which applies to any classes that implement Comparable. For numerical values, for instance, this places highest values, well, highest! Hereís an example:

  PriorityQueue<Integer> pq=
    new PriorityQueue<Integer>(20);
 
// Fill up with data, in an odd order
  for (int i=0; i<20; i++) {
   
pq.offer(20-i);
  }
 
// Print out and check ordering
  for (int i=0; i<20; i++) {
    
System.out.println(pq.poll());
  }

Since no Comparator implementation is given to PriorityQueue, it orders the numbers lowest to highest, even though theyíre not added to the queue in that order. So when peeling off elements, the lowest item comes out first:

  [echo] Running PriorityQueueTester...
  [java] 1
  [java] 2
  [java] 3
  [java] 4
  [java] 5
  [java] 6
  [java] 7
  [java] 8
  [java] 9
  [java] 10
  [java] 11
  [java] 12
  [java] 13
  [java] 14
  [java] 15
  [java] 16
  [java] 17
  [java] 18
  [java] 19
  [java] 20

However, this queue starts to really come into its own when you provide your own comparator, as shown in Example 1-3. This is done
via the constructor, and a custom implementation of java.util.Comparator.

Example 1-3Using a PriorityQueue

  package com.oreilly.tiger.ch01;
 
import java.util.Comparator;
  import java.util.PriorityQueue;
  import java.util.Queue;
 
public class PriorityQueueTester {
   
public static void main(String[] args) {
      
PriorityQueue<Integer> pq =
        new PriorityQueue<Integer>(20,
          new Comparator<Integer>() {
            public int compare(Integer i, Integer j) {
              int result = i%2 - j%2;
              if (result == 0)
               
result = i-j;
                return result;
            }
          }
        );

      // Fill up with data, in an odd order
      for (int i=0; i<20; i++) {
        pq.offer(20-i);
      }
     
// Print out and check ordering
      for (int i=0; i<20; i++) {
        System.out.println(pq.poll());
      }
    }
  }

The output from this is lowest to highest even numbers, and then lowest to highest odd numbers:

  [echo] Running PriorityQueueTester...
  [java] 2
  [java] 4
  [java] 6
  [java] 8
  [java] 10
  [java] 12
  [java] 14
  [java] 16
  [java] 18
  [java] 20
  [java] 1
  [java] 3
  [java] 5
  [java] 7
  [java] 9
  [java] 11
  [java] 13
  [java] 15
  [java] 17
  [java] 19


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials