Showing posts with label ArrayList. Show all posts
Showing posts with label ArrayList. Show all posts

Saturday, 2 May 2015

#05 How equlas() and hashCode() mehtods work - java.


What are equals and hashCode method do?

Equals and hashCode in Java are two fundamental method which is declared in Object class and part or core Java library. equals() method is used to compare Objects for equality while hashCode is used to generate an integer code corresponding to that object.


In my previous post Easy way to understand HashSet , I described HashSet does not allow duplicate value. 
But think about scenario I have a Student class.

Student POJO Class

I am creating two objects of student.
checking output of equals method

Now look at the output

output of equals method

Wheteher roll numbers ,fname ,lname are same for both objects output is false.
whenever we use == operator for object or equals to method for object. java check whether two reference pointing same memory location or not. if they are not pointing same memory, java consider it as two different objects. 

In this scenario HashSet also add s1 and s2. But it is wrong according to user view.

Here we need to change default behaviour of java equals method. 

So we need to override the equals method of object class and custmize it to our requirement.

I am adding two method in my Student class.

We are adding hashCode method also,because equals object must have same hash code.

Now run the application and look at the output :


It is common practise that whenever we want to add Class object to HashSet, TreeSet,ArrayList, or any java collection we override equals() and hashCode method.

Next ... 
In my next post I will explain functionality of TreeSet.

Hope it will help full.

Saturday, 11 April 2015

# 03 Important functions of ArrayList

Dear Readers, 

In my last post I presented overview of ArrayList. Now I am writing some common functions which are often use while using ArrayList.

1.add() 
We can add element by using add(Object object) method.

Example:


2. add(int index,Object object)
This method add object at given index number.

Example :


3. remove(int index)

This method removes element from specified index.

Example:




4. remove(Object object)

This method will search specified object in list if object exist in list then it will remove it and return true otherwise it return false.

Example:

5. Sorting of ArrayList 

To sort arrayList, we can use java.util.Collections class inbulit method sort.

Example :



6. size()
This method will return the no. of elements in arrayList.

Example :

7. get(int index)

This method will return the element from given index.

Example :

8. contains(Object object)

This method will return true if object present in arrayList otherwise return false.

Example :



Complete Code 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.javasuitor.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ArrayListDemo { 
 public static void main(String[] args) {
  List<String> countryList = new ArrayList<String>();  
  // add() method
  countryList.add(new String("India"));
  countryList.add(new String("England"));
  countryList.add(new String("Australia"));
  countryList.add(new String("Japan"));
  countryList.add(new String("China"));
  // We can add object at specified index.
  // Here Iraq will add on index 2 of countryList.
  countryList.add(2,new String("Iraq"));  
  System.out.println("After add operations");
  displayList(countryList);  
  // remove(int index) method removes element from given index
  // this will remove England from countryList.  
  countryList.remove(1);  
  // remove (Object object) method
  countryList.remove(new String ("Japan"));  
  System.out.println("\nAfter remove operation ");
  displayList(countryList);  
  // To perform sorting on ArrayList we can use Collection class's inbuilt method 
  Collections.sort(countryList);  
  System.out.println( "\nAfter sorting ");
  displayList(countryList);  
  // To get number of elements exist in list we use size() method.
  System.out.println("\ncountryList size = "+countryList.size());  
  // To get element from desired index we can use get method of ArrayList  
  System.out.println("\nElement at index 3 : "+countryList.get(3));  
  // contains() method to check whether element is present in list or not  
  System.out.println("Is India present in list : "+countryList.contains(new String("India")));
  
 }
 
 // method shows the contents of ArrayList
 public static void displayList(List<String> list){
  // We are using iterator to display contents of list
  Iterator<String> iterator = list.iterator();
  while(iterator.hasNext()){
   System.out.println(iterator.next());
  }
  
  
 }

}

Output




Sunday, 8 February 2015

#2. ArrayList Overview and Examples


ArrayList is an inbuilt java class available in java.util package. It implements the List interface. 

ArrayList  acts like an array, but it has extra features over array which make it very useful.

Features of ArrayList are :

1. ArrayList is growable. The size of ArrayList is not fixed as it was in array we can store as many elements as we want.when new element require to store in ArrayList and there is no place in ArrayList , it java automatically increase the size of ArrayList.

2. ArrayList can store any kind of object. ArrayList is collection of heterogeneous objects. We can store integer, double, string data in a single array list.  

Example:

1. Declare an ArrayList

We can declare an ArrayList by just using new key word.  



 2. Add elements into ArrayList

To add elements into ArrayList, we can use method add(). This is also an inbuilt method of ArrayList Class.



 You can see I added three different values into ArrayList object dummyList.

3. Access elements from ArrayList
All elements are added successfully into ArrayList. Now you can access these elements by get() method. We can access elements by using the index values of objects which start from 0 same as in array.

dummyList.get(0) will return the first object stored at index 0.


 size() method returns the number of elements situated in ArrayList.
This loop will print all the objects stored in list.

4.OutPut




You should try it by your self. I will post other uses of ArrayList in my next post.

I hope it will help you to get an overview of ArrayList.

Your valuable feedback will surely help me to improve.