Wednesday, 22 September 2010

My Quick Reference

Class with Static data which Returns as List

package demo.combobox.simple_combobox;

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

public class ShirtData {

private static List<String> colors = new ArrayList<String>();
private static List<String> sizes = new ArrayList<String>();



public static final List<String> getColors() {
return new ArrayList<String>(colors);

public static final List<String> getSizes() {
return new ArrayList<String>(sizes);

How to cast an Object to an int in java?

If you're sure that this object is an Integer :

int i = (Integer) object;

Or, starting from Java 7, you can equivalently write:

int i = (int) object;

Beware, it can throw a ClassCastException if your object isn't an Integer and aNullPointerException if your object is null.

This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int.

int is a primitive so it can't be stored as an Object, the only way is to have an intconsidered/boxed as an Integer then stored as an Object.

If your object is a String, then you can use the Integer.valueOf() method to convert it into a simple int :

int i = Integer.valueOf((String) object);

It can throw a NumberFormatException if your object isn't really a String with an integer as content.

Java Inner class example

import java.util.*;

public class SampleVM {

private List<MyData> names = new ArrayList<MyData>();

public SampleVM() {
names.add(new MyData("name 1",true));
names.add(new MyData("name 2",false));
names.add(new MyData("name 3",false));
names.add(new MyData("name 4",false));
names.add(new MyData("name 5",false));


public List<MyData> getNames() {
return names;
public class MyData{
String name;
boolean open;
public String getName() {
return name;
public void setName(String name) { = name;
public boolean isOpen() {
return open;
public void setOpen(boolean open) { = open;
public MyData(String name, boolean open) {
super(); = name; = open;


Hibernate Where in clause to Delete multiple items

public void deletePracticeAppMenu(List<Long> prAppMenuIDs, Long practiceID) {
String query = "delete from PracticeAppMenu where PrAppMenuID in (:appMenuIDs) and practiceID = :pracid ";
Query q = getCurrentSession().createQuery(query);
q.setParameterList("appMenuIDs", prAppMenuIDs);
q.setParameter("pracid", practiceID);

1 comment:

  1. Good efforts. All the best for future posts. I have bookmarked you. Well done. I read and like this post. Thanks. best mba colleges in punjab