Common Interfaces in Java

0
238
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

The vector,Hashtable and Hashmap classes all have an iterator method which returns an iterator of their contents.

The Iterator interface has the following methods:

hasNext();//Returns true if the collection contains more than one element
next();//returns the next element from the collection.
remove();//removes the current element from the collection.

next() method returns the next element if it is available in the collection else it will throw the NoSuchElementException.
remove() method throws the IllegalStateException when there is no element in the collection.
Using the iterator interface we can insert elements only at the end of the list.If we need to insert element at any required location of the list,we need to use ListIterator Interface.(It extends Iterator interface).
The ListIterator has a special method called add(int index,Object obj). This nethod add an object at the specified location or index.

An example:

Vector v=new vector();
v.add("Apple");
v.add("Banana");
v.add("Pear");
v.add("Grape");
Iterator itr=v.iterator();
while(itr.hasMore()){
String s=(String)itr.next();
System.out.println(s);
} 

All About SortedMap Interface in Collection in Java

The SortedMap interface extends the Map interface. The SortedMap interface contains elements in ascending order based on keys. The functionality of SortedMap is analogous to SortedSet. This is implemented in the TreeMap class.

The method available in the SortedMap interface are:

comparator()//returns the comparator of the SortedMap.It returns null if the sorted map uses
//natural ordering for their key
firstKey();//Returns the first key of the SortedMap.
headMap(Object end);//Returns the keys of the SortedMap that are less than the specified end key.
lastKey();//returns the last key of the SortedMap.
subMap(Object start,Object end);// returns the keys of the SortedMap that are greater than or equal
//to the specified start object and less than or equal to the specified end object.
tailMap(Object start);//Returns the keys of the SortedMap that are greater than or equal 
//to the start object. 

All About Map Interface in Collection in Java

The Map interface maps unique key elements to their corresponding values.Map interface allows us to view the elements of a collection as set of keys,collection of values and the mapping of key value pair.If we remove a key from the Map, the corresponding value also will be removed.
 

The method defined in Map interface is given as:


clear();//removes all the mapping from a map.
containsKey(Object key);// returns true if the specified value matches 
//with one or more keys in the Map interface.
containsValue(Object value);// returns true if the specified value 
//matches with one or more values in the Map interface.
entrySet();//returns the key value pair contained in the Map
equals(Object obj);//returns true if the specified object maps with an 
//object of a Map interface.
get(Object key);//returns the value which is mapped to the specified key.
isEmpty();//returns true if a Map contains no key value mapping.
keySet();//returns the keys in a Map.
put(Object key,Object value);Maps the specified key with the value.
putAll();//copies all the specified key value pairs from a specified Map
// to the Map with which we are currently working
remove(Object key);Removes the specified key from the Map, automatically 
//the value will be removed.
size();//returns the number of key value mapping available in a Map.
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here