Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Implement AttributeList API

  • Last Updated : 07 Jan, 2021

An AttributeList can be used to get and set multiple MBean attributes in a single invocation. It is an ArrayList that can only contain Attributes. AttributeLists must be externally synchronized.

Declaration:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

public class AttributeList extends ArrayList
{
  // code
}

AttributeList API’s implementing classes: 

  • Serializable
  • Cloneable
  • Iterable
  • Collection
  • List
  • RandomAccess

Constructor:

  1. AttributeList() – Constructs a new empty attribute list.
  2. AttributeList(AttributeList list) – Constructs a new attribute from another attribute list.
  3. AttributeList(int initialCapacity) – Constructs a new empty attribute list with specified initial capacity.

Methods:

  1. add(Attribute object) – Method to append an Attribute to the list.
  2. void add(int index, Attribute object) – Method to insert a new Attribute into the list at the specified index.
  3. boolean addAll(AttributeList list) – Method to append attributes to the list.
  4. boolean addAll(int index, AttributeList list) – Method to insert all the attributes in the passed list at the specified index.
  5. void set(int index, Attribute object) –Method to change an attribute at a specified index.

Java




// Java program to implement AttributeList API
  
import java.util.Collection;
import java.util.List;
import javax.management.Attribute;
import javax.management.AttributeList;
  
public class AttributeListImpl {
    private AttributeList attributeList;
  
    // constructor for creating an empty AttributeList
    public AttributeListImpl()
    {
        attributeList = new AttributeList();
    }
  
    // constructor for creating an AttributeList containing
    // the elements of the AttributeList specified, in the
    // order in which they are returned by the
    // AttributeList's iterator.
  
    public AttributeListImpl(AttributeList list)
    {
        attributeList = new AttributeList();
    }
  
    // constructor for creating an AttributeList with the
    // specified initial capacity
    public AttributeListImpl(int initialCapacity)
    {
        attributeList = new AttributeList(initialCapacity);
    }
  
    // constructor for creating an AttributeList
    // containing the elements of the List
    // specified.
  
    public AttributeListImpl(List<Attribute> list)
    {
        attributeList = new AttributeList();
    }
  
    // this method appends an Attribute to the list
    public void add(Attribute object)
    {
        attributeList.add(object);
    }
  
    // this method inserts an attribute at a specified
    // position
    public void add(int index, Attribute object)
    {
        attributeList.add(index, object);
    }
  
    // this method inserts an specified element at
    // a specified position
    public void add(int index, Object element)
    {
        attributeList.add(index, element);
    }
  
    // this method appends a specified element to the list
    public boolean add(Object element)
    {
        return attributeList.add(element);
    }
  
    // this method appends all the elements of a
    // given list in the AttributeList.
    public boolean addAll(AttributeList list)
    {
        return attributeList.addAll(list);
    }
  
    // this method will appends all of the
    // elements of the given Collection
    // in the AttributeList.
    public boolean addAll(Collection<?> c)
    {
        return attributeList.addAll(c);
    }
  
    // Inserts all of the elements in the AttributeList
    // specified into this list, starting at the specified
    // position, in the order in which they are returned by
    // the Iterator of the AttributeList specified.
    public boolean addAll(int index, AttributeList list)
    {
        return attributeList.addAll(index, list);
    }
  
    // this method will add all of the elements of the
    // given collection at a the specified index
    public boolean addAll(int index, Collection<?> c)
    {
        return attributeList.addAll(index, c);
    }
  
    // this method Return a view of this list as a
    // List<Attribute>
    public List<Attribute> asList()
    {
        return attributeList.asList();
    }
  
    // this method will set the element at a given position
    public void set(int index, Attribute element)
    {
        attributeList.set(index, element);
    }
  
    // this method will replace the element at the specified
    // position in this list with the given element
    public Object set(int index, Object element)
    {
        return attributeList.set(index, element);
    }
  
    public static void main(String[] arg)
    {
        // creating object for AttributeListImpl object
        AttributeListImpl attributeList
            = new AttributeListImpl();
  
        attributeList.add(new Attribute("rose", 1));
        attributeList.add(new Attribute("sun-flower", 2));
        attributeList.add(new Attribute("tulip", 3));
        attributeList.add(new Attribute("orchid", 4));
        attributeList.add(new Attribute("marie-gold", 5));
        attributeList.add(new Attribute("hibiscus", 0));
  
        System.out.println(
            "The elements of the attributelist are");
  
        List<Attribute> flowerlist = attributeList.asList();
  
        int ind = 0;
  
        // iterating through the attribute List and
        // printing elements.
        while (ind < flowerlist.size()) {
            System.out.print(flowerlist.get(ind++) + " ");
        }
  
        System.out.println();
  
        // setting value = 6 at key = "hibiscus"
        attributeList.set(5, new Attribute("hibiscus", 6));
  
        System.out.println();
        System.out.println("after setting index 5");
        System.out.println(
            "the elements of the attributelist are");
  
        // iterating through the attribute List and
        // printing elements.
        flowerlist = attributeList.asList();
  
        ind = 0;
        while (ind < flowerlist.size()) {
            System.out.print(flowerlist.get(ind++) + " ");
        }
    }
}
Output
The elements of the attributelist are
rose = 1 sun-flower = 2 tulip = 3 orchid = 4 marie-gold = 5 hibiscus = 0 

after setting index 5
the elements of the attributelist are
rose = 1 sun-flower = 2 tulip = 3 orchid = 4 marie-gold = 5 hibiscus = 6



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!