Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

map related example

Hi,

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class FirstSwap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] arr = { "bc", "bd" };
		System.out.println("result is--->"+Arrays.toString(firstSwap(arr)));

	}

	public static String[] firstSwap(String[] strings) {

		Map<String, Integer> map = new HashMap<String, Integer>();

		for (int i = 0; i < strings.length; i++) {

			String key = String.valueOf(strings[i].charAt(0));
			System.out.println("key is--->"+key);

			if (map.containsKey(key)) {

				int val = map.get(key);
				if (val == -1) {
					continue;
				}

				// swap
				int pos = map.get(key);
				String tmp = strings[pos];
				strings[pos] = strings[i];
				strings[i] = tmp;

				// set a flag
				map.put(key, -1);

				for (Map.Entry<String, Integer> entry : map.entrySet()) {
					String keys = entry.getKey().toString();
					Integer value = entry.getValue();
					System.out.println("key, " + keys + " value " + value);
				}

			} else {
				map.put(key, i);
				for (Map.Entry<String, Integer> entry : map.entrySet()) {
					String keys = entry.getKey().toString();
					Integer value = entry.getValue();
					System.out.println("key, " + keys + " value " + value);
				}
			}

		}

		return strings;
	}

}

Open in new window


above code generated below code.

key is--->b
key, b value 0
key is--->b
key, b value -1
result is--->[bd, bc]



I have not understood how above code is working.

Basically array is being passed from main method as below into the firstSwap method

String[] arr = { "bc", "bd" };
		System.out.println("result is--->"+Arrays.toString(firstSwap(arr)));

Open in new window




then writing firstSwap() method creating hash map whose key is string and value is integer
	public static String[] firstSwap(String[] strings) {

		Map<String, Integer> map = new HashMap<String, Integer>();

Open in new window


then looping the input array

for (int i = 0; i < strings.length; i++) {

			String key = String.valueOf(strings[i].charAt(0));
			System.out.println("key is--->"+key);

Open in new window


if map contains a key say b how it check same key at second element?

if (map.containsKey(key)) {

				int val = map.get(key);
				if (val == -1) {
					continue;
				}

Open in new window

Below code also not clear

int pos = map.get(key);
				String tmp = strings[pos];
				strings[pos] = strings[i];
				strings[i] = tmp;

				// set a flag
				map.put(key, -1);//why we need -1???

				for (Map.Entry<String, Integer> entry : map.entrySet()) {
					String keys = entry.getKey().toString();
					Integer value = entry.getValue();//why we need get value what we are we doing with empty map by getting each row of entrySet set entries??
					System.out.println("key, " + keys + " value " + value);
				}

			} else {//when the code comes to else loop??
				map.put(key, i);
				for (Map.Entry<String, Integer> entry : map.entrySet()) {
					String keys = entry.getKey().toString();
					Integer value = entry.getValue();
					System.out.println("key, " + keys + " value " + value);
				}
			}

		}

		return strings;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

any other easy way to do it? please advise
please tell me what the exact requirement is
Avatar of gudii9

ASKER

I have not clearly understood above code in terms of how the flow and workings
Did you not write the code?
I wouldn't spend time trying to understand a badly written peice of code. It wont achieve anything.

Move onto shaving the next yak.