0% found this document useful (0 votes)
4 views4 pages

Java Anagram Grouping and Employee Sorting

Annalakshmi G documented her learning experience with IntelliJ and completed two Java tasks: grouping anagrams from a given array of strings and creating an Employee class to sort and display employee details by name length. The Employee class includes attributes for name, age, and city, and the sorting is implemented using a custom comparator. The document also includes the Java code for both tasks, demonstrating the implementation of the anagram grouping and employee sorting functionalities.

Uploaded by

annalakshmig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Anagram Grouping and Employee Sorting

Annalakshmi G documented her learning experience with IntelliJ and completed two Java tasks: grouping anagrams from a given array of strings and creating an Employee class to sort and display employee details by name length. The Employee class includes attributes for name, age, and city, and the sorting is implemented using a custom comparator. The document also includes the Java code for both tasks, demonstrating the implementation of the anagram grouping and employee sorting functionalities.

Uploaded by

annalakshmig
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Annalakshmi G (12/08/2020)

 Installed Intellij and learnt to work in that environment.


 Learnt N4 and documented.
 Completed two task in java.
(1. Group anagrams
Given a array of strings {"eat", "eta", "tan", "ate", "nat", "bat"};
Group the anagrams together. Eg: "eat","eta" and "ate" are anagrams in the above
array

2. Create a Employee class with name, age and city.


Sort and display the employee details in the order of length of their name.)

Create a Employee class with name, age and city. Sort and display the employee
details in the order of length of their name.

package [Link];

public class Employee {


private String name;
private int age;
private String city;

public Employee(String name, int age, String city) {


[Link] = name;
[Link] = age;
[Link] = city;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


[Link] = age;
}

public String getCity() {


return city;
}
public void setCity(String city) {
[Link] = city;
}

@Override
public String toString() {
return "Employee [Name: " +name + ", Age:" +age +", City:" + city +".]";
}
}
package [Link];

import [Link];
import [Link];

public class Test {


public static void main(String[] args) {
ArrayList<Employee> obj = new ArrayList<Employee>();
[Link](new Employee("Anandhanarayanan", 35,"Chennai" ));
[Link](new Employee("AnnaLakshmi", 33,"Hyderabad" ));
[Link](new Employee("karthick", 34,"chennai" ));
[Link](new Employee("Simi", 35,"Calicut" ));
[Link]("Employee Details before sorting:");
for(Employee empobj:obj) {
[Link](empobj);
}
[Link](obj,(e1,e2)->([Link]().length()>[Link]().length()?+1:
([Link]().length()<[Link]().length()?-1:0)));

[Link]("-----------------------------------------------------------------------");
[Link]("Employee Details after sorting as per the length of the
Employee name:");
for(Employee empobj:obj) {
[Link](empobj);
}
}
}

OUTPUT:
Group anagrams
Given a array of strings {"eat", "eta", "tan", "ate", "nat", "bat"};
Group the anagrams together.
Eg: "eat","eta" and "ate" are anagrams in the above array

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

public class StringsAnagram {


public static List<List<String>> strAnagram(String[] inputStrs){

List<List<String>> answer = new ArrayList<>();


HashMap<String, List<String>> hmObj = new HashMap<>();
for(String str: inputStrs){
char[] charArray =[Link]();
[Link](charArray);
String key = new String(charArray);
if([Link](key))
{
[Link](key).add(str);
}
else {
List<String> strList = new ArrayList<>();
[Link](str);
[Link](key, strList);
}
}

[Link]([Link]());
return answer;
}

public static void main(String[] args) {


String[] inputStrs= {"eat", "eta", "tan", "ate", "nat", "bat"};
List<List<String>> answer= strAnagram(inputStrs);
[Link](val->[Link](val + " "));
}
}

Output:

You might also like