List, Map and Stack implementations in Java
This article describes how to implement data structures (List Stack, Map) in Java.
The implementations in this articles is for demonstration and education purpose. They do don't try to being as efficient as the standard libraries and they is not intended to is an replacement for the Standa RD libraries.
Table of Contents
-
1. Datastructures
-
2. List
-
3. Map-associative Array
-
4. Stack
-
5. About this website
-
-
5.1.
-
6. Links and literature
1. Datastructures
Every programmer requires certain data structures for saving several elements. Usually every programming languages provides Arrays. These is fixed sized storage elements where every element can be addressed via an index.
The programmer usually requires a higher level of abstraction, e.g. Lists, Maps, Stacks, etc.
The Java programming language provides these elements very efficiently implemented in libraries. This article tries-describe how such elements could is implemented directly with Java constructs. The implementations in this articles is for demonstration and education purpose. They do don't try to being as efficient as the standard libraries and they is not intended to is an replacement for the Standa RD libraries.
2. List
See Implementing a LIST data structure in Java.
3. Map-associative Array
The following example is contained in the project "De.vogella.datastructures.map".
A map represents a data structure in which collections of unique key and collections of values is stored where each key I s associated with one value. The operation of finding the value is called lookup.
A standard array was a special case of a map where the key was the index number of the elements pointing to the object in T He array.
The standard Java interfaces for maps is import java.util.Map. Standard implementations of Maps is for example Java.util.HashMap or import Java.util.concurrent.ConcurrentHashMap
A simple map with the option to add, get, remove and get the size of the map could is implemented like the following. Please note the This map was not very fast for large sets.
This would is an entry.
Package De.vogella.datastructures.map;public class Myentry<k, v> {private final K key; private V value; Public Myentry (K key, V value) {this.key = key; This.value = value; } public K GetKey () {return key; } public V GetValue () {return value; public void SetValue (V value) {this.value = value; }} This is the map implementation based on Myentry. Package De.vogella.datastructures.map;import Java.util.arrays;import Java.util.hashset;import java.util.Set;public Class Mymap<k, v> {private int size; private int default_capacity = 16; @SuppressWarnings ("unchecked") private myentry<k, v>[] values = new myentry[default_capacity]; Public V get (K key) {for (int i = 0; i < size; i++) {if (values[i]! = null) {if (Values[i].getkey (). E Quals (key)) {return values[i].getvalue (); }}} return null; } public void put (K key, V value) {Boolean insert = true; for (int i = 0; i < size; i++) { if (Values[i].getkey (). Equals (key)) {Values[i].setvalue (value); Insert = false; }} if (insert) {Ensurecapa (); values[size++] = new myentry<k, v> (key, value); }} private void Ensurecapa () {if (size = = values.length) {int newSize = values.length * 2; Values = arrays.copyof (values, newSize); }} public int size () {return size; } public void Remove (K key) {for (int i = 0; i < size; i++) {if (Values[i].getkey (). Equals (key)) {VA Lues[i] = null; size--; Condensearray (i); }}} private void Condensearray (int start) {for (int i = start; i < size; i++) {Values[i] = values[i + 1]; }} public set<k> KeySet () {set<k> Set = new hashset<k> (); for (int i = 0; i < size; i++) {Set.add (Values[i].getkey ()); } return set; }}
and a small test.
Package De.vogella.datastructures.map;import static Org.junit.assert.assertequals;import Java.util.hashmap;import Java.util.map;import Org.junit.before;import Org.junit.test;public class Mymaptest {@Before public void setUp () throws Exception {} @Test public void Teststandardmap () {//Standard map map<string, integer> map = new hashmap& Lt String, integer> (); Map.put ("Lars", 1); Map.put ("Lars", 2); Map.put ("Lars", 1); Assertequals (Map.get ("Lars"), 1); for (int i = 0; i < i++) {map.put (string.valueof (i), i); } assertequals (Map.size (), 101); Assertequals (Map.get ("51"), 51); Map.keyset (); } @Test public void Testmapmap () {//MyMap mymap<string, integer> map = new mymap<string, integer> (); Map.put ("Lars", 1); Map.put ("Lars", 2); Map.put ("Lars", 1); Assertequals (Map.get ("Lars"), 1); for (int i = 0; i < i++) {map.put (string.valueof (i), i); } assertequals (Map.size (), 101); Assertequals (Map.get ("51"), 51); }}
4. Stack
The following example is contain in the project "De.vogella.datastructures.stack".
The stack offers to put a new object on the stack (method push ()) and to get objects from the stack (method Pop ()). A Stack returns the object according to Last-in-first-out (LIFO), e.g. the object which were placed latest on the stack is Returned first. Java provides a standard implementation of a, stack in Java.util.Stack.
The following is implementations of stacks, one based on arrays and the other based on ArrayLists.
Package De.vogella.datastructures.stack;import Java.util.arrays;public class Mystackarray<e> { private int size = 0; private static final int default_capacity = ten; Private Object elements[]; Public Mystackarray () { elements = new object[default_capacity]; } public void push (E e) { if (size = = elements.length) { Ensurecapa (); } elements[size++] = e; } @SuppressWarnings ("unchecked") public E Pop () { e E = (E) elements[--size]; Elements[size] = null; return e; } private void Ensurecapa () { int newSize = elements.length * 2; elements = arrays.copyof (elements, newSize);} } Package De.vogella.datastructures.stack;import Java.util.arraylist;public Class Mystacklist<e> extends arraylist<e> { private static final long serialversionuid = 1L; Public E pop () { e E = get (Size ()-1); Remove (Size ()-1); return e; } public void push (E e) { add (e);
This was a small JUnit test for the data structure.
package De.vogella.datastructures.stack;import static Org.junit.assert.asserttrue;import Org.junit.before;import Org.junit.test;public class MyStackTest {@Before public void SetUp () throws Exception {} @Test public void Teststackarray () {mystackarray<integer> stack = new Mysta Ckarray<integer> (); Stack.push (1); Stack.push (2); Stack.push (3); Stack.push (3); Stack.push (4); Asserttrue (4 = = Stack.pop ()); Asserttrue (3 = = Stack.pop ()); Asserttrue (3 = = Stack.pop ()); Asserttrue (2 = = Stack.pop ()); Asserttrue (1 = = Stack.pop ()); } @Test public void Teststacklist () {mystacklist<integer> stack = new mystacklist<integer> (); Stack.push (1); Stack.push (2); Stack.push (3); Stack.push (3); Stack.push (4); Asserttrue (4 = = Stack.pop ()); Asserttrue (3 = = Stack.pop ()); Asserttrue (3 = = Stack.pop ()); Asserttrue (2 = = Stack.pop ()); Asserttrue (1 = = Stack.pop ()); }}
How to implement Common Datastructures (List, Stack, Map) in plain java-tutorial