Write a program to find maximum repeated words from a file
package com.java2novice.algos;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;
public class MaxDuplicateWordCount
{
public Map
getWordCount(String fileName)
{
FileInputStream
fis = null;
DataInputStream
dis = null;
BufferedReader
br = null;
Map wordMap = new HashMap();
try
{
fis
= new FileInputStream(fileName);
dis
= new DataInputStream(fis);
br
= new BufferedReader(new InputStreamReader(dis));
String
line = null;
while((line
= br.readLine()) != null)
{
StringTokenizer
st = new StringTokenizer(line, " ");
while(st.hasMoreTokens())
{
String
tmp = st.nextToken().toLowerCase();
if(wordMap.containsKey(tmp))
{
wordMap.put(tmp,
wordMap.get(tmp)+1);
}
else
{
wordMap.put(tmp,
1);
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch
(IOException e)
{
e.printStackTrace();
}
finally
{
try{if(br
!= null) br.close();}catch(Exception ex){}
}
return
wordMap;
}
public
List> sortByValue(Map
wordMap)
{
Set> set = wordMap.entrySet();
List> list = new ArrayList>(set);
Collections.sort(
list, new Comparator
{
public
int compare( Map.Entry o1, Map.Entry o2 )
{
return
(o2.getValue()).compareTo( o1.getValue() );
}
}
);
return
list;
}
public static void main(String
a[])
{
MaxDuplicateWordCount
mdc = new MaxDuplicateWordCount();
Map wordMap = mdc.getWordCount("C:/MyTestFile.txt");
List> list = mdc.sortByValue(wordMap);
for(Map.Entry entry:list){
System.out.println(entry.getKey()+"
==== "+entry.getValue());
}
}
}
Write a program to find out duplicate characters in a string
package com.java2novice.algos;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DuplicateCharsInString
{
public void findDuplicateChars(String
str)
{
Map dupMap = new HashMap();
char[]
chrs = str.toCharArray();
for(Character
ch:chrs)
{
if(dupMap.containsKey(ch))
{
dupMap.put(ch,
dupMap.get(ch)+1);
}
else
{
dupMap.put(ch,
1);
}
}
Set
keys = dupMap.keySet();
for(Character
ch:keys)
{
if(dupMap.get(ch)
> 1)
{
System.out.println(ch+"--->"+dupMap.get(ch));
}
}
}
public static void main(String a[])
{
DuplicateCharsInString
dcs = new DuplicateCharsInString();
dcs.findDuplicateChars("Java2Novice");
}
}
Write a program to find top two maximum numbers in a array
package com.java2novice.algos;
public class TwoMaxNumbers
{
public void printTwoMaxNumbers(int[]
nums)
{
int maxOne = 0;
int maxTwo = 0;
for(int n:nums)
{
if(maxOne
< n)
{
maxTwo
= maxOne;
maxOne
=n;
}
else if(maxTwo < n)
{
maxTwo
= n;
}
}
System.out.println("First
Max Number: "+maxOne);
System.out.println("Second
Max Number: "+maxTwo);
}
public static void main(String a[])
{
int num[] =
{5,34,78,2,45,1,99,23};
TwoMaxNumbers
tmn = new TwoMaxNumbers();
tmn.printTwoMaxNumbers(num);
}
}
Write a program to sort a map by value
package com.java2novice.algos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class OrderByValue
{
public static void main(String a[])
{
Map map = new HashMap();
map.put("java",
20);
map.put("C++",
45);
map.put("Java2Novice",
2);
map.put("Unix",
67);
map.put("Mac",
26);
map.put("Why
this kolavari", 93);
Set> set = map.entrySet();
List> list = new ArrayList>(set);
Collections.sort(
list, new Comparator
{
public int compare(
Map.Entry o1, Map.Entry o2 )
{
return (o2.getValue()).compareTo(
o1.getValue() );
}
}
);
for(Map.Entry entry:list)
{
System.out.println(entry.getKey()+"
==== "+entry.getValue());
}
}
}
No comments:
Post a Comment