The ArrayList
class is in the java.util
package (other collections can be found here too). A package is a set or library of related classes. The java.lang package is the main Java language classes that you get automatically without importing it. The java.util package has a lot of utility classes that you can use if you import the package. If you want to use any class other than those in java.lang
you will need to either use the full name (packageName.ClassName) like (java.util.ArrayList
) or use one or more import statements to import in that package.
Import statements have to be the first code in a Java source file. An import statement tells Java which class you mean when you use a short name (like ArrayList
). It tells Java where to find the definition of that class.
You can import just the classes you need from a package as shown below. Just provide an import
statement for each class that you want to use.
import java.util.ArrayList; // import just the ArrayList class
Another option is to import everything at the same level in a package using import packageName.*
.
import java.util.*; // import everything in package including ArrayList
To declare a ArrayList use ArrayList<Type> name
Change the Type to be whatever type of objects you want to store in the ArrayList, for example String
as shown in the code below. You don’t have to specify the generic type <Type>
, since it will default to Object
, but it is good practice to specify it to restrict what to allow in your ArrayList. Using a type ArrayList<Type> is preferred over just using ArrayList because it allows the compiler to find errors that would otherwise be missed until run-time.
// ArrayList<Type> name = new ArrayList<Type>(); // An ArrayList of Strings: ArrayList<String> shoppingList = new ArrayList<String>();
Note:
1. add( Object o): This method adds an object o to the arraylist.
obj.add("hello");
This statement would add a string hello in the arraylist at last position.
2. add(int index, Object o): It adds the object o to the array list at the given index.
obj.add(2, "bye");
It will add the string bye to the 2nd index (3rd position as the array list starts with index 0) of array list.
3. remove(Object o): Removes the object o from the ArrayList.
obj.remove("Chaitanya");
This statement will remove the string “Chaitanya” from the ArrayList.
4. remove(int index): Removes element from a given index.
obj.remove(3);
It would remove the element of index 3 (4th element of the list – List starts with o).
5. set(int index, Object o): Used for updating an element. It replaces the element present at the specified index with the object o.
obj.set(2, "Tom");
It would replace the 3rd element (index =2 is 3rd element) with the value Tom.
6. int indexOf(Object o): Gives the index of the object o. If the element is not found in the list then this method returns the value -1.
int pos = obj.indexOf("Tom");
This would give the index (position) of the string Tom in the list.
7. Object get(int index): It returns the object of list which is present at the specified index.
String str= obj.get(2);
Function get would return the string stored at 3rd position (index 2) and would be assigned to the string “str”. We have stored the returned value in string variable because in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer variable.
8. int size(): It gives the size of the ArrayList – Number of elements of the list.
int numberofitems = obj.size();
9. boolean contains(Object o): It checks whether the given object o is present in the array list if its there then it returns true else it returns false.
obj.contains("Steve");
It would return true if the string “Steve” is present in the list else we would get false.
10. clear(): It is used for removing all the elements of the array list in one go. The below code will remove all the elements of ArrayList whose object is obj.
obj.clear();
HashMap is a Map based collection class that is used for storing Key & value pairs, it is denoted as HashMap<Key, Value> or HashMap<K, V>. In other languages, they are referred to as Dictionaries (Swift, Python, JavaScript). This class makes no guarantees as to the order of the map. It is similar to the Hashtable class except that it is unsynchronized and permits nulls(null values and null key).
It is not an ordered collection which means it does not return the keys and values in the same order in which they have been inserted into the HashMap. It does not sort the stored keys and Values. You must need to import java.util.HashMap
or its superclass in order to use the HashMap class and methods.
A Set is an unordered collection, which means that it has all of the methods that Collections must-have. Unlike ArrayLists, it only contains unique members.