Enter your E-mail Address below for Free E-mail Alerts right Into your Inbox: -

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

Sunday

How to convert Java Object Array to List in Java

How to convert Java Object Array to List in Java

We studied how to create a Array, how to sort it etc. Now we are going to convert the Array to List .

Example:

public final class Test {

static String[] strArray = new String[10];

    public static void main(String args[])
    {
    strArray[0]="abc";
    strArray[1]="def";
    strArray[2]="ghi";
    strArray[3]="jkl";
    strArray[4]="mno";
   
    List list = Arrays.asList(strArray);
    Iterator li = list.iterator();
    while(li.hasNext())
    {
    System.out.println(li.next());
    }
    }
 
}

How to convert a map to list in Java with example

How to convert a map to list in Java with example

Before doing this you must have clear idea of List and Map in Java. List is an Interface implementing the Collection Interface.List allows duplicate values.Some Classes implementing List Interfaces are ArrayList,LinkedList etc.Map is not a Collection.Some Classes implementing the Map Interfaces are HashMap,TreeMap etc.

Logic:
  1. First we have to convert the Map into a Set using entryset.
  2. Second we have to convert Set into List.
Example:

public class HelloWorld{

public static void main(String[] args) {
HashMap<String, Integer> mapOne = new HashMap<String, Integer>();

mapOne.put("one", 1);
mapOne.put("two", 2);
mapOne.put("three", 3);
mapOne.put("four", 4);
mapOne.put("five", 5);
        
        Set<Entry<String, Integer>> set = mapOne.entrySet();
        List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
        Iterator<Entry<String, Integer>> it = list.iterator();
        
        while (it.hasNext()) {
            Entry<String, Integer> entry = it.next();
            System.out.println("Map Converted to List : " + entry);
        }
}
}


Output:
Map Converted to List : two=2
Map Converted to List : five=5
Map Converted to List : one=1
Map Converted to List : four=4
Map Converted to List : three=3

Wednesday

Difference between List and Set and Map

Difference between List and Set and Map

List,Set,Map are the basic collection interfaces, although Map doesn't inherit Collection Interface, it belongs to the Collection in Java.

What is List in Java? 

List implements the Collection<E>, Iterable<E> Interfaces.

public interface List<E>
extends Collection<E>.


List in Java allows to store objects by storing them in each index. Other than Set List allows Duplication of elements.To store an element in List use the add() method and to get a value use get() method.

Some Classes implementing List Interface are ArrayList,LinkedList etc.

List throws these Exceptions:




UnsupportedOperationException
ClassCastException
NullPointerException 
IllegalArgumentException
IndexOutOfBoundsException 
Example:
package com.jobberindia;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        list.add("Hello");
        list.add("World");
        list.add("Hello1");
        list.add("World2");
        System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}

}
What is Set in Java?

Set implements the Collection<E>, Iterable<E> Interfaces.

public interface Set<E>
extends Collection<E>

Set in Java allows to store objects by storing them in each index. Other than Set Set doesn't allows Duplication of elements.To store an element in Set use the add() method.

Example:



package com.jobberindia;
import java.util.*;
public class HelloWorld {

 public static void main(String[] args) {
        Set<String> set = new TreeSet<String>();

        set.add("Hello");
        set.add("World");
        set.add("Hello1");
        set.add("World2");
        set.add("Hello3");
        set.add("World4");
        System.out.println("Data stored in mainlist "+set); 
 }
}

Some Classes implementing List Interface are TreeSet,HashSet etc.

What is Map in Java?

Other than List and Set Map is used to Store Key and Value Pair.The key is unique and values are duplicate allowed. Map implement the collection Interface. It allows only one null value which is stored in the zeroth index.
These are the some of the classes implements the Map<K,V> Interface.  HashMapHashtableIdentityHashMapLinkedHashMap.
Example:
public class HelloWorld {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Hello");
list.add("Hello1");
list.add("World");
Map<Integer,String> map = new HashMap<Ineger,String>();
list.add("World2");
map.put(i, list.get(i));
for(int i=0;i<list.size();i++)
{ }
}
System.out.println("Data stored in mainlist "+map.get(2)); }
Difference between List VS Set VS Map Interface:
  1. List allows duplicate values.
  2. List allows any number of null values.
  3. Set doesn't allows duplicate values.
  4. Set allows one null value.
  5. Map allows one null key value.
  6. List maintains the insertion order.
  7. Set and Map doesn't maintain the insertion order.
  8. Map stores key and value pair.
Based on your usage select these interfaces carefully.


How to get sublist from an ArrayList in Java

How to get sublist from an ArrayList in Java


We already studied about ArrayList in Java. Now we can see how to sublist an ArrayList. the scenario occurs when a ArrayList contains data that is to be displayed into two areas.
For this we use subList(arg1,arg2);
Example:
/** * author: JOBBERINDIA
 */
package com.jobberindia;
import java.util.*;
/**
 * @author user
 *
 */
public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
     
        list.add("Hello");
        list.add("World");
        list.add("Hello1");
        list.add("World2");
        list.add("Hello3");
        list.add("World4");
     
        System.out.println("Data stored in mainlist "+list);
System.out.println("Size of list before removing "+list.size());
ArrayList<String> subList = new ArrayList<String>(list.subList(1, 2));
System.out.println("Data stored in sublist "+subList);
System.out.println("Size of list before removing "+subList.size());
}
}


OutPut:
Data stored in mainlist [Hello, World, Hello1, World2, Hello3, World4]Size of list before removing 6Data stored in sublist [World]
Size of list before removing 1
Possible Errors are IndexOutOfBoundException and IllegalArgumentException.

Monday

ArrayList in Java with example Collections framework

ArrayList in Java with example Collections framework

ArrayList which implements the List Interface which belongs to the Collections Framework. ArrayList is very fast and ease of use. So developers prefer ArrayList over Arrays in java.

Advantages:


  • Other than Arrays ArrayList can increase or decrease its size based on the data size.
  • Fast so developers prefer ArrayList
Example:



/**
 * author: JOBBERINDIA
 */
package com.jobberindia;

import java.util.*;
/**
 * @author user
 *
 */
public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
        List<String> list = new ArrayList<String>();
        
        list.add("Hello");
        list.add("World");
System.out.println("Size of list before removing"+list.size());
list.remove("Hello");
System.out.println("Size of list "+list.size());
}

}

Output: - 

Size of list before removing2
Size of list 1

Important Methods belongs to ArrayList:

  1.                 list.add(arg0);
  2. list.add(arg0, arg1);
  3. list.addAll(arg0);
  4. list.addAll(arg0, arg1);
  5. list.clear();
  6. list.contains(arg0);
  7. list.containsAll(arg0);
  8. list.containsAll(arg0);
  9. list.equals(arg0);
  10. list.get(arg0);
  11. list.size();
  12. list.isEmpty();
  13. list.lastIndexOf(arg0);
  14. list.remove(arg0);
  15. list.removeAll(arg0);
  16. list.toArray();
  17. list.toString();