Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

Saturday, August 15, 2009

Difference between Set and List (Set vs List)

Set: an Interface available in "java.util" package.
  • a collection that cannot contain duplicate elements
  • generally order of the elements does not matter
  • models the mathematical set abstraction and is used to represent sets, such as the card comprising a poker hand
List: an Interface available in "java.util" package
  • a collection that can contain duplicate elements
  • it is an ordered collection, also called as a sequence
  • user of a List generally has control over where in (at what index) the List the user want to insert an element and access it using its index
When you access the Set using its Iterator, it doesn't guarantee the order of elements, whereas when you access the List using its Iterator, it gives you a sequential access to the elements in the list one by one.

For the better understanding, you can just execute the example program in my previous post "How Do I Convert Set into List".

Thursday, August 06, 2009

Converting an Array to a List

Java Collections API has very useful methods to play with all the data structures. Conversion between the collections made so easy.

following line of code converts an array into a list.
List list = Arrays.asList(str); // str is an array

Following program converts an array of strings into a List.

package com.blog.javaexposure.util;

import java.util.Arrays;
import java.util.List;

public class Array2List {

public static void main(String rags[]){
// Creating a string array
String[] str = {"One","Two","Three","Four","Five"};

// Convert array to List
List list = Arrays.asList(str);

System.out.println("Elements in the list are:");
//Checking the elements in the list
for(String s : list){
System.out.println(s);
}
}
}

Tuesday, August 04, 2009

How do I convert Set into List?

Conversion between the collections made pretty easy in Java.

Converting from a Set/List to List/Set can be done by passing the Set/List instance to the constructor of List/Set such as ArrayList/HashSet respectively.

Following line of code performs the conversion (from Set to List):

Set strSet = new HashSet();
strSet.add("one")
.
.
.
strSet.add("xxx");

List strList = new ArrayList(set);

Above List can be converted back to Set using the following line of code:

Set setObject = new HashSetst(strList);

Following is the example program that does the entire same thing we discussed above:
-----
package com.blog.javaexposure.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Set2List {

public static void main(String rags[]) {
// Creating a Set object
Set set = new HashSet();
// Adding elements to the Set object
set.add("One");
set.add("Two");
set.add("Three");
set.add("Four");

// Checking the elements in the Set
System.out.println("Elements in the set are:");
for (Object obj : set) {
System.out.println(obj);
}

// Creating a list object
List list = new ArrayList(set);

// Checking the elements in the list
System.out.println("Elements in the list are:");
for (Object obj : list) {
System.out.println(obj);
}
// Removing elements from the set
set.clear();
System.out.println("Number of elements in the set are: "+set.size());

// Converting list to set
set = new HashSet(list);

// Checking the elements in the Set
System.out.println("Elements in the set are:");
for (Object obj : set) {
System.out.println(obj);
}
}
}
-----
È