Reputation:
Passing an Integer arrayList. I get how to find the max and add that to a separate arrayList. But how would I be able to continue find the max. Not trying to use a selection sort algorithm.
Upvotes: 0
Views: 233
Reputation: 13859
If you can not use a HashMap
, you can try this.
Comments in the code.
Code
mport java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author Sedrick(sedj601)
*/
public class Main
{
public static void main(String[] args)
{
//Fake file data
String dataToRead =
"Lorem Ipsum is simply dummy text of the
printing and typesetting industry Lorem
Ipsum has been the industrys standard dummy
text ever since thes when an unknown printer
took a galley of type and scrambled it to make a
type specimen book It has survived not only five
centuries but also the leap into electronic typesetting
remaining essentially unchanged It was popularised in the
a with the release of Letraset sheets containing
Lorem Ipsum passages and more recently with desktop
publishing software like Aldus PageMaker including
versions of Lorem Ipsum";
List<String> data = Arrays.asList(dataToRead.toUpperCase().split("\n"));//Capatilize the data as you read it.
List<String> keyCapitalLetters = new ArrayList(Arrays.asList("ABCDEFGHIJKLMNOPQRSTUVWXYZ ".split("")));//Use this List like you would use the key part of a HashMap.
//data.forEach(System.out::println);
List<Integer> valuescounters = new ArrayList();//Use this list like you would use the value part of a HashMap
//Set all the counters values to zero.
for(int i = 0; i < 27; i++)
{
valuescounters.add(0);
}
//Process the fake data line by line
for(String line : data)
{
System.out.println("Line: " + line);
for(int i = 0; i < line.length(); i++)//Read each character of each line.
{
int index = keyCapitalLetters.indexOf(String.valueOf(line.charAt(i)));//find the index in the key List of the characters that is being read.
valuescounters.set(index, valuescounters.get(index) + 1);//Increment the counter value in the value list.
System.out.println("Char: " + line.charAt(i) + "\tIndex: " + index);
}
}
//Remove the space character.
keyCapitalLetters.remove(" ");
valuescounters.remove(26);
//Print the output of the key and value list.
System.out.println("\nCount:");
for(int i = 0; i < keyCapitalLetters.size(); i++)
{
System.out.println(keyCapitalLetters.get(i) + ": " + valuescounters.get(i));
}
doSelectionSort(keyCapitalLetters, valuescounters);//Sort.
//Print After sort.
System.out.println("\nSorted:");
for(int i = 0; i < keyCapitalLetters.size(); i++)
{
System.out.println(keyCapitalLetters.get(i) + ": " + valuescounters.get(i));
}
//Print Five Highest.
System.out.println("\nHighest Five");
List<String> keysFiveHighest = keyCapitalLetters.subList(keyCapitalLetters.size() - 5, keyCapitalLetters.size());
List<Integer> valuesFiveHighest = valuescounters.subList(valuescounters.size() - 5, valuescounters.size());
for(int i = 0; i < keysFiveHighest.size(); i++)
{
System.out.println(keysFiveHighest.get(i) + ": " + valuesFiveHighest.get(i));
}
System.out.println("\nHighest - " + keyCapitalLetters.get(keyCapitalLetters.size() - 1) + ": " + valuescounters.get(valuescounters.size() -1 ));
System.out.println("\nLowest - " + keyCapitalLetters.get(0) + ": " + valuescounters.get(0));
}
public static void doSelectionSort(List<String> keyList, List<Integer> valuesList) {
for (int i = 0; i < valuesList.size(); i++) {
// find position of smallest num between (i + 1)th element and last element
int pos = i;
for (int j = i; j < valuesList.size(); j++) {
if (valuesList.get(j) < valuesList.get(pos))
pos = j;
}
// Swap min (smallest num) to current position on array
int minValue = valuesList.get(pos);
String minKey = keyList.get(pos);
valuesList.set(pos, valuesList.get(i));
keyList.set(pos, keyList.get(i));
valuesList.set(i, minValue);
keyList.set(i, minKey);
}
}
}
OutPut
Sorted:
J: 0
Q: 0
Z: 0
X: 2
B: 5
V: 5
F: 6
W: 6
K: 7
C: 10
G: 11
Y: 13
H: 14
D: 16
U: 17
P: 19
M: 19
L: 22
R: 24
O: 25
A: 30
I: 38
N: 38
S: 38
T: 43
E: 59
Highest Five
I: 38
N: 38
S: 38
T: 43
E: 59
Highest - E: 59
Lowest - J: 0
Upvotes: 1
Reputation: 3765
Yes. You are right. You can use a hashmap. Try this code ->
public static Hashmap<String, Integer> letterInstances(String file)throws IOException{
Hashmap<String, String> instanceMap = Hashmap<String, String>;
Scanner reader = new Scanner(new File("test.txt"));//scanner accesses file
while(reader.hasNext()){
String line = reader.nextLine();
for(int i = 0; i< line.length(); i++){
String character = line.get(i).toString().toLowerCase();
int count = 0;
if(instanceMap.containsKey(character)) count = instanceList.get(character);
instanceMap.put(character, count)
}
return instanceMap;
}
Or if you also want to sort them to get the first 5 results, use this ->
public List<Entry<String, Integer>> getTop5(Hashmap map){
Set<Entry<String, Integer>> set = map.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(
set);
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o2.getValue().compareTo(o1.getValue());
}
});
System.out.println(list.subList(0, 5)); // this is the list of largest 5 characters in count
return list.subList(0, 5);
}
Upvotes: 2