Author Topic: [Source] Bag Data Structure - Use of Java Generics  (Read 1325 times)

0 Members and 1 Guest are viewing this topic.

Offline Psycho_Coder

  • Knight
  • **
  • Posts: 166
  • Cookies: 84
  • Programmer, Forensic Analyst
    • View Profile
    • Code Hackers Blog
[Source] Bag Data Structure - Use of Java Generics
« on: August 29, 2014, 03:09:58 pm »
Bag Data Structure

A Bag is a collection where removing items is not supported. It purpose is to provide users with the ability to collect items and then to iterate through the collected items. At the end you have an option to empty the complete bag.

In this implementation you have an additional method to increase the Bag storage.


A Test code is given below in order to understand how to use this little library :-


BagTest.java

Code: (java) [Select]
package net.thinkfuzzy.test;


import java.util.Iterator;

import net.thinkfuzzy.Bag.Bag;

/**
 *
 * @author psychocoder
 */
public class BagTest {

    public static void main(String[] args) {

        int initSize = 5;

        /**
         * Create an instance of a class Bag and initialize the constructor with
         * an initial value.
         */
Bag<Integer> bag = new Bag<>(initSize);
        System.out.println("Initial Size of Bag (No of elements): " + bag.getSize());

        /**
         * Get the capacity of the Bag
         */
        System.out.println("Capacity (No. of items the bag can hold) :" + bag.capacity());

        /**
         * Insert some values.
         */
        bag.add(12);
        bag.add(212);
        bag.add(1);
        bag.add(123);

        /**
         * Get the Size
         */
        System.out.println("Size of Bag after inserting items : " + bag.getSize());

        /**
         * Extend the size of the Bag
         */
        bag.extendSize(initSize * 2);

        /**
         * Get the capacity of the Bag after extending the size
         */
        System.out.println("Capacity of Bag after extending the size :" + bag.capacity());

        /**
         * Insert some more values.
         */
        bag.add(142);
        bag.add(12);
        bag.add(19);
        bag.add(131);

        /**
         * Get the Size
         */
        System.out.println("Size of Bag after inserting more items : " + bag.getSize());

        System.out.println("Elements present in the Bag :- \n");
        int count = 0;
        for (Iterator<Integer> itr = bag.iterator(); itr.hasNext(); count += 1) {
            System.out.println(itr.next());
        }

        System.out.println("No. of elements in the Bag (When used counter) :" + count);

        /**
         * Clear all the elements from the bag
         */
        bag.clearAll();

        /**
         * Get the Size of the bag after clearing all the items.
         */
        System.out.println("Size of Bag after clearing : " + bag.getSize());

    }

}


Output

Code: [Select]
Initial Size of Bag (No of elements): 0
Capacity (No. of items the bag can hold) :5
Size of Bag after inserting items : 4
Capacity of Bag after extending the size :10
Size of Bag after inserting more items : 8
Elements present in the Bag :-

12
212
1
123
142
12
19
131
No. of elements in the Bag (When used counter) :8
Size of Bag after clearing : 0

Download the eclipse Project
The Source includes the JavaDocs and binaries as well.
"Don't do anything by half. If you love someone, love them with all your soul. When you hate someone, hate them until it hurts."--- Henry Rollins