Set

Java API provides a Set interface BasicSet for storing a series of elements of the same type.

Creating a Set

Elements of a set are called keys. The constructor for BasicSet is as follows:

public BasicSet(DATA_TYPE keyType, int capacity)

Parameters

  • keyType: An enum declaring the type of the set.
  • capacity (optional, default 0): An integer specifying the initial capacity of the set. The set will automatically double its capacity when the number of elements reaches 0.75*capacity.

Usage

  1. You can add objects to a set using the add method. For example:

    BasicSet basicSet = new BasicSet(Entity.DATA_TYPE.DT_INT,4);
    basicSet.add(new BasicInt(1));
  2. You can get all keys from a set using the keys method:

    public Vector keys();

    This method returns a vector (from com.xxdb.data package). You can get all the elements of the set using this vector:

    BasicSet basicSet = new BasicSet(Entity.DATA_TYPE.DT_INT);
    basicSet.add(new BasicInt(1));
    basicSet.add(new BasicInt(2));
    basicSet.add(new BasicInt(3));
    basicSet.add(new BasicInt(4));
    basicSet.add(new BasicInt(5));
    Vector vector = basicSet.keys();
    System.out.println(vector.getString());
  3. You can check if a key exists in the set using the contains method, which returns a boolean value. For example:

    BasicSet basicSet = new BasicSet(Entity.DATA_TYPE.DT_INT,4);
    basicSet.add(new BasicInt(1));
    basicSet.add(new BasicInt(2));
    basicSet.add(new BasicInt(3));
    basicSet.add(new BasicInt(4));
    basicSet.add(new BasicInt(5));
    System.out.println(basicSet.contains(new BasicInt(4)));