Java
  Home arrow Java arrow Page 3 - What`s New in Java 1.5 Tiger?
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

What`s New in Java 1.5 Tiger?
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2005-05-19

    Table of Contents:
  • What`s New in Java 1.5 Tiger?
  • Using Queues
  • Ordering Queues Using Comparators
  • Overriding Return Types
  • Taking Advantage of Better Unicode
  • Adding StringBuilder to the Mix

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More Java Articles
    More By O'Reilly Media


     

    Buy this book now. This article was taken from chapter one of Java 1.5 Tiger: A Developer's Notebook, written by Brett McLaughlin and David Flanagan (O'Reilly, 2004; ISBN: 0596007388). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek