Powered by Blogger.

How to Iterate Generic type of ArrayList

>> Sunday, June 10, 2012

Generics in Java
Generics were introduced in Java 1.5 to define abstract types for the variables of by enclosing a comma-separated list of their names within angle brackets after the name of the class or interface. We can user this type of variables any where in the class if a type is required in any instance fields or methods of the class.

Now in this post we are learning how to iterate the Generic type of list.

Normally we used to iterate any list we take the help of Iterator. If it is normal list as well we can iterate the Generic type.

Lets take a bean class of User with a constructor and setters and getters methods.

Here the User having the String type name and link variables.

/*
 * Created on Jun 6, 2012
 */
package com.javabynataraj;

/**
 * @author Muralidhar N
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

public class User {
 
 public String name;
 public String url;
 //constructor to create a new Object with name and url parameters.
 User(String n,String u){
  this.name=n;
  this.url=u;
 }
 //overrided toString method here to reduce the looping repetitions.
 public String toString(){
   return this.name + " " + this.url;
 } 
 //setters and getters
 public String getName() {
  return name;
 }
 public String getUrl() {
  return url;
 }
 public void setName(String string) {
  name = string;
 }
 public void setUrl(String string) {
  url = string;
 }

}


Take a list object in ListUser class add the list objects with the name and link.
Here we can iterate the listuser object by normal iteration and for-each loop.These are given below in the program.
/*
 * Created on Jun 6, 2012
 *
 */
package com.javabynataraj;

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

/**
 * @author Muralidhar N
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class ListUser {
 
 public static void main(String[] args) {
  
  List userlist = new ArrayList();
  userlist.add(new User("name1","url1"));
  userlist.add(new User("name2","url2"));
  userlist.add(new User("name2","url3"));
  userlist.add(new User("name4","url4"));
  userlist.add(new User("name5","url5"));
  userlist.add(new User("name6","url6"));
  
  System.out.println("Using Iterator");
  Iterator<user> it = userlist.iterator();
  while(it.hasNext()){
   User u =  it.next();
   System.out.println(u.getName()+"   "+u.getUrl());
  }
  System.out.println("Using for-each loop");
      for (User u : list) {
         System.out.println(u.link + " " + u.url);
      }
 }

}


by using these ways we can do the iteration of User class type by using to the arraylist.

We can see the out put to this program as given below.

How to iterate Generic type of ArrayList_JavabynataraJ

We can do the iterations as mentioned above. if you found any more in different ways leave the comments.
The detailed document on Generics


Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.