Array Map : Customized Map « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » Customized MapScreenshots 
Array Map
    
/*
 * Copyright (c) 2002-2008 "Neo Technology,"
 *     Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class ArrayMap<K,V>
{
    private ArrayEntry<K,V>[] arrayEntries;

    private volatile int arrayCount = 0;
    private int toMapThreshold = 5;
    private Map<K,V> propertyMap = null;
    private final boolean useThreadSafeMap;
    private boolean switchBackToArray = false;

    public ArrayMap()
    {
        useThreadSafeMap = false;
        arrayEntries = new ArrayEntry[toMapThreshold];
    }

    public ArrayMapint mapThreshold, boolean threadSafe, boolean shrinkToArray )
    {
        this.toMapThreshold = mapThreshold;
        this.useThreadSafeMap = threadSafe;
        this.switchBackToArray = shrinkToArray;
        arrayEntries = new ArrayEntry[toMapThreshold];
    }

    public void putK key, V value )
    {
        if useThreadSafeMap )
        {
            synchronizedPutkey, value );
            return;
        }
        for int i = 0; i < arrayCount; i++ )
        {
            if arrayEntries[i].getKey().equalskey ) )
            {
                arrayEntries[i].setNewValuevalue );
                return;
            }
        }
        if arrayCount != -)
        {
            if arrayCount < arrayEntries.length )
            {
                arrayEntries[arrayCount++new ArrayEntry<K,V>key, value );
            }
            else
            {
                propertyMap = new HashMap<K,V>();
                for int i = 0; i < arrayCount; i++ )
                {
                    propertyMap.putarrayEntries[i].getKey(), arrayEntries[i]
                        .getValue() );
                }
                arrayCount = -1;
                propertyMap.putkey, value );
            }
        }
        else
        {
            propertyMap.putkey, value );
        }
    }

    private synchronized void synchronizedPutK key, V value )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if arrayEntries[i].getKey().equalskey ) )
            {
                arrayEntries[i].setNewValuevalue );
                return;
            }
        }
        if arrayCount != -)
        {
            if arrayCount < arrayEntries.length )
            {
                arrayEntries[arrayCount++new ArrayEntry<K,V>key, value );
            }
            else
            {
                propertyMap = new ConcurrentHashMap<K,V>();
                for int i = 0; i < arrayCount; i++ )
                {
                    propertyMap.putarrayEntries[i].getKey(), arrayEntries[i]
                        .getValue() );
                }
                arrayEntries = null;
                arrayCount = -1;
                propertyMap.putkey, value );
            }
        }
        else
        {
            propertyMap.putkey, value );
        }
    }

    public V getK key )
    {
        if key == null )
        {
            return null;
        }
        if useThreadSafeMap )
        {
            return synchronizedGetkey );
        }
        int count = arrayCount;
        for int i = 0; i < count; i++ )
        {
            if key.equalsarrayEntries[i].getKey() ) )
            {
                return arrayEntries[i].getValue();
            }
        }
        if arrayCount == -)
        {
            return propertyMap.getkey );
        }
        return null;
    }

    private synchronized V synchronizedGetK key )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if key.equalsarrayEntries[i].getKey() ) )
            {
                return arrayEntries[i].getValue();
            }
        }
        if arrayCount == -)
        {
            return propertyMap.getkey );
        }
        return null;
    }

    private synchronized V synchronizedRemoveK key )
    {
        for int i = 0; i < arrayCount; i++ )
        {
            if arrayEntries[i].getKey().equalskey ) )
            {
                V removedProperty = arrayEntries[i].getValue();
                arrayCount--;
                System.arraycopyarrayEntries, i + 1, arrayEntries, i,
                    arrayCount - i );
                return removedProperty;
            }
        }
        if arrayCount == -)
        {
            V value = propertyMap.removekey );
            if switchBackToArray && propertyMap.size() < toMapThreshold )
            {
                arrayEntries = new ArrayEntry[toMapThreshold];
                int tmpCount = 0;
                for Entry<K,V> entry : propertyMap.entrySet() )
                {
                    arrayEntries[tmpCount++new ArrayEntry<K,V>entry
                        .getKey(), entry.getValue() );
                }
                arrayCount = tmpCount;
            }
            return value;
        }
        return null;
    }

    public V removeK key )
    {
        if useThreadSafeMap )
        {
            return synchronizedRemovekey );
        }
        for int i = 0; i < arrayCount; i++ )
        {
            if arrayEntries[i].getKey().equalskey ) )
            {
                V removedProperty = arrayEntries[i].getValue();
                arrayCount--;
                System.arraycopyarrayEntries, i + 1, arrayEntries, i,
                    arrayCount - i );
                return removedProperty;
            }
        }
        if arrayCount == -)
        {
            V value = propertyMap.removekey );
            if switchBackToArray && propertyMap.size() < toMapThreshold )
            {
                arrayEntries = new ArrayEntry[toMapThreshold];
                int tmpCount = 0;
                for Entry<K,V> entry : propertyMap.entrySet() )
                {
                    arrayEntries[tmpCount++new ArrayEntry<K,V>entry
                        .getKey(), entry.getValue() );
                }
                arrayCount = tmpCount;
            }
            return value;
        }
        return null;
    }

    static class ArrayEntry<K,V> implements Entry<K,V>
    {
        private K key;
        private V value;

        ArrayEntryK key, V value )
        {
            this.key = key;
            this.value = value;
        }

        public K getKey()
        {
            return key;
        }

        public V getValue()
        {
            return value;
        }

        void setNewValueV value )
        {
            this.value = value;
        }

        public V setValueV value )
        {
            V oldValue = value;
            this.value = value;
            return oldValue;
        }
    }

    public Iterable<K> keySet()
    {
        if arrayCount == -)
        {
            return propertyMap.keySet();
        }
        List<K> keys = new LinkedList<K>();
        for int i = 0; i < arrayCount; i++ )
        {
            keys.addarrayEntries[i].getKey() );
        }
        return keys;
    }

    public Iterable<V> values()
    {
        if arrayCount == -)
        {
            return propertyMap.values();
        }
        List<V> values = new LinkedList<V>();
        for int i = 0; i < arrayCount; i++ )
        {
            values.addarrayEntries[i].getValue() );
        }
        return values;
    }
    
    public Set<Entry<K,V>> entrySet()
    {
        if arrayCount == -)
        {
            return propertyMap.entrySet();
        }
        Set<Entry<K,V>> entries = new HashSet<Entry<K,V>>();
        for int i = 0; i < arrayCount; i++ )
        {
            entries.addarrayEntries[i] );
        }
        return entries;
    }

    public int size()
    {
        if arrayCount != -)
        {
            return arrayCount;
        }
        return propertyMap.size();
    }

    public void clear()
    {
        if arrayCount != -)
        {
            arrayCount = 0;
        }
        else
        {
            propertyMap.clear();
        }
    }
}

   
    
    
    
  
Related examples in the same category
1. Ordered Map
2. Case Insensitive Map
3. A Map collection with real-time behavior
4. Cache Map
5. Map implementation Optimized for Strings keys
6. An integer hashmap
7. An IdentityMap that uses reference-equality instead of object-equality
8. Int Object HashMap
9. Concurrent Skip List Map
10. A hash map that uses primitive ints for the key rather than objects.
11. Integer Map
12. Copy On Write Map
13. Expiring Map
14. Int Object HashMap (from CERN)
15. Int HashMap from jodd.org
16. String Map
17. List Map
18. Map using Locale objects as keys
19. Map with keys iterated in insertion order
20. Most Recently Used Map
21. Multi Map
22. MultiMap is a Java version of the C++ STL class std::multimap
23. Object Int Map
24. Sequenced HashMap
25. Int Int Map
26. Int Object Map
27. Identity HashMap
28. A java.util.Map interface which can only hold a single object
29. A multi valued Map
30. A simple hashmap from keys to integers
31. A memory-efficient hash map.
32. An implementation of the java.util.Map interface which can only hold a single object.
33. Utility methods for operating on memory-efficient maps.
34. CaseBlindHashMap - a HashMap extension, using Strings as key values.
35. A fixed size map implementation.
36. Int HashMap
37. IntMap provides a simple hashmap from keys to integers
38. Complex Key HashMap
39. A Map with multiple values for a key
40. A Map that accepts int or Integer keys only
41. A Map where keys are compared by object identity, rather than equals()
42. Type-safe Map, from char array to String value
43. A hashtable-based Map implementation with soft keys
44. List ordered map
45. Hash map using String values as keys mapped to primitive int values.
46. Lookup table that stores a list of strings
47. HashNMap stores multiple values by a single key value. Values can be retrieved using a direct query or by creating an enumeration over the stored elements.
48. Combines multiple values to form a single composite key. MultiKey can often be used as an alternative to nested maps.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.