Can we use Linq for geting value from collection object in java? -
now days working on blackberry within parse json array string , convert hashtable this
this json string
[ { "stdid":"a1", "rollno":"23", "class":"first" }, { "stdid":"a2", "rollno":"13", "class":"first" }, { "stdid":"a3", "rollno":"53", "class":"second" }, { "stdid":"a4", "rollno":"33", "class":"third" }, ]
and parse hashtable as
hashtable t1=new hashtable(); t1.put("stdid","a1"); t1.put("rollno","23"); t1.put("class","first"); hashtable t2=new hashtable(); t2.put("stdid","a2"); t2.put("rollno","13"); t2.put("class","first"); hashtable t3=new hashtable(); t3.put("stdid","a3"); t3.put("rollno","53"); t3.put("class","second"); hashtable t4=new hashtable(); t4.put("stdid","a4"); t4.put("rollno","33"); t4.put("class","third"); hashtable main=new hashtable (); main.put(new integer(1), t1); main.put(new integer(2), t2); main.put(new integer(3), t3); main.put(new integer(4), t4);
so can i/ how can retrieve value as
select students have first class
so 1 can me? possible linq ?
uhm... yes! need library java linq stuff: //github.com/nicholas22/jpropel-light
how it:
import java.util.hashtable; import lombok.extensionmethod; import propel.core.utils.linq; import java.util.list; import lombok.function; @extensionmethod({linq.class}) public class main { public static void main(string[] args) { hashtable t1=new hashtable(); t1.put("stdid","a1"); t1.put("rollno","23"); t1.put("class","first"); hashtable t2=new hashtable(); t2.put("stdid","a2"); t2.put("rollno","13"); t2.put("class","first"); hashtable t3=new hashtable(); t3.put("stdid","a3"); t3.put("rollno","53"); t3.put("class","second"); hashtable t4=new hashtable(); t4.put("stdid","a4"); t4.put("rollno","33"); t4.put("class","third"); hashtable main = new hashtable(); main.put(new integer(1), t1); main.put(new integer(2), t2); main.put(new integer(3), t3); main.put(new integer(4), t4); list<hashtable> result= main.values().where(classequals("first")).tolist(); for(hashtable ht : result) system.out.println(ht.get("stdid")); } @function private static boolean classequals(hashtable table, string _class) { return table.get("class") != null && table.get("class").equals(_class); } }
Comments
Post a Comment