Java Program to Implement Attribute API Last Updated : 08 Jul, 2021 Comments Improve Suggest changes Like Article Like Report Valid attribute names are case-insensitive, and they are restricted to the ASCII characters in the set of [0-9,a-z, A-Z_-], and cannot be exceeded over 70 characters in length. The attribute's value can contain any characters and will be encoded in UTF-8 when written to the output stream in any program. This is the source code of the Java program to Implement Attributes API. The program is successfully run and the output of the program is also shown below: Procedure: Setting attributesCreating an object of class in main() method and name it as 'attribute'.Assigning attributes.Stores the value Value associated with key name in this Attribute using putValue() method.Returns a Set containing all the keys found in this Attributes using keySet() method.Iterator to traverse over collection interface of the object type.Checking condition using hasNext() method which holds true till there is a single element left in order to print the elements.Clearing all attribute objects from the collection using clear() method.Lastly, printing the display message whether the attribute is empty or not. Implementation: Example Java // Java Program to Implement Attribute API // Importing classes from // java.util package import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.jar.Attributes; // Class public class GFG { private Attributes attributes; // Constructor 1 // Constructs a new, empty Attributes object // with default size public GFG() { attributes = new Attributes(); } // Constructor 2 // Constructs a new Attributes object // with the same attribute name-value mappings // as in the specified Attributes. public GFG(Attributes attr) { attributes = new Attributes(attr); } // Constructor 3 // Constructs a new, empty Attributes object // with the specified initial size. public GFG(int size) { attributes = new Attributes(size); } // Method 1 - clear() public void clear() { // Removes all attributes from this Map attributes.clear(); } // Method 2 - clone() public Object clone() { // Returns a copy of the Attributes // Returns true if this Map contains // the specified attribute name (key) return attributes.clone(); } // Method 3 - containsKey() public boolean containsKey(Object key) { // Returns true if this Map maps one or more // attribute names to the specified value. return attributes.containsKey(key); } // Method 4 - containsValue() public boolean containsValue(Object value) { // Returns a Collection view of the attribute // name-value mappings contained in this Map return attributes.containsValue(value); } // Method 5 - entrySet() public Set<Map.Entry<Object, Object> > entrySet() { // Returns the value of the specified attribute name // or NULL if the attribute name is not present return attributes.entrySet(); } // Method 6 - get() public Object get(Object key) { // Returns the value of the specified // Attributes.Name, or null if the attribute is not // present return attributes.get(key); } // Method 7 - getValue() public String getValue(Attributes.Name name) { // Returns the value of the specified attribute // name, specified as a string, or null if the // attribute was not found. return attributes.getValue(name); } // Method 8 - getValue() public String getValue(String name) { // Returns true if this Map contains no attributes return attributes.getValue(name); } // Method 9 - isEmpty() public boolean isEmpty() { // Returns a Set view of the attribute names (keys) // contained in this Map return attributes.isEmpty(); } // Method 10 - keySet() public Set<Object> keySet() { // Associates the specified value with the // specified attribute name (key) in this Map return attributes.keySet(); } // Method 11 - put() public Object put(Object key, Object value) { // Copies all of the attribute name-value mappings // from the specified attributes to this Map return attributes.put(key, value); } // Method 12 - putAll() public void putAll(Map<?, ?> m) { // Associates the specified value with // the specified attribute name, specified as a // String attributes.putAll(m); } // Method 13 - putValue() public String putValue(String name, String value) { // Removes the attribute with the specified // name(key) from this Map return attributes.putValue(name, value); } // Method 14 - remove() public Object remove(Object key) { // Returns the number of attributes in this Map return attributes.remove(key); } // Method 15 - size() public int size() { return attributes.size(); } // Method 16 - values() public Collection<Object> values() { // Returns a Collection view of the attribute values // contained in this Map return attributes.values(); } // Method 17 - main() driver method public static void main(String[] args) { // Setting attributes Attributes.Name CLASS_PATH = new Attributes.Name("CLASS_PATH"); Attributes.Name CONTENT_TYPE = new Attributes.Name("CONTENT_TYPE"); Attributes.Name MANIFEST_VERSION = new Attributes.Name("MANIFEST_VERSION"); // Creating an object of class in main() method GFG attribute = new GFG(); // Assigning attributes // Custom values attribute.put(CLASS_PATH, "root/sub_dir/class_path"); attribute.put(CONTENT_TYPE, "UTF-8"); attribute.put(MANIFEST_VERSION, "2"); // Stores the value Value associated with key name // in this Attribute attribute.putValue("MAIN_CLASS", "TESTMAIN.java"); // Display message System.out.println( "the key set of the Attributes is "); // Returns a Set containing all the keys found in // this Attributes Set<Object> keySet = attribute.keySet(); // Iterator to traverse over collection interface Iterator<Object> itr = keySet.iterator(); // Condition check using hasNext() method which // holds true till there is an element remaining while (itr.hasNext()) { System.out.print(itr.next() + "\t"); } // New Line System.out.println(); // Display message System.out.println( "the values of the Attributes is "); // Collection<Object> collectionValues = attribute.values(); itr = collectionValues.iterator(); // Condition check using hasNext() method which // holds true till there is an element remaining while (itr.hasNext()) { // Print the elements System.out.print(itr.next() + "\t"); } // New line System.out.println(); // Display message System.out.println( "the entry set of the Attributes is "); Iterator<Entry<Object, Object> > eitr; Set<Entry<Object, Object> > entrySet = attribute.entrySet(); eitr = entrySet.iterator(); // Condition check using hasNext() method which // holds true till there is an element remaining while (eitr.hasNext()) { // Print all elements System.out.println(eitr.next() + "\t"); } // Print and display messages System.out.println( "the Attributes contains Key CLASS_PATH:" + attribute.containsKey(CLASS_PATH)); System.out.println( "the Attributes contains Value TESTMAIN.java :" + attribute.containsValue("TESTMAIN.java")); System.out.println("the size of the Attributes is " + attribute.size()); // Clearing all attribute objects from the // collection attribute.clear(); // Determines whether this attribute contains any // keys if (attribute.isEmpty()) // Print the display message System.out.println("the Attributes is empty"); else // Print the display message System.out.println( "the Attributes is not empty"); } } Outputthe key set of the Attributes is CLASS_PATH CONTENT_TYPE MANIFEST_VERSION MAIN_CLASS the values of the Attributes is root/sub_dir/class_path UTF-8 2 TESTMAIN.java the entry set of the Attributes is CLASS_PATH=root/sub_dir/class_path CONTENT_TYPE=UTF-8 MANIFEST_VERSION=2 MAIN_CLASS=TESTMAIN.java the Attributes contains Key CLASS_PATH:true the Attributes contains Value TESTMAIN.java :true the size of the Attributes is 4 the Attributes is empty Comment More infoAdvertise with us Next Article Java Program to Implement Attribute API A abhijat_sarari Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads How to Create a File with a Specific File Attribute in Java? In Java, you can create a file with specific File attributes such as read-only, hidden, or system attributes. This allows you to control the behavior and visibility of the file in the File system. In this article, we'll explore how to create a file with specific attributes in Java. In this article, 2 min read Java Program to Access All Data as Object Array Java is an object-oriented programming language. Most of the work is done with the help of objects. We know that an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types. Java allows us to store objects in an array. In Java, the class i 4 min read Getter and Setter in Java In Java, Getter and Setter are methods used to protect your data and make your code more secure. Getter and Setter make the programmer convenient in setting and getting the value for a particular data type. Getter in Java: Getter returns the value (accessors), it returns the value of data type int, 3 min read How to Read and Write XML Files in Java? XML is defined as the Extensible Markup Language, and it is mostly used as a format for storing and exchanging data between systems. To read and write XML files, Java programming offers several easily implementable libraries. The most widely used library is the built-in JAXP (Java API for XML proces 5 min read Java Object Oriented Programming - Exercises Looking for Java OOP exercises to test and improve your object-oriented programming skills? Explore our topic-wise Java OOP practice exercises, featuring over 25 practice problems designed to help you master key OOP concepts such as encapsulation, inheritance, polymorphism, and abstraction. Java is 15+ min read Four Main Object Oriented Programming Concepts of Java Object-oriented programming generally referred to as OOPS is the backbone of java as java is not a purely object oriented language but it is object oriented language. Java organizes a program around the various objects and well-defined interfaces. There are four pillars been here in OOPS which are l 7 min read DSA in JAVA Data Structures and Algorithms (DSA) are critical for optimizing how data is stored, accessed, and processed, directly affecting the performance of an application. This tutorial will guide you through the essential data structures and algorithms using the Java programming language.Why Learn DSA in J 14 min read Jackson Annotations For Java Application Java has immense application in the coding world, and almost all of us know this fact. One of its features is the Jackson annotations. Although that is a very familiar term with the people involved in the coding world, it is less known. Jackson is a popular and very efficient JAVA library used to ma 8 min read Java Crash Course In today's tech-driven world, coding is a valuable skill, and Java is an excellent starting point. Its versatility and ease of understanding make it ideal for a wide range of applications, from entertaining apps to crucial business tools. If you are new to coding or looking to expand your knowledge, 11 min read Introduction to Java Agent Programming Java agents are a part of the Instrumentation API in Java, which allows developers to modify the behavior of a running application by altering its bytecode. This process, known as instrumentation, does not require changes to the source code. Java agents are a powerful feature that can be used for pe 7 min read Like