Java Interfaces



Introduction

The Java Interfaces contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).

Interface Summary

Sr.No. Intreface & Description
1

Collection<E>

This is the root interface in the collection hierarchy.

2

Comparator<T>

This is a comparison function, which imposes a total ordering on some collection of objects.

3

Deque<E>

This is a linear collection that supports element insertion and removal at both ends.

4

Enumeration<E>

This is an object that implements the Enumeration interface generates a series of elements, one at a time.

5

EventListener

This is a tagging interface that all event listener interfaces must extend.

6

Formattable

This is the Formattable interface must be implemented by any class that needs to perform custom formatting using the 's' conversion specifier of Formatter.

7

Iterator<E>

This is an iterator over a collection.

8

List<E>

This is an ordered collection (also known as a sequence).

9

ListIterator<E>

This is an iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.

10

Map<K,V>

This is an object that maps keys to values.

11

Map.Entry<K,V>

This is a map entry (key-value pair).

12

NavigableMap<K,V>

This is a SortedMap extended with navigation methods returning the closest matches for given search targets.

13

NavigableSet<E>

This is a SortedSet extended with navigation methods reporting closest matches for given search targets.

14

Observer

This is a class can implement the Observer interface when it wants to be informed of changes in observable objects.

15

Queue<E>

This is a collection designed for holding elements prior to processing.

16

RandomAccess()

This is the Marker interface used by List implementations to indicate that they support fast (generally constant time) random access.

17

Set<E>

This is a collection that contains no duplicate elements.

18

SortedMap<K,V>

This is a Map that further provides a total ordering on its keys.

19

SortedSet<E>

This is a Set that further provides a total ordering on its elements.

Adding an Element to a HashSet of Integers Example

The following example shows the usage of Java HashSet add() method to add entries to the HashSet. We've created a HashSet object of Integer. Then few entries are added using add() method and then set is printed.

package com.tutorialspoint;

import java.util.HashSet;
import java.util.Set;

public class SetDemo {
   public static void main(String args[]) {
      
      // create hash set
      Set<Integer>newSet = new HashSet <>();

      // populate hash set
      newSet.add(1); 
      newSet.add(2);
      newSet.add(3);  

      // checking elements in hash set
      System.out.println("Hash set values: "+ newSet);
   }    
}

Output

Let us compile and run the above program, this will produce the following result.

Hash set values: [1, 2, 3]
Advertisements