[go: up one dir, main page]

0% found this document useful (0 votes)
306 views9 pages

Collections Assignment

The document discusses 8 questions related to Java collections and data structures. 1. Write code to add floating point numbers to a List, iterate through it with an iterator, and calculate the sum. 2. Write a method to take a string and return the number of unique characters. 3. Write a method to take a string and print the number of occurrences of each character. 4. Write a program to sort a HashMap by value. 5. Write a program to sort Employee objects based on highest salary using a Comparator. 6. Write a program to sort Student objects based on score then name if scores are the same. 7. Print the elements of an

Uploaded by

Divya Chhabra
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)
306 views9 pages

Collections Assignment

The document discusses 8 questions related to Java collections and data structures. 1. Write code to add floating point numbers to a List, iterate through it with an iterator, and calculate the sum. 2. Write a method to take a string and return the number of unique characters. 3. Write a method to take a string and print the number of occurrences of each character. 4. Write a program to sort a HashMap by value. 5. Write a program to sort Employee objects based on highest salary using a Comparator. 6. Write a program to sort Student objects based on score then name if scores are the same. 7. Print the elements of an

Uploaded by

Divya Chhabra
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/ 9

SESSION -11

COLLECTIONS IN JAVA

1. Write Java code to define List . Insert 5 floating point numbers in List, and
using an iterator, find the sum of the numbers in List.

import java.util.*;
import java.util.Iterator;

public class Ques1


{
public static void main(String[] a)
{
List<Float> listStrings = new ArrayList<Float>();
listStrings.add(3.5f);
listStrings.add(6.8f);
listStrings.add(4.7f);
listStrings.add(8.9f);
listStrings.add(6.7f);
System.out.println(listStrings);

Iterator<Float> it = listStrings.iterator();
float sum = 0;
while (it.hasNext()) {

float value = it.next();


sum = sum + value;

}
System.out.println("Sum: "+sum);

}
}
2. Write a method that takes a string and returns the number of unique
characters in the string.
public class Ques2
{
public static void main(String[] args)
{
String a = "tothenew";
System.out.println("String: "+a);
allunique(a);
}

private static void allunique(String a)


{
int[] count = new int[256];
for (int i = 0; i < a.length(); i++)
{
char ch = a.charAt(i);
count[ch]++;
}
for (int i = 0; i < a.length(); i++)
{
char chh = a.charAt(i);

if (count[chh] == 1) {
System.out.println(" unique character => " + a.charAt(i));

}
}

3. Write a method that takes a string and print the number of occurrence of
each character characters in the string.
import java.util.Scanner;
public class Ques3
{
static void Occuring(String str)
{
int count[] = new int[256];
int len = str.length();

for (int i = 0; i < len; i++)


count[str.charAt(i)]++;

char ch[] = new char[str.length()];


for (int i = 0; i < len; i++)
{
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++)
{

if (str.charAt(i) == ch[j])
find++;
}

if (find == 1)
System.out.println(str.charAt(i) + " occurs " + count[str.charAt(i)] +" times");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String str = sc.nextLine();
Occuring(str);
}
}
4. Write a program to sort HashMap by value.

import java.util.*;
import java.lang.*;

public class Ques4 {

public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)


{
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());

Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {


public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}

public static void main(String[] args)


{

HashMap<String, Integer> hm = new HashMap<String, Integer>();

hm.put("Divya", 4032);
hm.put("Seeta", 4085);
hm.put("Geeta", 4091);
hm.put("Suresh", 4095);
hm.put("Rashmi", 4079);
hm.put("Nitin", 4080);
Map<String, Integer> hm1 = sortByValue(hm);

for (Map.Entry<String, Integer> en : hm1.entrySet()) {


System.out.println("Key = " + en.getKey() +
", Value = " + en.getValue());
}
}
}
5. Write a program to sort Employee objects based on highest salary using
Comparator. Employee class{ Double Age; Double Salary; String Name
6. Write a program to sort the Student objects based on Score , if the score
are same then sort on First Name . Class Student{ String Name; Double
Score; Double Age
7. Print the elements of an array in the decreasing frequency if 2 numbers
have same frequency then print the one which came first.

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

public class Ques7


{
private static void sortByFrequency(int[] inputArray)
{
Map<Integer, Integer> elementCountMap = new LinkedHashMap<>();

for (int i = 0; i < inputArray.length; i++)


{
if (elementCountMap.containsKey(inputArray[i]))
{

elementCountMap.put(inputArray[i], elementCountMap.get(inputArray[i])+1);
}
else
{

elementCountMap.put(inputArray[i], 1);
}
}

ArrayList<Integer> sortedElements = new ArrayList<>();

elementCountMap.entrySet()
.stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.forEach(entry -> {
for(int i = 1; i <= entry.getValue(); i++)
sortedElements.add(entry.getKey());
});

System.out.println("Input Array :"+Arrays.toString(inputArray));

System.out.println("Sorted Array Elements In Descending Order Of their


Frequency :");

System.out.println(sortedElements);
}

public static void main(String[] args)


{
sortByFrequency(new int[] {7, 1, 3, 4, 7, 1, 7, 1, 4, 5, 1, 9, 3});
}
8. Design a Data Structure SpecialStack that supports all the stack operations
like push(), pop(), isEmpty(), isFull() and an additional operation getMin()
which should return minimum element from the SpecialStack. (Expected
complexity ​ O(1))

You might also like