Showing posts with label Collection. Show all posts
Showing posts with label Collection. Show all posts

Sunday, December 8, 2019

How do you sort collection of StringBuffer object in java?

Problem statement: Given the collection of StringBuffer objects.
StringBuffer sb1 = new StringBuffer("Z");
StringBuffer sb2 = new StringBuffer("A");
StringBuffer sb3 = new StringBuffer("P");

You have to sort it either way, meaning natural order or custom order.

Answer:
// ASCENDING ORDER:
  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3. public class Test {
  4. public static void main(String[] args) {
  5. SortStringBufferObject s = new SortStringBufferObject();
  6. TreeSet<StringBuffer> t = new TreeSet<>(s);
  7. t.add(new StringBuffer("Z"));
  8. t.add(new StringBuffer("A"));
  9. t.add(new StringBuffer("P"));
  10. System.out.println(t);
  11. }
  12. }
  13. class SortStringBufferObject implements Comparator<StringBuffer> {
  14. public int compare(StringBuffer sb1, StringBuffer sb2) {
  15. return sb1.toString().compareTo(sb2.toString());
  16. }
  17. }
Output:
[A, P, Z] 

// DESCENDING ORDER:
  1. import java.util.Comparator;
  2. import java.util.TreeSet;
  3. public class Test {
  4. public static void main(String[] args) {
  5. SortStringBufferObject s = new SortStringBufferObject();
  6. TreeSet<StringBuffer> t = new TreeSet<>(s);
  7. t.add(new StringBuffer("Z"));
  8. t.add(new StringBuffer("A"));
  9. t.add(new StringBuffer("P"));
  10. System.out.println(t);
  11. }
  12. }
  13. class SortStringBufferObject implements Comparator<StringBuffer> {
  14. public int compare(StringBuffer sb1, StringBuffer sb2) {
  15. return sb2.toString().compareTo(sb1.toString());
  16. }
  17. }
Output:
[Z, P, A]
Reference

What will be the output when you add String object in HashSet?

Problem statement: Given code is 
  1. public class Test {
  2. public static void main(String[] args) {
  3. HashSet<String> s = new HashSet<>();
  4. s.add("A");
  5. s.add("B");
  6. s.add("C");
  7. s.add("A");
  8.   System.out.println("Elements: \n" + s);
  9.   System.out.println("Size: " + s.size());
  10.   System.out.println("Objects: " + s);
  11. }
  12. }
Answer:
Elements: 
[A, B, C]
Size: 3
Objects: [A, B, C]

What will be the output when we add elements in collection of ArrayList in java?

Problem statement: In given java code, can you please let me know, what will be the output.
  1. import java.util.ArrayList;
  2. class A {}
  3. class B {}
  4. class C {}
  5. public class Test {
  6. public static void main(String[] args) {
  7. ArrayList list = new ArrayList();
  8. list.add(new A());
  9. list.add(new B());
  10. list.add(new C());
  11.   list.add(new A());
  12. System.out.println(list);
  13.   System.out.println("Size: " + list.size());
  14. }
  15. }
Answer:
[com.practice.core.A@15db9742, com.practice.core.B@6d06d69c, com.practice.core.C@7852e922, com.practice.core.A@4e25154f]
Size: 4

Note: fully_qualified_class_name@hashcode_value

Monday, August 26, 2019

How do you sort the keys in HashMap?

Problem statement:
Write the java code to sort the keys in HashMap.

  1. import java.util.HashMap;
  2. import java.util.Set;
  3. import java.util.TreeSet;
  4. public class Test {
  5. public static void main(String[] args) {
  6. HashMap<String, String> m = new HashMap<String, String>();
  7. m.put("key45", "aa");
  8. m.put("key12", "zz");
  9. m.put("key39", "cc");
  10. m.put("key27", "bb");
  11. Set<String> s = m.keySet();
  12. s = new TreeSet<String>(s);
  13. System.out.println(s);
  14. }
  15. }
Output:
[key12, key27, key39, key45]

How do you sort the values in HashMap?

Problem statement:
Write the java code to sort the values in HashMap.
  1. import java.util.Collection;
  2. import java.util.HashMap;
  3. import java.util.TreeSet;
  4. public class Test {
  5. public static void main(String[] args) {
  6. HashMap<String, String> m = new HashMap<String, String>();
  7. m.put("key45", "aa");
  8. m.put("key12", "zz");
  9. m.put("key39", "cc");
  10. m.put("key27", "bb");
  11. Collection<String> c = m.values();
  12. c = new TreeSet<String>(c);
  13. System.out.println(c);
  14. }
  15. }
Output:
[aa, bb, cc, zz]

Which of the following option will sort the keys in HashMap ?

Problem statement:
Given java code as follows, can you find out the correct option to sort the keys in HashMap?
  1. import java.util.HashMap;
  2. import java.util.Set;
  3. import java.util.TreeSet;
  4. public class Test {
  5. public static void main(String[] args) {
  6. HashMap<String, String> m = new HashMap<String, String>();
  7. m.put("key45", "aa");
  8. m.put("key12", "bb");
  9. m.put("key39", "cc");
  10. Set<String> s = m.keySet();
  11.   // code goes here
  12. s = new TreeSet(s);
  13.  }
  14. }
  • Arrays.sort(s);
  • s = new TreeSet(s);
  • Collections.sort(s);
  • s = new SortedSet(s);
Correct option: 
s = new TreeSet(s);
Explanation:
Arrays.sort(s) // sorts for static array 
Collection.sort(s) // sort the List type of object not Set type
s =new SortedSet(s); // SortedSet is an interface

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...