0% found this document useful (0 votes)
120 views6 pages

Hands On - Collection, Generics and Stream API

The document describes code samples for using different Java collection classes - ArrayList, TreeSet, HashMap. It includes code to add elements to and retrieve elements from each collection. It also includes a longer example of a Library class that uses an ArrayList to store Book objects, and allows adding, retrieving, searching and counting books.

Uploaded by

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

Hands On - Collection, Generics and Stream API

The document describes code samples for using different Java collection classes - ArrayList, TreeSet, HashMap. It includes code to add elements to and retrieve elements from each collection. It also includes a longer example of a Library class that uses an ArrayList to store Book objects, and allows adding, retrieving, searching and counting books.

Uploaded by

voreka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

ArrayList - Simple Handson1

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 no_of_ele = sc.nextInt();
String[] str =new String[no_of_ele+1];
int i;
for(i =0; i<=no_of_ele;i++){
str[i]=sc.nextLine();
}
ArrayList<String> fruit= new ArrayList<String>();
for(i=1;i<=no_of_ele;i++){
fruit.add(str[i]);
}
System.out.println(fruit);
}

-----------------------------------------------------------------------------------
-----------------------
TreeSet - Simple Handson1

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 no_of_ele = sc.nextInt();
TreeSet<String> veg = new TreeSet<String>();
String str[] = new String[no_of_ele +1];
for(int i=0;i<=no_of_ele;i++){
str[i] = sc.nextLine();
}
for(int i=1;i<=no_of_ele;i++){
veg.add(str[i]);
}
System.out.println(veg);
}

-----------------------------------------------------------------------------------
--------------------------
HashMap - Simple Handson1

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 no_of_element = sc.nextInt();
String[] str = new String[no_of_element+1];
int[] arr = new int[no_of_element];
int i;
sc.nextLine();
for(i=0;i<no_of_element;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> details = new HashMap <Integer,String>();
for(i=0;i<no_of_element;i++){
details.put(arr[i],str[i]);

}
System.out.println(details);
}

-----------------------------------------------------------------------------------
-------------------------
Book Manipulation

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;
}
}

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());
}
}
}
else if (n==4)
{
System.out.println("Enter the book name");
String b = sc.nextLine();
int a = obj.countnoofbook(b);
System.out.println(a);
}
else{
return;
}

}
}
}

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;
}
}

-----------------------------------------------------------------------------------
-------------------------
Number of New Words

import java.util.*;
//import the necessary packages if needed

@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);
}
}
}

-----------------------------------------------------------------------------------
------------------------------------------
Count of Each Words

import java.util.*;
//import the necessary packages if needed

@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);
}

}
}
}

You might also like