"Container" class Listmap with both list and map

Source: Internet
Author: User
Tags comparable delete key int size range sort

The two word "container" is quoted because the class does not implement the Collection interface. To write a class with List function and map function, there are several difficulties, one is that Java does not allow both the list and map two interfaces to be implemented simultaneously, and the second is that the LISTMAP combines the functions of the two and produces a special interface. For example, the contains method of Collection, in Listmap, requires the derivation of the ContainsKey and Containsvalue two methods to determine whether a specified key and value exist in the container.

Here is my implementation (what bugs are welcome to correct):

1.package mycollections;
2.3.import java.util.*; 4.5./** 6. * Container Class 7 with List and MAP functions. 
* * 8 @SuppressWarnings ({"Unchecked"}) 9.public class Listmap<k, v> {10.
Private list<item> values = new arraylist<item> ();    12.13.     /** 14.     * Get the number of elements 15.     * 16.     * @return Element number 17.    * * 18.        public int size () {19.
return Values.size (); 
20.} 21.     22./** 23.     * Determine if the container is empty 24.     * 25.
* @return returns True if null.    26. * * 27.        public Boolean IsEmpty () {28.
return Values.isempty (); 
29.} 30.     31./** 32.     * Get a value iterator 33.     * 34.     * @return Value iterator 35.    * * 36.        Public iterator<v> iterator () {37.
return values (). iterator (); 
38.} 39.     40./** 41.     * Get an array of values 42.     * 43.     * @return Value Array 44.    * * 45.        Public v[] ToArray () {46.
object[] arr = new object[values.size ()];            for (int i = 0; i < values.size (); i++) {48.
Arr[i] = Values.get (i). value;
49.}Return (v[]) arr; 
51.} 52.     53./** 54.     * Check whether the specified key exists 55.     * 56.     * @param key 57.     * 58.
* @return Returns True if it exists.    59. * * 60.        public boolean ContainsKey (K key) {61.
if (key = null) return false;            for (Item item:values) {63.                if (Item.key.equals (key)) {64.
return true;        65.} 66.        } 67.
return false; 
68.} 69.     70./** 71.     * Check whether the specified value exists at 72.     * 73.     * @param value of 74.     * 75.
* @return Returns True if it exists.    76. * * 77.        public boolean Containsvalue (V value) {78.            for (Item item:values) {79.                if (item.value.equals (value)) {80.
return true;        81.} 82.        } 83.
return false; 
84.} 85.     86./** 87.     * Get a value of 88 through the key.     * 89.     * @param key 90.     * 91.     * @return value 92.    * * 93.        Public V get (K key) {94.            for (Item item:values) {95.    if (Item.key.equals (key)) {96.            return item.value;        97.} 98.        } 99.
return null; 
100.} 101.     102./** 103. * Set the value.
Add if the key does not exist.     104. * 105.     * @param key 106.     * @param value of 107.     * 108. * @return the original value.
Returns NULL if the key does not exist.    109. * * 110.    Note here that the key must be unique, so if the key already exists, replace it, or add 111.        Public V-Put (K key, V value) {112.            for (Item item:values) {113.                if (Item.key.equals (key)) {114.
V replaced = Item.value;
Item.value = value;
116. return replaced;        117.} 118. 
} 119.
Item item = new Item (key, value);
121. Values.add (item);
122. return null; 
123.} 124.     125./** 126. * Delete key.
Values are also deleted.     127. * 128.     * @param key 129.     * 130.
* @return Returns True if the key exists.    131. * * 132.        public boolean remove (K key) {133.            for (Item item:values) {134.                if (Item.key.equals (key)) {135. Values.remove (Item);
136. return true;        137.} 138.        } 139.
return false; 
140.} 141.     142./** 143.     * Deletes the key and value 144 at the specified location.     * 145.     * @param index position 146.     * 147.     * @return The value of this location is 148.     * 149.     * @throws Indexoutofboundsexception If the position exceeds the range 150.    * * 151.        Public V Remove (int index) {152.
Return Values.remove (index). value; 
153.} 154.     155./** 156.     * Clears all keys and values in the container 157.    * * 158.        public void Clear () {159.
Values.clear (); 
160.} 161.     162./** 163.     * Gets the value 164 at the specified location.     * 165.     * @param index position 166.     * 167.     * @return Value 168.     * 169.     * @throws Indexoutofboundsexception If the position exceeds the range 170.    * * 171.        Public V get (int index) {172.
Return Values.get (index). value; 
173.} 174.     175./** 176.     * Sets the value of the specified location at 177.     * 178.     * @param index position 179.     * @param value of new values 180.     * 181.     * @return old value of 182.     * 183.     * @throws Indexoutofboundsexception If the position exceeds the range 184.* * 185.        Public V Set (int index, V value) {186.
Item item = values.get (index);
187. V Old_value = Item.value;
188. Item.value = value;
189. return old_value; 
190.} 191.     192./** 193.     * Add a new key and value of 194 at the specified location.     * 195.     * @param index position 196.     * @param key 197.     * @param value of 198.     * 199.     * @throws IllegalStateException If the key already exists 200.    * * 201.        public void Add (int index, K key, V value) {202.            if (ContainsKey (key)) {203.
throw new IllegalStateException ("Key Alreay exists.");        204.} 205.
Item item = new Item (key, value);
206. Values.add (Index, item); 
207.} 208.     209./** 210.     * Find the first position 211 based on the value.     * 212.     * @param value of 213.     * 214.     * The first position of the @return value is 215.    * * 216.        public int Indexofvalue (V value) {217.            for (int i = 0; i < values.size (); i++) {218.
Item item = Values.get (i); 219. if (item.value.equals (value)) {220.
return i;        221.} 222.        } 223.
return-1; 
224.} 225.     226./** 227.     * Find location based on key 228.     * 229.     * @param key 230.     * 231.     * @return Key location 232.    * * 233.        public int Indexofkey (K key) {234.            if (key = = null) {235.
return-1;        236.} 237.            for (int i = 0; i < values.size (); i++) {238.
Item item = Values.get (i);                239. If (Item.key.equals (key)) {240.
return i;        241.} 242.        } 243.
return-1; 
244.} 245.     246./** 247. * Gets a container that contains a subset of the elements.     The number of elements in the container is Toindex-fromindex 248.     * 249.     * @param fromindex subset and start position (inclusive) 250.     * @param toindex subsets and the end position (excluding) 251.     * 252.     * @return contains a subset of elements and container 253.    * * 254.        Public listmap sublist (int fromindex, int toindex) {255.
listmap<k, v> map = new listmap<k, v> ();
256. MAP.VALUES.ADDALL (Values.sublist (Fromindex, Toindex)); 257. Return map; 
258.} 259.     260./** 261.     * Gets the value List object 262.     * @return List Object 263 containing the value.    * * 264.        Public list<v> values () {265.
list<v> list = new arraylist<v> ();            266. for (Item item:values) {267.
List.add (Item.value);        268.} 269.
return list; 
270.} 271.     272./** 273.     * Gets the List object 274 containing the key.     * @return List Object 275 containing the key.    * * 276.        Public list<k> keys () {277.
list<k> list = new arraylist<k> ();            278. for (Item item:values) {279.
List.add (Item.key);        280.} 281.
return list; 
282.} 283.     284./** 285.     * The key to the small to large sort 286.    * * 287.        public void SortKey () {288.            Collections.sort (values, new comparator<item> () {289.                public int Compare (item i1, item i2) {290.
Comparable C1 = (comparable) I1.key;
291. Comparable C2 = (comparable) I2.key; 292. if (C1 = = NULL && c2 = = null) return 0;
293. if (c1 = null) return-1;
294. if (C2 = null) return 1;
295. Return C1.compareto (C2);        296.} 297.
}); 
298.} 299.     300./** 301.     * To the value of the small to large sort 302.    * * 303.        public void Sortvalue () {304.            Collections.sort (values, new comparator<item> () {305.                public int Compare (item i1, item i2) {306.
Comparable C1 = (comparable) I1.value;
307. Comparable C2 = (comparable) I2.value;
308. if (C1 = = NULL && C2 = null) return 0;
309. if (C1 = null) return-1;
310. if (C2 = null) return 1;
311. Return C1.compareto (C2);        312.} 313.
}); 
314.} 315.     316./** 317.     * The container element is inverted 318.    * * 319.        public void reverse () {320.
Collections.reverse (values); 
321.} 322.     323./** 324.     * Internal class 325.    * * 326. 
Private class Item {327.   328.     Public K key;        329.330.
public V value;        331.332.            Public Item (K key, V value) {333.                if (key = = null) {334.
throw new NullPointerException ("key cannot be null.");            335.} 336.
This.key = key;
337. This.value = value;    338.} 339. } 340.}

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.