sorting based on hashmap value,string swapping, deadlock program.
Utilisateur anonyme
HashMap map = new HashMap(); map.put("Prakash", new Emp(10, "Prakash")); map.put("Trinadh", new Emp(5, "Trinadh")); map.put("Roksana", new Emp(1, "Roksana")); map.put("IsabellaWang", new Emp(2, "IsabellaWang")); List list = new ArrayList(map.values()); Collections.sort(list); LinkedHashMap lmap = new LinkedHashMap(); Iterator it = list.iterator(); while (it.hasNext()) { Emp e = it.next(); Iterator it1 = map.keySet().iterator(); while (it1.hasNext()) { String e1 = it1.next(); if (e.getName().equals(e1)) { lmap.put(e1, e); } } } System.out.println(lmap); } 2.public class Q1 { String str1="java"; String str2="python"; Thread td1=new Thread("My Thread"){ public void run(){ while(true){ synchronized(str1){ synchronized(str2){ System.out.println("the given one is:"+str1+str2); } } } } }; Thread td2=new Thread("MY THREAD2"){ public void run(){ while(true){ synchronized(str1){ synchronized(str2){ System.out.println("the other thread:"+str1+str2); } } } } }; public static void main(String... a){ Q1 q=new Q1(); q.td1.start(); q.td2.start(); } } 3.String s1="Capgemini"; String s2="sabre"; //append 2nd string to first s1=s1+s2; //store initial string s1 in to a s2 s2=s1.substring(0,s1.length()-s2.length()); //store initial strings2 in strings1 s1=s1.substring(s2.length()); System.out.println("the first string is:"+s1+"................"+"and second one is:"+s2);