0% found this document useful (0 votes)
41 views2 pages

PBLJ Lab Worksheet 2.1

1) The document is a student worksheet that implements a hash table in Java to store cards with symbols and values. 2) A TreeMap is used to map symbols to lists of values, adding values to lists if the symbol exists or creating a new list if not. 3) The code prints the distinct symbols, cards for each symbol with values, number of cards, and sum of values before resetting for the next symbol.

Uploaded by

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

PBLJ Lab Worksheet 2.1

1) The document is a student worksheet that implements a hash table in Java to store cards with symbols and values. 2) A TreeMap is used to map symbols to lists of values, adding values to lists if the symbol exists or creating a new list if not. 3) The code prints the distinct symbols, cards for each symbol with values, number of cards, and sum of values before resetting for the next symbol.

Uploaded by

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

Worksheet 2.

Name: Milan Kamboj UID: 18BCS6701


Branch: BE – CSE Section/Group: 18_IS_10 A
{{{ {{{
Semester: 6 th
Date of Performance: 19th March 2021
Subject Name: PBLJ Lab Subject Code: CSP-358

Aim: Implement this using Hash Table.

Code (Java):
package pkg;
import java.util.*;
public class cls {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
List<Integer> valueList = new ArrayList<Integer>();
TreeMap<String, List<Integer>> mapObj = new TreeMap<String, List<Integer>>();
int totalCards, index, value, sum = 0, count = 0;

System.out.println("ENTER NUMBER OF CARDS : ");


totalCards = input.nextInt();
String symbol;

for(index = 1; index <= totalCards; index++){


System.out.println("ENTER CARD" + " " + index);
symbol = input.next();
value = input.nextInt();

if(mapObj.containsKey(symbol)){
valueList = mapObj.get(symbol);
valueList.add(value);
}else{
valueList = new ArrayList<Integer>();
valueList.add(value);
mapObj.put(symbol, valueList);
}
}

System.out.println("DISTINCT SYMBOLS ARE :");


for(Map.Entry getData : mapObj.entrySet()){
System.out.print(getData.getKey() + " ");
18BCS6701 18_IS_10 A
}
System.out.println();

for(Map.Entry getData : mapObj.entrySet()){


System.out.println("CARDS IN " + getData.getKey() + " SYMBOL :");
ArrayList<Integer> temp = (ArrayList<Integer>) getData.getValue();
Iterator itr= temp.iterator();
while(itr.hasNext())
{
count++;
int val = (int) itr.next();
System.out.print(getData.getKey());
System.out.println(" " + val);
sum += val;
}
System.out.println("NUMBER OF CARDS : " + count);
System.out.println("SUM OF NUMBERS : " + sum);
sum = 0;
}
}
}

Output: Screenshot

[Input] [Output]

18BCS6701 18_IS_10 A

You might also like