Repository navigation
LinearCollectionPage
Since there is no concept of generics in golang yet, Object(equals to interface{}) is defined to represent the element in the collection. An element must be converted to Object before added to the collection and can be converted to its real type by type assertions after taken out. An object may represent an element of any type, a collection may contain different types of elements (not suggested).
The interfaces of commonly used collection types are defined in collection.go, such as list, map, and so on. Most collections belong to linear collection except Map (shown on the following pages). These interfaces and their implements are as follows:
| Interface | Implementation |
|---|---|
| List | ArrayList, LinkedList |
| Stack | LinkedList |
| Queue | LinkedList, PriorityQueue |
| Deque | LinkedList, ArrayDeque |
| Set | HashSet |
| SortedSet | SortedSet |
| SortedMap | SkipListMap |
Method New, which defined in every collection, is recommended to create a new container:
var l = arraylist.New()After array list is created, we can use Append adds the specified element to the end of this list:
var a collection.Object = Student{1, "Alice"}
var b collection.Object = Student{2, "Bob"}
l.Append(&a)
l.Append(&b)As mentioned in the previous article, you must use type assertion to convert the element to its real type after you get it from the collection. Remember all the elements are represented as Object!
var p = l.Get(1)
var _ = (*p).(Student)ArrayList is not as powerful as LinkedList in some aspects, the latter implements not only List, but also Stack, Queue and Deque. You must choose LinkedList if you want to use certain methods of Stack or Queue.
// init
var l = linkedlist.New()
// add
var a collection.Object = Student{1, "Alice"}
l.Append(&a)
// stack operation
var b collection.Object = Student{2, "Bob"}
l.Push(&b)
var p1 = l.Pop()
var _ = (*p1).(Student)
// queue operation
var p2 = l.First()
var _ = (*p2).(Student)
// deque operation
var p3 = l.RemoveFirst()
var _ = (*p3).(Student)The elements of the priority queue are ordered according to the specific parameter 'precede',which must be pre-defined before creating a new PriorityQueue.
// init
var precede = func(p1 *collection.Object, p2 *collection.Object) bool {
s1 := (*p1).(Student)
s2 := (*p2).(Student)
return s1.Id < s2.Id
}
var q = priorityqueue.New(precede)
// add
var a collection.Object = Student{1, "Alice"}
var b collection.Object = Student{2, "Alice"}
q.Add(&a)
q.Add(&b)
// poll
var p = q.Poll()
var _ = (*p).(Student)ArrayDeque is a resizable-array implementation of the Deque interface based on loop array, which may be faster than LinkedList when used as a queue or stack. It is worth mentioning that the loop array is widely used in saving memory.
// init
var q = arraydeque.New()
// add
var a collection.Object = Student{1, "Alice"}
var b collection.Object = Student{2, "Alice"}
q.AddFirst(&a)
q.AddLast(&b)
// get
var p = q.GetLast()
var _ = (*p).(Student)HashSet is an implementation of the Set interface and backed by a hash table(actually the basic type of map). Obviously, the most commonly used method in this collection is 'add'.
// init
var s = hashset.New()
// add
var a collection.Object = Student{1, "Alice"}
var b collection.Object = Student{1, "Alice"}
s.Add(&a)
s.Add(&b)The SortedSet is backed by SortedMap, and the elements of the sorted set are ordered according to the specific parameter 'precede', both of them must be pre-defined before creating a new SortedSet.
// init
var precede = func(p1 *collection.Object, p2 *collection.Object) bool {
s1 := (*p1).(Teacher)
s2 := (*p2).(Teacher)
return s1.Id < s2.Id
}
var m collection.SortedMap = skiplistmap.New(precede)
var s = sortedset.New(&m)
// add
var t2 collection.Object = Teacher{2, "t2", 0}
var t4 collection.Object = Teacher{4, "t4", 0}
var t6 collection.Object = Teacher{6, "t6", 0}
var t8 collection.Object = Teacher{8, "t8", 0}
s.Add(&t2)
s.Add(&t4)
s.Add(&t6)
s.Add(&t8)In addition to the basic functions of add and remove, this collection also includes navigation methods returning the closest matches for given search targets.
var temp collection.Object = Teacher{5, "t5", 0}
var _ = s.Higher(&temp)
var _ = s.Lower(&temp)
var _ = s.Ceiling(&temp)
var _ = s.Floor(&temp)You may find more examples from the tests in the source code.