-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* Group the employees by city | ||
* Input: | ||
* ---------------- | ||
* Employee | City | ||
* --------------------- | ||
* Arijit Kolkata | ||
* Minaz Mumbai | ||
* Rahul Chennai | ||
* Rajesh Kolkata | ||
* Sekhar Chennai | ||
* Kasim Chennai | ||
* Output: | ||
* ---------------- | ||
* Kolkata: Arijit,Rajesh | ||
* Mumbai: Minaz | ||
* Chennai: Rahul,Sekhar,Kasim | ||
*/ | ||
|
||
import java.util.*; | ||
public class Set_Employees_By_Location { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
LinkedHashMap<String,String> emp = new LinkedHashMap<>(); | ||
System.out.print("Number of Employee: "); | ||
int n = sc.nextInt();sc.nextLine(); | ||
for (int i = 0; i < n; i++) { | ||
System.out.print((i+1)+"> Enter name: "); | ||
String name = sc.nextLine(); | ||
System.out.print("Enter city: "); | ||
String city = sc.nextLine(); | ||
emp.put(name,city); | ||
} | ||
System.out.println(); | ||
// Show my data set | ||
for(HashMap.Entry<String,String> data : emp.entrySet()) | ||
{ | ||
System.out.println(data.getKey()+": "+data.getValue()); | ||
} | ||
System.out.println(); | ||
|
||
LinkedHashMap<String,String> update = new LinkedHashMap<>(); | ||
for(HashMap.Entry<String,String> data : emp.entrySet()) | ||
{ | ||
if(!update.containsKey(data.getValue())) | ||
{ | ||
update.put(data.getValue(),data.getKey()); | ||
} | ||
else | ||
{ | ||
update.put(data.getValue(),update.get(data.getValue())+","+data.getKey()); | ||
} | ||
} | ||
// List data | ||
for(HashMap.Entry<String,String> ans : update.entrySet()) | ||
{ | ||
System.out.println(ans.getKey()+": "+ans.getValue()); | ||
} | ||
} | ||
} |