Oracle Interview Question
Software Engineer in TestsCountry: India
Interview Type: In-Person
code in java:
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c;
Scanner sc = new Scanner(System.in);
Map <Integer,Integer> ex= new HashMap<Integer,Integer>();
ex.put(1, 10);
ex.put(2, 30);
ex.put(3, 20);
ex.put(4, 40);
System.out.println("the values are"+ ex);
System.out.println("enter key value to search");
a=sc.nextInt();
b=ex.remove(a);
System.out.println("the values are"+ b);
a=b+a;
b=a-b;
a=a-b;
ex.put(a,b);
System.out.println("the values are"+ ex);
}
}
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c;
Scanner sc = new Scanner(System.in);
Map <Integer,Integer> ex= new HashMap<Integer,Integer>();
ex.put(1, 10);
ex.put(2, 30);
ex.put(3, 20);
ex.put(4, 40);
System.out.println("the values are"+ ex);
System.out.println("enter key value to search");
a=sc.nextInt();
b=ex.remove(a);
System.out.println("the values are"+ b);
a=b+a;
b=a-b;
a=a-b;
ex.put(a,b);
System.out.println("the values are"+ ex);
}
}
package practice;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class prac {
public static void find(String value,Map m1)
{
Set s = m1.entrySet();
Iterator i = s.iterator();
String Key;
String Value;
Map.Entry m ;
while(i.hasNext())
{
m = (Map.Entry)i.next();
if(m.getValue().equals(value))
{
System.out.println(m.getKey());
}
Key = m.getKey().toString();
Value = m.getValue().toString();
m1.remove(m.getKey());
m1.put(Value, Key);
}
System.out.println("after swapping: "+m1);
}
public static void main(String args[])
{
Map m1 = new ConcurrentHashMap();
m1.put("1", "aa");
m1.put("2", "bb");
m1.put("3", "cc");
find("cc",m1);
}
}
public void findKey(String s)
{
HashMap keyValues = new HashMap();
keyValues.put(new Integer(1), new String("One"));
keyValues.put(new Integer(2), new String("Two"));
keyValues.put(new Integer(3), new String("Three"));
keyValues.put(new Integer(4), new String("Four"));
Iterator iter = keyValues.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
if (entry.getValue().equals(s)) {
System.out.println(((Integer) entry.getKey()).intValue());
}
}
}
package myworks.tricky;
- Raja June 27, 2013import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MyMap {
public static void main(String[] args) {
String val = "value";
String key = null;
Map<String, String> map = new HashMap<String, String>();
map.put("1", "map");
map.put("2", "value");
System.out.println("Intial Map " + map);
if (map.containsValue(val)) {
Set<Entry<String, String>> s = map.entrySet();
Iterator<Entry<String, String>> i = s.iterator();
for (; i.hasNext(); i.next()) {
Entry<String, String> e = i.next();
if (e.getValue().equals(val)) {
key = e.getKey();
System.out.println("The key is " + e.getKey());
}
}
}
if (key.length() > 0) {
map.remove(key);
map.put(val, key);
}
System.out.println("Final Map " + map);
}
}