Skip to content

piotrkrzyminski/collections

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 

Repository files navigation

collections

This is simple java collections library with most used structures. In the future i will add new collections.

Lists

ArrayList

This structure uses simple array to store generic elements. You can create list with specified size of use default. When array will be filled add another element causes increasing array size by 2. This operation takes quite time because it copying all elements from smaller array to bigger. Howewer access to each node is very fast.

Available methods:

  1. Three types of constructor: non-parameter with default list size, with integer parameter to specify default size and copying constructor
  2. void add(E) - adding new elements to array. If array is filled creates new bigger array to store new values.
  3. int size() - returns number of elements in array.
  4. boolean isEmpty() - returns true if array has no elements.
  5. void clear() - deletes all elements in array and sets its size to default.
  6. E get(int) - returns element from array at index passed in parameter.
  7. E remove(int) - the same as get() but also remove element from array.
  8. boolean contains(E) - returns true if element passed in parameter is in structure
  9. int indexOf(E) - returns index of element in list or -1 if not exists
  10. void replace(E, int) - changes element at specified index
  11. void trimToSize() - changes size of list to fill all elements and avoid unnecesary memory alocation
  12. Object[] toArray() - returns array with elements copied from list
  13. Iterator[] iterator() - returns Iterator object that allows to move safely on ArrayList elements

ArrayList Iterator

There are two ways to move across ArrayList elements. Using indexes or iterator object. That object has only two methods:
  1. boolean hasNext() - returns true if array has next element after current element
  2. E next() - returns element reference after currently used

Linked list

Doubly linked list stores elements in nodes. Besides elements, each node contains also information about next and previous node. This sort of collection don't have size limit. But access to each node may take more time than in ArrayList implementation.
Available methods:
  1. void add(E) - create new node with element and put it at the end of list
  2. int size() - returns number of nodes.
  3. boolean isEmpty() - returns true if first node is null.
  4. void clear() - change first node to null.
  5. E get(int) - returns element from list at index passed in parameter.
  6. E remove(int) - the same as get() but also remove node from list.
  7. boolean contains(E) - returns true if element passed in parameter is in structure
  8. int indexOf(E) - returns index of element in list or -1 if not exists
  9. void replace(E, int) - changes element at specified index
  10. void toArray() - returns array with elements copied from list
  11. Iterator[] iterator() - returns Iterator object that allows to move safely on LinkedList elements

Singly linked list Iterator

Iterator allows to move safely across nodes. It has only two methods:
  1. boolean hasNext() - returns true if array has next element after current element
  2. E next() - returns element reference after currently used

Queue ArrayList and LinkedList implementation

Queue uses almost every method from standard list class. But except taking element from end of the list it takes it from beginning.

Available methods:

  • void add(E) - adding new elements at the end of structure
  • int size() - returns number of elements in structure.
  • boolean isEmpty() - returns true if structure has no elements.
  • void clear() - deletes all elements in structure.
  • E get() - returns element from structure at 0 index
  • E remove() - the same as get() but also remove element from structure.
  • Stack ArrayList and LinkedList implementation

    Stack add element to the end of structure but in contrast to queue, it takes elements from end.

    Available methods:

  • boolean push(E) - adding new elements at the end of structure
  • int size() - returns number of elements in structure.
  • boolean isEmpty() - returns true if structure has no elements.
  • void clear() - deletes all elements in structure.
  • E peek() - returns element from structure at 0 index
  • E pop() - the same as peek() but also remove element from structure.
  • Trees

    Binary tree

    Binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child

    Available methods:

    1. void insert(E)
    2. E getRoot()
    3. int countNodes()
    4. void printAscending()
    5. void printDescending()
    6. boolean contains(E)
    7. boolean isEmpty()
    8. E maximum()
    9. E minimun()
    10. void clear()

    Tree Iterator

    Iterator allows to move safely across nodes. It has only two methods:
    1. hasNext() - returns true if array has next element after current element
    2. next() - returns element reference after currently used

    Every method was tested using jUnit library