Home
Login
Contact
Articles
Newsletter
Bookmark and Share
XSLT
CSS
Javascript
Java
Linux
Username
Password
Annuler
Connexion

Recent Articles

javascript print partial pages

In developping web applications and designing websites, you've probably come accross a situation where you wanted the user to be able to press a print...

Marquee in javascript

The Marquee Element has been deprecated by the W3C and is commonly ill-advised but nevertheless, if you really want to do it, then javascript is the w...

Sending an email in Java

Sending an email in Java is actually quite simple, as always, there is an API that will do most of the work for you and it becomes just a matter of im...

Opacity in Firefox 3.5

If you've upgraded to Firefox 3.5 and you've been using -moz-opacity in your CSS, then you will see that the transparency or opacity (depending on ho...

Installing Tomcat on Linux in a few minutes

Installing tomcat is actually very quick and easy. Assuming you already have the JDK installed, this will only take a few minutes. In my years of exp...

Getting unique values from a list in Java

Matt Castonguay, 27 June at 03:07PM 

Extracting unique values from a list is a common problem and the goal is to do it in as little operations as possible. The memory used is negligeable and this can be done in linear time.

The goal is to use a map who's keys are the values. We can therefore test to see if the element is already in the list in O(1) and insure linear time of the algorithm.

public static <T extends Object> List<T> getUniques(List<T> list) {
    //Initiate the necessary variables.

    final List<T> uniques = new ArrayList<T>();
    //We declare the map to be final and it will be garbage
    //collected at the end of this method's execution, making     //the space used by the map temporary.

    final HashMap<T,T> hm = new HashMap<T,T>();
    //Loop through all of the elements.

    for (T t : list) {
        //If you don't find the object in the map, it is
        //unique and we add it to the uniques list.

        if (hm.get(t) == null) {
            hm.put(t,t);
            uniques.add(t);
        }
    }
    return uniques;
}

 

The above example shows a method that will accept a list of any extension of Objects using generic templates. However, it should be noted that HashMap uses the Object's hash code for a unique identifier. If this method is overridden, it is possible that HashMaps may not work properly with those Objects.

Note that it would be possible to check for unique on insert of the list, making this method unecessary, but it would mean having to store the map along with the list. The trade off of space and memory depends on the size of the list and the need for a live list of uniques.

 

Category:   Java

Tags:   uniques, hashmap

Random Tags

xml    opacity    float    ie6    xsl:import    css classes    library    hashmap    template    tomcat    caching    double negation    memory    min-height    bash    xsl:if    svnant    mode    ant    wysiwyg    javamail    math    uniques    apply-templates    iframe    firefox    position    print    min-width    weakhashmap    clear    url mapping    email    svn    apache    marquee    modproxy    proxypass    css classes    custom css   
MG2 Media Inc. - A Web Development Company
Top