In: Computer Science
JAVA
* create Q3 class to implement comparator for a TreeSet. The
purpose of
* the comparator is to compare the
alphabetic order of integers. With Q3,
* the order of Integer elements
will be sort according to the order of
* digit Unicode.
* For example, the value of
{11,3,31,2} will return {11,2,3,31}
* NOTE: You don't need to compare
each digit one by one. Just compare them
System.out.println("Q3 = [15 marks]
=================================");
Set set = new TreeSet<>(new
Q3());
Integer[] arr3 = {11,3,31,2};
set.addAll(Arrays.asList(arr3));
for (Integer element: set)
System.out.print(element+"\t");
System.out.println();
implement stats method in Q4 class to perform statistics for
the
* occurrence of each character in
the provided sentence string.
* Save the results into a hashmap,
with key = character and value = occurrence
* For example, "no news is good
news" should return a hashmap with value of
* { =4, s=3, d=1, e=2, w=2, g=1,
i=1, n=3, o=3}
* NOTE: the first key indicates
space.
String sentence = "no news is good news";
System.out.println("Q5 = [15 marks]
=================================");
System.out.println(Q5.stats(sentence));
import
java.util.Comparator;
import
java.util.TreeSet;
class
The_Comparator
implements
Comparator<String> {
public
int
compare(String str1, String str2)
{ String
first_Str;
String
second_Str;
first_Str
= str1;
second_Str
= str2;
return
second_Str.compareTo(first_Str);
}
}
public
class
Tree_Set_Demo {
public
static
void
main(String[]
args)
{
TreeSet<String>
tree_set =
new
TreeSet<String>(
new
The_Comparator());
tree_set.add("G");
tree_set.add(
"E"
);
tree_set.add(
"E"
);
tree_set.add(
"K"
);
tree_set.add(
"S"
);
tree_set.add(
"4"
);
System.out.println(
"Set
before using the comparator: "
+
tree_set);
System.out.println(
"The elements sorted in
descending"
+
"order:"
);
for
(String element : tree_set)
System.out.print(element
+
" "
);
}
}
output:
Set before using the comparator: [S, K, G, E, 4] The elements sorted in descendingorder: S K G E 4
=================================
import java.io.*;
import java.util.*;
class OccurenceOfCharInString {
static void characterCount(String inputString)
{
// Creating a HashMap containing
char
// as a key and occurrences as a
value
HashMap<Character, Integer>
charCountMap
= new
HashMap<Character, Integer>();
// Converting given string
to char array
char[] strArray =
inputString.toCharArray();
// checking each char of
strArray
for (char c : strArray) {
if
(charCountMap.containsKey(c)) {
// If char is present in charCountMap,
// incrementing it's count by 1
charCountMap.put(c, charCountMap.get(c) +
1);
}
else
{
// If char is not present in charCountMap,
// putting this char to charCountMap with 1 as
it's value
charCountMap.put(c, 1);
}
}
// Printing the
charCountMap
for (Map.Entry entry :
charCountMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
// Driver Code
public static void main(String[] args)
{
String str = "Abhi";
characterCount(str);
}
}
output:
A 1 b 1 h 1 i 1