public static void main(String args[]) throws IOException{
List list = new ArrayList();
List temp = new ArrayList();
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
for (int i=0;i<15;i++)
{
System.out.println("Enter value");
String x=br.readLine();
list.add(x);
}
LinkedHashMap lhm=new LinkedHashMap();
for(String str1:list){
int flag=0;
for(Entry entry:lhm.entrySet()){
if(entry.getKey().equals(str1)){
flag=1;
break;
}}
if(flag==0){
lhm.put(str1, 1);
}
}
int maxCount = 1;
int currCount = 1;
for (int i=1;i
if (list.get(i).equals(list.get(i-1))) {
++currCount;
if(list.size()==i+1){
maxCount = Math.max(lhm.get(list.get(i)), currCount);
lhm.put(list.get(i), maxCount);
}
} else {
maxCount = Math.max(lhm.get(list.get(i-1)), currCount);
lhm.put(list.get(i-1), maxCount);
currCount = 1;
}
}
for(Entry entry:lhm.entrySet()){
System.out.println("Maximum Sequential occurrence of element- "+entry.getKey()+" is- "+entry.getValue());//display result
}
}
上面的代码将打印列表中所有元素的最大连续出现.