com.ibm.icu.text
Class AlphabeticIndex<V>

java.lang.Object
  extended by com.ibm.icu.text.AlphabeticIndex<V>
All Implemented Interfaces:
Iterable<AlphabeticIndex.Bucket<V>>

public final class AlphabeticIndex<V>
extends Object
implements Iterable<AlphabeticIndex.Bucket<V>>

AlphabeticIndex supports the creation of a UI index appropriate for a given language. It can support either direct use, or use with a client that doesn't support localized collation. The following is an example of what an index might look like in a UI:

  ... A B C D E F G H I J K L M N O P Q R S T U V W X Y Z  ...
  
  A
     Addison
     Albertson
     Azensky
  B
     Baecker
  ...
 
The class can generate a list of labels for use as a UI "index", that is, a list of clickable characters (or character sequences) that allow the user to see a segment (bucket) of a larger "target" list. That is, each label corresponds to a bucket in the target list, where everything in the bucket is greater than or equal to the character (according to the locale's collation). Strings can be added to the index; they will be in sorted order in the right bucket.

The class also supports having buckets for strings before the first (underflow), after the last (overflow), and between scripts (inflow). For example, if the index is constructed with labels for Russian and English, Greek characters would fall into an inflow bucket between the other two scripts.

Note: If you expect to have a lot of ASCII or Latin characters as well as characters from the user's language, then it is a good idea to call addLabels(ULocale.English).

Direct Use

The following shows an example of building an index directly. The "show..." methods below are just to illustrate usage.

 // Create a simple index where the values for the strings are Integers, and add the strings
 
 AlphabeticIndex index = new AlphabeticIndex(desiredLocale).addLabels(additionalLocale);
 int counter = 0;
 for (String item : test) {
     index.addRecord(item, counter++); 
 }
 ...
 // Show index at top. We could skip or gray out empty buckets
 
 for (AlphabeticIndex.Bucket bucket : index) {
     if (showAll || bucket.size() != 0) {
         showLabelAtTop(UI, bucket.getLabel());
     }
 }
  ...
 // Show the buckets with their contents, skipping empty buckets
 
 for (AlphabeticIndex.Bucket bucket : index) {
     if (bucket.size() != 0) {
         showLabelInList(UI, bucket.getLabel());
         for (AlphabeticIndex.Record item : bucket) {
             showIndexedItem(UI, item.getName(), item.getData());
         }
 
The caller can build different UIs using this class. For example, an index character could be omitted or grayed-out if its bucket is empty. Small buckets could also be combined based on size, such as:
 ... A-F G-N O-Z ...
 

Client Support

Callers can also use the AlphabeticIndex to support sorting on a client that doesn't support collation.

Notes: