Merlin's Magic: maintaining the Insertion Order

Source: Internet
Author: User
Tags arrays constructor hash month name

J2SE 1.4 introduces two new implementations for the Java collections Framework, Linkedhashset and Linkedhashmap. The benefit of adding these two new implementations is that the hash set can now maintain the two paths that run through its elements. In addition to the standard hash relationships, there is now a list of links that can traverse the entire collection. Normally, this new second path follows the insertion order, this means that the iterator of the collection returns elements in the order in which they are inserted (rather than in the order in which they are grouped into a collection by their hash code), but Linkedhashmap supports the second sort option: maintaining the list in Access order rather than insert order.

Let's take a look at how these new classes work.

Begin

It is easy to start using these new classes. Just import the Java.util package and find a set of items to use. In our example, we will use the month of the calendar table. We will use the English month name when using the set, and the name of the month in English and Italian when using the mapping table.

Listing 1. Start defining classes

import java.util.*;
public class OrderedAccess {
  public static void main(String args[]) {
   String months[] =
    new DateFormatSymbols().getMonths();
   String italianMonths[] =
    new DateFormatSymbols(Locale.ITALIAN).getMonths();
  }
}

I will assume that you already know the name and order of the English month. For those unfamiliar with the Italian month name, they are: Gennaio, Febbraio, Marzo, Aprile, Maggio, Giugno, Luglio, Agosto, settembre, ottobre, novembre, and Dicembre, although the name returned by Getmonths () is not capitalized for some reason.

Using the new HashSet

Linkedhashset is a subclass of the basic HashSet class. Therefore, all hashset can do the work, Linkedhashset also can do. There are no new methods in the class. There are only 4 constructors you can get:

LinkedHashSet()

   LinkedHashSet(Collection c)

   LinkedHashSet(int initialCapacity)

   LinkedHashSet(int initialCapacity, float loadFactor)

To add elements to the set, we can call add () for each element, or create a Collection and pass it to the constructor. Because the element is already in the array, the simplest mechanism is to use arrays.aslist (), which returns an array wrapped in a list while maintaining the order of the original array. By passing the list to the constructor, we can easily add the same list to the Linkedhashset and simple hashset.

Listing 2. Fill Set

List list = Arrays.asList(months);
  Set orderedSet = new LinkedHashSet(list);
  Set unorderedSet = new HashSet(list);

After filling up the set, we can examine their elements to see if the linked sets maintain their elements in the order in which they were inserted, and then compare the results with the standard hash set. You can manually iterate through the elements of each set through the iterator () of the set, or call the ToString () method (implicitly), which is actually what it does for us.

Listing 3. Display Set Results

System.out.println("Ordered:  " + orderedSet);
  System.out.println("Unordered: " + unorderedSet);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.