Table of contents
Java Advanced Features - Collection Framework (1)
Level 1: Basic use of collections
Level 2: Add, delete, modify and check the ArrayList collection
Level 3: Architecture of Collections
Level 5: Add, delete, modify and check the Map collection
Level 6: Multiple choice questionsEdit
Java string and collection exercises - word frequency statistics
Level 2: Determine the position of the word in the string
Level 3: Implement word frequency statistics and sorting output
Java Advanced Features - Collection Framework (1)
Level 1: Basic use of collections
package step1;
import java.util.ArrayList;
// 导包
/********** Begin **********/
/********** End **********/
public class HelloWorld {
@SuppressWarnings("unchecked")
public ArrayList getList() {
/********** Begin **********/
ArrayList List = new ArrayList();
List.add("https:www.educoder.net");
List.add("2018.423");
return List;
/********** End **********/
}
}
Level 2: Add, delete, modify and check the ArrayList collection
package step2;
import java.util.ArrayList;
import java.util.Scanner;
public class HelloWorld {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
//获取输入的数据并添加至集合
Scanner sc = new Scanner(System.in);
ArrayList list = new ArrayList<>();
int length = sc.nextInt();
for(int i =0 ; i< length; i++){
list.add(sc.next());
}
/********** Begin *********/
list.remove(list.size()-1);
list.remove(0);
list.add("hello");
list.add("educoder");
list.set(2,"list");
int size=list.size();
for(int i=0;i<size;i++){
System.out.println(list.get(i));
}
/********** End **********/
}
}
Level 3: Architecture of Collections
package step3;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
public class HelloWorld {
public HashSet getHashSet(){
/********** Begin **********/
HashSet hs=new HashSet();
hs.add("www.educoder.net");
return hs;
/********** End **********/
}
public ArrayList getArrayList(){
/********** Begin **********/
ArrayList al=new ArrayList();
al.add("www.educoder.net");
return al;
/********** End **********/
}
public LinkedList getLinkedList(){
/********** Begin **********/
LinkedList ll=new LinkedList();
ll.add("www.educoder.net");
return ll;
/********** End **********/
}
public Map getHashMap(){
/********** Begin **********/
HashMap hm=new HashMap();
hm.put("address","www.educoder.net");
return hm;
/********** End **********/
}
}
Level 4: Generics
package step4;
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//程序会输入三次数据
/********** Begin **********/
List<String> list=new ArrayList<String>();
for(int i=0;i<3;i++){
list.add(sc.next());
}
System.out.println("集合的第1个数据为:"+list.get(0));
System.out.println("集合的第2个数据为:"+list.get(1));
System.out.println("集合的第3个数据为:"+list.get(2));
/********** End **********/
}
}
Level 5: Add, delete, modify and check the Map collection
package step5;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Map<String, Integer> menuDict = new HashMap<>();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
menuDict.put(sc.next(),sc.nextInt());
}
/********** Begin **********/
menuDict.put("lamb",50);
System.out.println(menuDict.get("fish"));
menuDict.put("fish",100);
menuDict.remove("noodles");
System.out.println(menuDict.toString());
/********** End **********/
}
}
Level 6: Multiple choice questions
Java string and collection exercises - word frequency statistics
Level 1: Word Segmentation
package step1;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class StudentDemo{
//使用String.split()方法分割
public List<String> splitPartition(String str){
List<String> list=new ArrayList<String>();
//请在此添加实现代码
/********** Begin **********/
String[] strings = str.split("\\|");
for (String string : strings) {
list.add(string);
}
/********** End **********/
return list;
}
//使用StringTokenizer类进行分割
public List<String> tokenPartition(String str){
List<String> list=new ArrayList<String>();
//请在此添加实现代码
/********** Begin **********/
StringTokenizer strings = new StringTokenizer(str, "?");
while (strings.hasMoreTokens()) {
list.add(strings.nextToken());
}
/********** End **********/
return list;
}
}
Level 2: Determine the position of the word in the string
package step2;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class StudentDemo{
//返回一个Map集合来得到单词和首次出现的下标 key为单词名称 value为单词的角标
public Map<String, Integer> getMap(String str){
Map<String, Integer> map = new HashMap<String, Integer>();
//对str进行分割 再加入map集合中
//请在此添加实现代码
/********** Begin **********/
StringTokenizer strings = new StringTokenizer(str, " ,?.!:\n");
while (strings.hasMoreTokens())
{
String s=strings.nextToken();
int value=str.indexOf(s);
map.put(s,value);
}
/********** End **********/
return map;
}
}
Level 3: Implement word frequency statistics and sorting output
package step3;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class StudentDemo{
//获取单词的数量
public Map<String, Integer> getWordCount(String str) {
Map<String, Integer> map = new HashMap<String, Integer>();
//请在此添加实现代码
/********** Begin **********/
StringTokenizer stn = new StringTokenizer(str, " ;’,?.!:\n");
while (stn.hasMoreTokens()) {
String str1 = stn.nextToken();
if (map.containsKey(str1)) {
map.put(str1, map.get(str1) + 1);
} else {
map.put(str1, 1);
}
}
/********** End **********/
return map;
}
}