Introduction to JAVAList, Set, and Map
Before writing this article, I would like to first thank Chuanzhi Podcast – because of their selflessness, we have many high-quality learning videos. Let’s get to the point – introduce List, Set, and Map. First of all, List and Set are subclasses of Collection. So first introduce the Collection collection 1. Collection (Collection) (1) The origin of collections? What we learn is Java – object-oriented – operating many objects – storage – container ( Arrays and StringBuffer) — Arrays and arrays have a fixed length – so they are not suitable for changing needs – Java provides collections for us to use. (2) What is the difference between a collection and an array? A: The difference in length is that the array is fixed and the collection is variable. B: The difference in content is that the array can be a basic type or a reference type. The collection can only be a reference type. C: the element content array can only store elements. The same type of collection can store different types (in fact, collections generally store the same type) (3) Inheritance architecture of collections? Due to different needs, Java provides different collection classes. The data structures of these…
Java basics – simple operations and traversal of List, Set, and Map
The first:List [three iteration methods] public class ListTest { ArrayList< String > list1= new ArrayList<String>();public ArrayList <String> addMethod(){for ( int i = 0; i < 9; i++) {list1.add(i+ ;””);}return list1; }public ArrayList<String> deleteMethod(){list1.remove( “5”);return list1 ;}public ArrayList<String> updateMethod(){list1.set (1,”5″);return list1;}/** Iterator traversal*/ public void selectMethod1(){Iterator <String>it=list1.iterator ();while(it.hasNext()){String string=it.next();System.out.print(string& #43;”\t”);}}/** foreach() method traversal*/public void selectMethod2(){for(String s:list1){System .out.print(s+”\t”);}}/* * for() method traversal */public void selectMethod3 (){for (int i &# 61; 0; i < list1 .size(); i++) {System. out.print(list1.get(i)+”\t”);}} public static void main(String[ ] args) {ListTest list1=new ListTest();System.out.print(list1.addMethod()+”\t” );list1.selectMethod1();System.out.println(); System.out.print(list1.deleteMethod()+”\t”);list1.selectMethod2();System.out.println() ;System.out.print(list1.updateMethod()+”\t”);list1.selectMethod3();}} Second:Set [Two iteration methods] public class SetTest {HashSet<String> set1 =new HashSet<>();public HashSet<String> addMethod(){set1.add(“aaa”);set1.add(“bbb”);set1.add(” ccc”);return set1;}public HashSet<String> deleteMethod(){set1.remove(“aaa”);return set1;} public HashSetreturn map;} public Map<String,String> deleteMethod() {map.remove(“001”);return map;}public Map<String,String> updateMethod(){map.remove(“002”); map.put(“001″,”Erha”);return map;}/** Iterator traversal*/public void selectMethod(){Iterator <String> it=map.keySet( ).iterator();while span>(it.hasNext()){String s1=it. next();String name=map .get(s1);System.out.println(s1+”\t”+name);}}public void selectMethod1( ){/** foreach() method traversal*/for (Map .Entry<String, String> entry : map.entrySet ()){System.out.println(entry.getKey() + “\t” + entry.getValue());}} public static void main(String[ ] args) {MapTest map1=new MapTest();System.out.print(map1.addMethod()+”\t” );map1.selectMethod();System.out.println(); System.out.print(map1.deleteMethod()+”\t”);map1.selectMethod();System.out.println() ;System.out.print(map1.updateMethod()+”\t”);map1.selectMethod1();}} class=”token generics”><String, String> entry : map.entrySet()){System.out.println(entry.getKey() + “\t” + entry.getValue());}}public static void main(String[] args) {MapTest map1&# 61;new MapTest( );System.out.print(map1.addMethod()+“\t”);map1.selectMethod();System.out.println ();System.out.print(map1.deleteMethod()+“\ t”);map1.selectMethod() ;System.out. println();System.out.print(map1.updateMethod()+“\t”);map1.selectMethod1( );}} ode>