Thread: Programming series Question 1

Forum : Problem Solving   14-5-2018 11:32:29 PM
User Image
Amritk

Newbie

Joined: May 8, 2017

Points: 230

Threads: 57

Posts: 211

Programming series Question 1

| Quote Date : May 14, 2018    Views:270    

We have one hashmap object, with this object we want to display only repeated values .
Means, suppose if we have one hashmap it contains 1 "a", 2 "b", 3 "c", 4 "d", 5 "a", 6 "b", 7 "a" with this hashmap we want to display only repeated key and value pair.

Output: 5 "a", 6 "b" ,7  "a"

Comments

User Image
Ankit123

Newbie

Joined : Dec 8, 2017

Points : 100

Threads: 36

Posts: 33

Re: Programming series Question 1

Reply Date : May 25, 2018


package com.google.logical;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MapTest {
public static void main(String args[]) {
 
  Map map = new ConcurrentHashMap();
  map.put(1, "a");
  map.put(2, "b");
  map.put(3, "c");
  map.put(4, "d");
  map.put(5, "a");
  map.put(6, "b");
  map.put(7, "a");
  System.out.println("Original Map : " + map);
 
  dup(map);  
}
public static void dup(Map map) {
String value = "";  
String store = "";
for (Integer key : map.keySet())
if(store.indexOf(map.get(key))==-1 && map.containsValue(value = map.remove(key)))
store = store + value;
System.out.println("Repeated Map values : " + map);
}
}

/*
Output:
Original Map : {1=a, 2=b, 3=c, 4=d, 5=a, 6=b, 7=a}
Repeated Map values : {5=a, 6=b, 7=a}

*/
   

Load More

No More Comments