Posts

Showing posts with the label core java

Map interface with implementation classes

Map interface stores elements in the form of key-value pairs in java. If the key is provided then its corresponding value can be obtained. Of course, the keys should have unique values. Map interface implemented classes are, HashMap HashTable LinkedHashMap TreeMap Methods of Map implemented classes: value put(key, value) : This method stores key-value pairs. value get(Object key) : This method returns corresponding value when key is given. If the key does not have a value associated with it, then it returns null. Set<K> keySet() : This method, when applied on a map converts it into a Set where only keys will be stored. Collection<V> values() : This method, when applied on a HashMap objects returns all the values of the HashMap into Collection objects. value remove(Object key) : This method removes the key and corresponding value from the map. void clear() : This method removes all the key-value pairs. boolean isEmpty() : This met...

arrays in java with example

The main definition of an array is, an array represents the group of elements.  In Java, array plays a key role. The main purpose of the array is to hold a fixed set of elements. Why I said fixed set is, while creating array we have to define the size of the array. So, that array can hold only that no of elements in it. For example, when we have created main() method in our program, you can observe there is a String[] array. Otherwise, JVM can't understand and it will not consider that main method as starting point of the application. How to create an array in Java? The below are two different ways of creating an array in java. In the first way, you have to pass the elements between the braces {}.  int [] ar = {}; or int [] ar = new int [ 10 ]; As I have already mentioned, array represents the group of elements. Those elements should be of same type. Otherwise compiler will not allow to add. But, there is a possibility of casting. For example, I hav...