package October;
import java.util.*;
public class Test {
public static void main(String[] args) {
News news = new News("新冠确诊病例");
News news1 = new News("男子突然想起");
ArrayList arr = new ArrayList();
arr.add(news);
arr.add(news1);
int size = arr.size();
for (int i = size-1; i >=0 ;i--){
//向下转型
News news2 = (News)arr.get(i);
System.out.println(process(news2.getTitle()));
}
}
public static String process(String title) {
if(title.length()>4){
return title.substring(0,4)+"...";
}
else{
return title;
}
}
}
class News{
private String title;
public String getTitle() {
return title;
}
public News(String title) {
this.title = title;
}
@Override
public String toString() {
return "title='" + title + '\'';
}
}
package October;
import java.util.*;
public class Test {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put("morty",700);
hashMap.put("rick",700);
hashMap.put("jerry",7);
//获取集合中所有的key,给到set
Set set=hashMap.keySet();
for (Object key :set) {
//hashMap.get(key)根据对应的key获取到对应的value值
//(Integer)hashMap.get(key)
//它的类型是object需要向下转型才能进行加100运算
hashMap.put(key,(Integer)hashMap.get(key)+100);
}
//使用entrySet遍历所有的员工
Set set1 = hashMap.entrySet();
//迭代器
Iterator iterator = set1.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey());
}
//使用Collection遍历所有的工资
Collection values = hashMap.values();
for (Object value :values) {
System.out.println(value);
}
}
}