Original works, reproduced please specify the source: http://blog.csdn.net/qiujuer/article/details/40298761
Sometimes I have this requirement: I need to save the history or store some information in the operation of a program, but I don't want this data to be infinitely increased in memory. Simply put, the use of arrays or linked lists but I do not want their unlimited growth, I just need to save the last 20 or 30 records on OK, more than I do not.
In terms of the above requirements, we may be able to directly through a certain method of the array or linked list of certain operations (full delete the first) can achieve the corresponding demand, of course, can also be two times encapsulation.
My initial implementation is as follows:
First create an interface:
Import Java.util.deque;import java.util.list;/** * Created by Qiuju * on 2014/9/23. */public interface fixedlist<t> extends List<t>, Deque<t>, cloneable, java.io.Serializable { /** * Add a new * to the last * If the length exceeds the maximum allowable value, then a /t addelement (t addlast) is ejected; /** * Get the maximum threshold * * @return threshold * /int getmaxsize (); /** * Set maximum storage range * * @return because the queue size is changed, resulting in the ejection of header data * /list<t> setmaxsize (int maxSize); /** * Convert to list data * @return List * /list<t> toList ();}
This interface integrates lists, queues, and classes used for instantiation
Then I implemented this interface:
Import Java.util.arraylist;import Java.util.linkedlist;import Java.util.list;public class fixedlinkedlist<t> Extends Linkedlist<t> implements fixedlist<t> {private transient int maxSize = Integer.max_value; Public Fixedlinkedlist () {super (); } public fixedlinkedlist (int maxSize) {super (); This.maxsize = maxSize; } @Override public T addelement (t addlast) {t head = null; while (size () >= maxSize) {head = poll (); } addlast (AddLast); return head; } @Override public list<t> setmaxsize (int maxSize) {list<t> List = null; if (MaxSize < this.maxsize) {list = new arraylist<t> (); while (size () > MaxSize) {list.add (poll ()); }} this.maxsize = MaxSize; return list; } @Override Public list<t> toList () {list<t> List = new Arraylist<t> (); List.addall (this); return list; } @Override public int getmaxsize () {return this.maxsize; }}
The implementation of the interface is relatively simple, nothing more than to add the decision to delete, change the size of the unwanted pop-up OK.
The method of use is also simple:
fixedlist<string> fixed = new fixedlinkedlist<string> (10);//add Element Fixed.addelement ("1"); fixed.addelement ("2"); Fixed.addelement ("3"); Fixed.setmaxsize (5);//convert to listlist<string> fixedlist = Fixed.tolist ();
The implementation of the function is realized, but for me the perfect plot of the people always have a lump in my heart, because its add element can only use the AddElement () method, cannot use the default Add () method, the default method does not know the deletion of redundant data, of course, this is the implementation of the short board. To solve this problem, I looked at Java's implementation of the List, looked at its implementation of ArrayList and LinkedList, and found it inappropriate to use ArrayList for the above requirements because it would need to delete elements frequently, if using an Array The List will be related to duplicate copy issues. So I'm going to rewrite the LinkedList class.
Rewriting of course is a long process, but really thank the foreign Daniel for the contribution of LinkedList, standing on the shoulders of giants is good, rewrite the operation for the following:
===================fixedlist===================//fixed-length queue//can be specified in length, using the same method as the normal queue//when the number of elements to be added to the specified number of elements will pop up//head insertion trailer, Tail Insert Header popup//initialization maximum length is 5fixedlist<integer> list = new fixedlist<integer> (5);//add Element List.add (1);// The insertion element at the end is the same as add list.addlast (1);//Insert from the head, the default is to delete the tail beyond the element List.addfirst (19);//Add a list list.addall (new Arraylist<integer > ());//Get the maximum capacity list.getmaxsize ();//Adjust the maximum length, reduce the length of the extra element will be automatically removed list.setmaxsize (3);//Use Poll mode pop-up element int i = List.poll (); /Remove is similar to poll, but does not return delete element, delete only one element list.remove ();//Empty Operation List.clear ();//can use list operation list<integer> List1 = new Fixedlist<integer> (2); List1.add (1); List1.clear ();
It can be seen that it can be operated entirely using the LinkedList method, except that it joins the method of obtaining maximum capacity and setting maximum capacity.
This heart more comfortable, the source more I give a link: source
If you feel good, take a look at my open source project:
Genius-android
Implementation of the [Java] fixed queue