Question

In: Computer Science

how to sorted the list from another class!!! example i have a class public meso public...

how to sorted the list from another class!!!
example i have a class
public meso
public meso(string id)
public hashmap<string, integer> neou{
this class print out
b
d
e
c
a
now create another class
public mesono
public mesono(hashmap<string, integer> nei)
//want to sorted meso class print list to this class!!
// print out
a
b
c
d
e

}
( -------Java------)
  

Solutions

Expert Solution

import java.util.*;
import java.util.Map.Entry;
public class meso {
        static String word;
        //Parameterized constructor
        public meso(String id) {
                word=id;
        }
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                meso m = new meso("bdeca");
                //Calling neou function to print characters in the string and returns hashmap
                HashMap<Character, Integer> dict=neou(word);
                //Calling another class
                mesono m2 = new mesono(dict); 
                //Calling sortbykey function which is in mesono class
                m2.sortbykey();
        }
        private static HashMap<Character, Integer> neou(String word2) {
                // TODO Auto-generated method stub
                HashMap<Character, Integer> hash =new HashMap<Character, Integer>();
                System.out.println("Before sorting:");
                //Reading each character in the string
                for(int i=0;i<word2.length();i++) {
                        System.out.println(word2.charAt(i));
                        hash.put(word2.charAt(i), i);
                }
                return hash;
        }

}
class mesono{
        static HashMap<Character, Integer> map;
        public mesono(HashMap<Character, Integer> dict) {
                map=dict;
        }
        //Sorting values in the hashmap using treemap
        public static void sortbykey() 
    { 
        // TreeMap to store values of HashMap 
        TreeMap<Character, Integer> sorted = new TreeMap<>(); 
  
        // Copy all data from hashMap into TreeMap 
        sorted.putAll(map); 
        System.out.println("After sorting:");
        // Display the TreeMap which is naturally sorted 
        for (Entry<Character, Integer> entry : sorted.entrySet())  
            System.out.println(entry.getKey());         
    } 
}

Sample input and output:

Before sorting:
b
d
e
c
a
After sorting:
a
b
c
d
e
//Please leave comment if you have any doubts. THANK YOU.


Related Solutions

Give one example each of the following: a. Macro level public policy b. Meso level public...
Give one example each of the following: a. Macro level public policy b. Meso level public policy c. Micro level public policy ii) In 100 words describe what each policy is about and your justification for allocating the policy
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.....
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.. Assuming that we compare x with the first element A0, if x is equal to A0 , we found x and we return 1; otherwise we jump 4 elements (to A4). If x is equal to A4, we return 5 (index + 1), otherwise if it is less we back up 1 space to A3; if x is equal to A3 we return 4,...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements...
I have three classes: class VolleyPlayer, class VolleyTeam and class TestDriver 1.class: public class VolleyPlayer implements Comparable<VolleyPlayer> { // instance variables - private String name; private int height; private boolean female; public VolleyPlayer(String name, int height, boolean female) { this.name = name; this.height = height; this.female = female; } public String toString() {    return (female ?"Female":"Male")+": "+name+" ("+height+" cm)"; } public boolean isFemale() {    return female; } public boolean isMale() {    return !female; } public int getHeight()...
Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Hi can someone fully explain how the output was reached? thanks public class Example { public...
Hi can someone fully explain how the output was reached? thanks public class Example { public static void main(String [] args) { int a = 0, b = 4, i, j;    for (i = 1; i<=3; i++) { for(j = 1; j < 3; ++j) { a = a + j; b = b + a; System.out.println (a + " " + b); } } System.out.println ("ALL DONE"); } }    output 1 5 3 8 4 12 6...
I have written this paper for my public speaking class,and I would appreciate it if someone...
I have written this paper for my public speaking class,and I would appreciate it if someone revised it for me to make sure my punctuations are correct as well as everything else.Thank you, Public Speaking…Dealing with It Often when a person thinks of public speaking the thoughts a person may have will vary from the next, because while one person may have experience the other person may not. Being able to communicate effectively requires that a person acknowledges the frame...
Lets say that I were to give you a sorted list of P elements that was...
Lets say that I were to give you a sorted list of P elements that was followed by randomly ordered elements. Please explain how you would sort the entire, whole list? Please give a detailed reasoning and provide a good explanation (In language C++) Please type answer if you can
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public...
Hi I have problem with run this JAVA file import java.io.*; public class DataPresenter { public static void main (String args []) { System.out.println("File:../SmallAreaIncomePovertyEstData.text"); System.out.println("Id" + "\t" + "Population" + "\t" + "ChildPop" + "\t" + "CPovPop" + "\t" + "CPovPop%"); }// read the data try (FileReader fr = new FileReader("File: C:\\605.201/SmallAreaIncomePovertyEstData.text")) { int c; while (( c = fr.read())!= -1){ System.out.print((char) c); } } catch(IOException e) { System.out.println("I/O Error" + e); }    } Please help to fix
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT