List集合
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * @Author: Re * @Date: 2020/12/6 15:39 */ public class CurdTest01 { List<String> stringList=new ArrayList<>(); public void addFunction(){ for (int i = 0; i < 9; i++) { stringList.add(""+i); } } public void deleteFunction(){ stringList.remove(5); } public List<String> alterFunction(){ stringList.set(1,"5"); return stringList; } public void getStringList(){ System.out.println(stringList.get(6)); } public void getStringList1() { System.out.println("使用迭代器遍历:"); Iterator<String> it=stringList.iterator(); while (it.hasNext()){ String string=it.next(); System.out.print(string+"\t"); } System.out.println(); } public void getStringList2(){ System.out.println("使用for遍历:"); for (int i = 0; i < stringList.size(); i++) { System.out.print(stringList.get(i)+"\t"); } System.out.println(); } public void getStringList3(){ System.out.println("使用foreach遍历:"); for(String list:stringList){ System.out.print(list+"\t"); } System.out.println(); } public static void main(String[] args) { CurdTest01 curdTest01=new CurdTest01(); curdTest01.addFunction(); curdTest01.getStringList1(); curdTest01.deleteFunction(); curdTest01.getStringList2(); curdTest01.alterFunction(); curdTest01.getStringList3(); curdTest01.getStringList(); } }
Set集合
import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * @Author: Re * @Date: 2020/12/6 16:46 */ public class CurdTest02 { Set<String> stringSet=new HashSet<>(); public void addFunction(){ for (int i = 0; i < 9; i++) { stringSet.add(""+i); } } public void deleteFunction(){ stringSet.remove(5); } public void alterFunction(String oldDate,String newDate){ stringSet.remove(oldDate); stringSet.add(newDate); } public void getFunction1(){ System.out.println("使用迭代器遍历:"); Iterator<String> it=stringSet.iterator(); while(it.hasNext()){ System.out.print(it.next()+"\t"); } System.out.println(); } public void getFunction2(){ System.out.println("使用foreach遍历:"); for (String list:stringSet) { System.out.print(list+"\t"); } System.out.println(); } public static void main(String[] args) { CurdTest02 curdTest02=new CurdTest02(); curdTest02.addFunction(); curdTest02.getFunction1(); curdTest02.alterFunction("5","4"); curdTest02.getFunction2(); curdTest02.deleteFunction(); curdTest02.getFunction1(); } }
Map集合
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * @Author: Re * @Date: 2020/12/6 17:03 */ public class CurdTest03 { Map<Integer,String> integerStringMap=new HashMap<>(); public void addFunction(){ integerStringMap.put(1,"张三"); integerStringMap.put(2,"李四"); integerStringMap.put(3,"王五"); } public void deleteFunction(){ integerStringMap.remove(3); } public void alterFunction(){ integerStringMap.put(1,"小二"); } public void getFunction1(){ System.out.println("用foreach遍历:"); for (Integer key:integerStringMap.keySet()) { System.out.println(key+"="+integerStringMap.get(key)); } System.out.println(); } public void getFunction2(){ System.out.println("利用迭代器遍历:"); Set<Map.Entry<Integer,String>> entrySet=integerStringMap.entrySet(); Iterator it=entrySet.iterator(); while(it.hasNext()){ System.out.println(it.next()); } System.out.println(); } public static void main(String[] args) { CurdTest03 curdTest03=new CurdTest03(); curdTest03.addFunction(); curdTest03.getFunction1(); curdTest03.alterFunction(); curdTest03.getFunction2(); curdTest03.deleteFunction(); curdTest03.getFunction2(); } }