Download our latest MNC Answers Application at Play Store. Download Now

Collection, Generics and Stream API Hands-Ons [Java > Collection, Generics and Stream API] | Accenture TFA Training Pre-Learning Modules

Disclaimer: The primary purpose of providing this solution is to assist and support anyone who are unable to complete these courses due to a technical issue or a lack of expertise. This website's information or data are solely for the purpose of knowledge and education.

Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).

All Question of the Hands-On Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!


1. ArrayList - Simple Handson1

Path: JAVA/Collection, Generics and Stream API/ArrayList - Simple Handson1/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
String[] str=new String[n+1];
int i;
for(i=0;i<=n;i++)
{
    str[i]=sc.nextLine();
}
ArrayList<String> fruit=new ArrayList<String>();
for(i=1;i<=n;i++)
{   
    fruit.add(str[i]);
}
System.out.println(fruit);
}

}


2. ArrayList - Simple Handson2

Path: JAVA/Collection, Generics and Stream API/ArrayList - Simple Handson2/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
    String[] str=new String[n+1];
    int i;
    for(i=0;i<=n;i++)
    str[i]=sc.nextLine();
    ArrayList<String> fruit=new ArrayList<String>();
    for(i=1;i<=n;i++)
    {
        fruit.add(str[i]);
    }
    for(i=0;i<n;i++)
    {
        System.out.println(fruit.get(i));
    }
    
}

}


3. Book Manipulation 

Path: JAVA/Collection, Generics and Stream API/Book Manipulation/Book.java
public class Book {

private int isbnno;
private String bookName;
private String author;
public int getIsbnno() {
return isbnno;
}
public void setIsbnno(int isbnno) {
this.isbnno = isbnno;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}


Path: JAVA/Collection, Generics and Stream API/Book Manipulation/Library.java
import java.util.*;
public class Library
{
    private ArrayList<Book> bookList=new ArrayList<Book>();
    public void addBook(Book bobj)
    {
        bookList.add(bobj);
    }
    public void setBookList(ArrayList<Book> l)
    {
        bookList=l;
    }
    public ArrayList<Book> getBookList()
    {
        return bookList;
    }
    public boolean isEmpty()
    {
        boolean f=false;
        if(bookList.isEmpty())
        {
            f=true;
        }
        return f;
    }
    public ArrayList<Book> viewBooksByAuthor(String author)
    {
        ArrayList<Book> a1=new ArrayList<Book>();
        for(int a=0;a<bookList.size();a++)
        {
            if(bookList.get(a).getAuthor().equalsIgnoreCase(author))
            {
                a1.add(bookList.get(a));
            }
        }
        return a1;
    }
    public ArrayList<Book> viewAllBooks()
    {
        return bookList;
    }
    public int countnoofbook(String bname)
    {
        int ctr=0;
        for(int a=0;a<bookList.size();a++)
        {
            if(bookList.get(a).getBookName().equalsIgnoreCase(bname))
            {
                ctr++;
            }
        }
        return ctr;
    }
}


Path: JAVA/Collection, Generics and Stream API/Book Manipulation/Main.java
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        Library obj=new Library();
        int i=0;
        while(i==0)
        {
            System.out.println("1.Add Book\n2.Display all book details\n3.Search Book by author\n4.Count number of books - by book name\n5.Exit");
            System.out.println("Enter your choice:");
            int n=Integer.parseInt(sc.nextLine());
            if(n==1)
            {
                Book obj1=new Book();
                System.out.println("Enter the isbn no:");
                obj1.setIsbnno(Integer.parseInt(sc.nextLine()));
                System.out.println("Enter the book name:");
                obj1.setBookName(sc.nextLine());
                System.out.println("Enter the author name:");
                obj1.setAuthor(sc.nextLine());
                obj.addBook(obj1);
            }
            else if(n==2)
            {
                if(obj.isEmpty())
                {
                    System.out.println("The list is empty");
                }
                else
                {
                    ArrayList<Book> b=obj.viewAllBooks();
                    for(Book a:b)
                    {
                        System.out.println("ISBN no:"+a.getIsbnno());
                        System.out.println("Book name:"+a.getBookName());
                        System.out.println("Author name:"+a.getAuthor());
                    }
                }
            }
            else if(n==3)
            {
                System.out.println("Enter the author name:");
                String s=sc.nextLine();
                for(Book b:obj.viewBooksByAuthor(s))
                {
                    if(obj.viewBooksByAuthor(s).isEmpty())
                    {
                        System.out.println("None of the book published by the author "+s);
                        
                    }
                    else
                    {
                        System.out.println("ISBN no:"+b.getIsbnno());
                        System.out.println("Book name:"+b.getBookName());
                        System.out.println("Author name:"+b.getAuthor());
                    }
                }
            }
        }
    }
}


4. Count of Each Words

Path: JAVA/Collection, Generics and Stream API/Count of Each Words/CountOfWords.java
//import the necessary packages if needed
     import java.util.*;
@SuppressWarnings("unchecked")//Do not delete this line
public class CountOfWords
{
         public static void main (String[] args)
         {
             Scanner sc = new Scanner(System.in);
             String s=sc.nextLine();
             s=s.toLowerCase();
             String longString = "\"long\"";
             StringBuilder sb = new StringBuilder(s);
             for (int i=0;i<sb.length();i++)
             {
                 if(!(Character.isLetter(sb.charAt(i))))
                 {
                     if(sb.charAt(i)!=' ' && sb.equals(longString))
                     {
                         if(sb.charAt(i)!='\'')
                         {
                             sb.deleteCharAt(i);
                             System.out.println(sb);
                         }
                     }
                 }
             }
             String str[] =s.split("[\\s,;:.?!]+");
             Set<String> words = new HashSet<String>(Arrays.asList(str));
             List<String> wordList = new ArrayList<String>(words);
             Collections.sort(wordList);
             int count =0;
             System.out.println("Number of words "+str.length);
             System.out.println("Words with the count");
             int longCount=0;
             for(String word: wordList)
             {
                 for(String temp: str)
                 {
                     if(word.equals(temp))
                     {
                         ++count;
                     }
                 }
                 if(!(word.equals(longString)))
                 {
                    System.out.println(word+":"+count); 
                 }
                 else
                 longCount=count;
                 count=0;
                 boolean flag=false;
                 for(String str2 : str)
                 {
                     if(str2.equals(longString))
                     flag=true;
                 }
                 if(flag==true)
                 System.out.println(longString+":"+longCount);
             }
         }
}


5. HashMap - Simple Handson1

Path: JAVA/Collection, Generics and Stream API/HashMap - Simple Handson1/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
String[] str=new String[n+1];
int[] arr=new int[n];
int i;
sc.nextLine();
for(i=0;i<n;i++)
{
    System.out.println("Enter the country code");
    arr[i]=sc.nextInt();
    sc.nextLine();
    System.out.println("Enter the country name");
    str[i]=sc.nextLine();
}
HashMap<Integer,String> map=new HashMap<Integer,String>();
for(i=0;i<n;i++)
{
    map.put(arr[i],str[i]);
}
System.out.println(map);
}

}


6.  Member Manipulation

Path: JAVA/Collection, Generics and Stream API/Member Manipulation/Library.java
import java.util.*;
public class Library
{
    private List<Member> memberList=new ArrayList<Member>();
    public void setMemberList(List<Member> l)
    {
        memberList=l;
    }
    public List<Member> getMemberList()
    {
        return memberList;
    }
    public void addMember(Member memberObj)
    {
        memberList.add(memberObj);
    }
    public List<Member> viewAllMembers()
    {
        return memberList;
    }
    public List<Member>viewMembersByAddress(String address)
    {
        List<Member> l=new ArrayList<Member>();
        for(Member obj:memberList)
        {
            if(obj.getAddress().equalsIgnoreCase(address))
            {
                l.add(obj);
            }
        }
        return l;
    }
}


Path: JAVA/Collection, Generics and Stream API/Member Manipulation/Main.java
import java.util.*;
public class Main 
{
    public static void main(String args[])
    {
        Library o=new Library();
        Scanner sc=new Scanner(System.in);
        int i=0;
        while(i==0)
        {
            System.out.println("1.Add Member\n2.View All Members\n3.Search Member by address\n4.Exit");
            System.out.println("Enter your choice");
            int n=Integer.parseInt(sc.nextLine());
            if(n==1)
            {
                Member obj=new Member();
                System.out.println("Enter the id:");
                obj.setMemberId(Integer.parseInt(sc.nextLine()));
                System.out.println("Enter the name:");
                obj.setMemberName(sc.nextLine());
                System.out.println("Enter the address:");
                o.addMember(obj);
            }
            if(n==2)
            {
                List<Member>obj1=o.viewAllMembers();
                for(Member o1:obj1)
                {
                    System.out.println("Id:"+o1.getMemberId());
                    System.out.println("Name:"+o1.getMemberName());
                    System.out.println("Address:"+o1.getAddress());
                }
            }
            if(n==3)
            {
                System.out.println("Emter the address:");
                List<Member>
                obj=o.viewMembersByAddress(sc.nextLine());
                for(Member obj1:obj)
                {
                    System.out.println("Id:"+obj1.getMemberId());
                    System.out.println("Name:"+obj1.getMemberName());
                    System.out.println("Address:"+obj1.getAddress());
                }
            }
            if(n==4)
            {
                System.exit(0);
            }
        }
    }

}


Path: JAVA/Collection, Generics and Stream API/Member Manipulation/Member.java
public class Member
{
    private int memberId;
    private String memberName;
    private String address;
    
    public Member()
    {
        
    }
    public Member(int id,String name,String add)
    {
        memberId=id;
        memberName=name;
        address=add;
    }
    public void setMemberId(int id)
    {
        memberId=id;
    }
     public void setMemberName(String name)
    {
        memberName=name;
    }
     public void setAddress(String add)
    {
        address=add;
    }
    public int getMemberId()
    {
        return memberId;
    }
    public String getMemberName()
    {
        return memberName;
    }
    public String getAddress()
    {
        return address;
    }
    
}



7. Number of New Words

Path: JAVA/Collection, Generics and Stream API/Number of New Words/UniqueWords.java
//import the necessary packages if needed
import java.util.*;   
@SuppressWarnings("unchecked")//Do not delete this line
public class UniqueWords
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Student's Article");
        String s=sc.nextLine();
        s=s.toLowerCase();
        int len=s.length();
        StringBuilder sb=new StringBuilder(s);
        for(int a=0;a<sb.length();a++)
        {
            if(!(Character.isLetter(sb.charAt(a))))
            {
                if(sb.charAt(a)!=' ')
                {
                    if(sb.charAt(a)!='\'')
                    sb.deleteCharAt(a);
                }
            }
        }
        s=sb.toString();
        String str[]=s.split(" ");
        Set<String> s1=new HashSet<String>
        (Arrays.asList(str));
        List<String> s2=new ArrayList<String>(s1);
        System.out.println("Number of words "+str.length);
        System.out.println("Number of unique words "+s1.size());
        Collections.sort(s2);
        int a=1;
        System.out.println("The words are");
        for(String stemp:s2)
        {
            System.out.println((a++)+". "+stemp);
        }
    }
}


8. PhoneBook Manipulation

Path: JAVA/Collection, Generics and Stream API/PhoneBook Manipulation/Contact.java
public class Contact {

private String firstName;
private String lastName;
private long  phoneNumber;
private String emailId;
public Contact(){}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public Contact(String firstName, String lastName, long phoneNumber,
String emailId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.emailId = emailId;
}
}


Path: JAVA/Collection, Generics and Stream API/PhoneBook Manipulation/Main.java
import java.util.*;
public class Main 
{
            public static void main(String args[])
        {
            Scanner sc=new Scanner(System.in);
            int i=0;
            PhoneBook objmain=new PhoneBook();
            while(i==0)
            {
                System.out.println("Menu\n1.Add Contact\n2.Display all contacts\n3.Search contact by phone\n4.Remove contact\n5.Exit");
                System.out.println("Enter your choice: ");
                int n=Integer.parseInt(sc.nextLine());
                if(n==1)
                {
                    Contact obj=new Contact();
                    System.out.println("Add a contact: ");
                    System.out.println("Enter the First Name: ");
                    obj.setFirstName(sc.nextLine());
                    System.out.println("Enter the Last Name: ");
                    obj.setLastName(sc.nextLine());
                    System.out.println("Enter the Phone No. : ");
                    obj.setPhoneNumber(Long.parseLong(sc.nextLine()));
                    System.out.println("Enter the Email: ");
                    obj.setEmailId(sc.nextLine());
                    objmain.addContact(obj);
                }
                if(n==2)
                {
                    System.out.println("The contacts in the List are:");
                    List<Contact>obj=objmain.viewAllContacts();
                    for(Contact temp:obj)
                    {
                        System.out.println("First Name:"+temp.getFirstName());
                        System.out.println("Last Name:"+temp.getLastName());
                        System.out.println("Phone No.:"+temp.getPhoneNumber());
                        System.out.println("Email:"+temp.getEmailId());
                    }
                }
                if(n==3)
                {
                    System.out.println("Enter the Phone number to search contact:");
                    Long n1=Long.parseLong(sc.nextLine());
                    Contact obj1=objmain.viewContactGivenPhone(n1);
                    System.out.println("The contact is:");
                    System.out.println("First Name:"+obj1.getFirstName());
                    System.out.println("Last Name:"+obj1.getLastName());
                    System.out.println("Phone No.:"+obj1.getPhoneNumber());
                    System.out.println("Email:"+obj1.getEmailId());
                }
                if(n==4)
                {
                    System.out.println("Enter the Phone number to remove:");
                    Long n1=Long.parseLong(sc.nextLine());
                    System.out.println("Do you want to remove the contact(Y/N):");
                    char ch=sc.nextLine().charAt(0);
                    if(ch=='Y')
                    {
                        boolean f1=objmain.removeContact(n1);
                        if(f1)
                        System.out.println("The contact is successfully deleted");
                        else
                        System.out.println("Contact is not found");
                    }
                    if(ch=='N')
                    {
                        System.out.println("ok");
                    }
                    
                }
                if(n==5)
                {
                    System.exit(0);
                }
            }
        }
    }


Path: JAVA/Collection, Generics and Stream API/PhoneBook Manipulation/PhoneBook.java
import java.util.*;
public class PhoneBook
{
    private List<Contact> phoneBook=new ArrayList<Contact>();
    public void setPhoneBook(List<Contact>obj)
    {
        phoneBook=obj;
    }
    public List<Contact>getPhoneBook()
    {
        return phoneBook;
    }
    public void addContact(Contact contactObj)
    {
        phoneBook.add(contactObj);
    }
    public List<Contact> viewAllContacts()
    {
        return phoneBook;
    }
    public Contact viewContactGivenPhone(long phoneNumber)
    {
        Contact obj=new Contact();
        for(Contact obj1:phoneBook)
        {
            if(obj1.getPhoneNumber()==phoneNumber)
            {
                obj=obj1;
            }
        }
        return obj;
    }
    public boolean removeContact(long phoneNumber)
    {
        boolean f=false;
        for(Contact obj:phoneBook)
        {
            if(obj.getPhoneNumber()==phoneNumber)
            {
                f=true;
                phoneBook.remove(obj);
                break;
            }
        }
        return f;
    }
}


9. Remove Duplicated from the Names

Path: JAVA/Collection, Generics and Stream API/Remove Duplicates from the Names/TestMain.java
import java.util.*;
public class TestMain{
    public static void main(String[] args)
    {
        //implement the required business logic here
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of Students:");
        int num=sc.nextInt();
        sc.nextLine();
        TreeSet<String> ts=new TreeSet<String>();
        for(int i=0;i<num;i++){
            ts.add(sc.nextLine());
        }
        Object[] str=ts.toArray();
        for(Object i:str){
            System.out.println(i);
        }
    }
}


10. Retrieve Student Info

Path: JAVA/Collection, Generics and Stream API/Retrieve Student Info/Retrieve Student Info/Student.java
//Do not modify the class
public class Student {
private int studId;
private String studName;
private String schoolName;
public Student()
{
}
public int getStudId() {
return studId;
}
public void setStudId(int studId) {
this.studId = studId;
}
public String getStudName() {
return studName;
}
public void setStudName(String studName) {
this.studName = studName;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
}


Path: JAVA/Collection, Generics and Stream API/Retrieve Student Info/Retrieve Student Info/TestMain.java
import java.util.*;
public class TestMain{
    public static ArrayList<String> retrieveStudentInfo(ArrayList<Student> al,String schoolName) {
       // implement the required business logic
    ArrayList<String> str=new ArrayList<String>();
    for(int i=0;i<al.size();i++){
    Student s=al.get(i);
    if(s.getSchoolName().equalsIgnoreCase(schoolName))
    {
        str.add(s.getStudName());
    }
    }
    return str;
    }
    
    public static void main(String[] args)
    {
        Student s1=new Student();
        s1.setStudId(1);
        s1.setStudName("John");
        s1.setSchoolName("ZEE");
        Student s2=new Student();
        s2.setStudId(2);
        s2.setStudName("Tom");
        s2.setSchoolName("ZEE");
        Student s3=new Student();
        s3.setStudId(3);
        s3.setStudName("Peter");
        s3.setSchoolName("BEE");
        Student s4=new Student();
        s4.setStudId(4);
        s4.setStudName("Rose");
        s4.setSchoolName("OX-FO");
        Student s5=new Student();
        s5.setStudId(5);
        s5.setStudName("Alice");
        s5.setSchoolName("ZEE");
       
       //invoke the retrieveStudentInfo method and display the result
        ArrayList<Student>studentInfo=new ArrayList<Student>();
        studentInfo.add(s1);
        studentInfo.add(s2);
        studentInfo.add(s3);
        studentInfo.add(s4);
        studentInfo.add(s5);
        ArrayList<String>retrieveStudent=retrieveStudentInfo(studentInfo,"BEE");
        for(int i=0;i<retrieveStudent.size();i++)
        {
            System.out.println(retrieveStudent.get(i));
        }
    }
}


11. TreeMap - Simple Handson1
Path :JAVA/Collection, Generics and Stream API/TreeMap - Simple Handson1/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
TreeMap<Integer,String> map=new TreeMap<Integer,String>();
for(int i=0;i<n;i++)
{
    System.out.println("Enter the country code");
    int ar=sc.nextInt();
    System.out.println("Enter the country name");
    sc.nextLine();
    String str=sc.nextLine();
    map.put(ar,str);
    }
    for(Map.Entry m:map.entrySet())
    {
        System.out.println(m.getKey()+" : "+m.getValue());
    }

}
}


12. TreeSet - Simple Handson1

Path: JAVA/Collection, Generics and Stream API/TreeSet - Simple Handson1/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
TreeSet<String> veg=new TreeSet<String>();
int i;
String[] str=new String[n+1];
for(i=0;i<=n;i++)
{
    str[i]=sc.nextLine();
}
for(i=1;i<=n;i++)
veg.add(str[i]);
System.out.println(veg);
}

}


13. TreeSet - Simple Handson2

Path: JAVA/Collection, Generics and Stream API/TreeSet - Simple Handson2/UserInterface.java
import java.util.*;

public class UserInterface {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
// Fill your code here
System.out.println("Enter number of elements to add");
int n=sc.nextInt();
String[] str=new String[n+1];
int i;
for(i=0;i<n+1;i++)
{
    str[i]=sc.nextLine();
}
TreeSet<String> veg=new TreeSet<String>();
for(i=1;i<=n;i++)
{
    veg.add(str[i]);
}
Iterator<String> it=veg.iterator();
while(it.hasNext())
{
    System.out.println(it.next());
}
}
}



If you have any queries, please feel free to ask on the comment section.

If you want MCQs and Hands-On solutions for any courses, Please feel free to ask on the comment section too.

Please share and support our page!