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>
Threading issues with List, Set, Map, and Queue in java
2019 Unicorn companies spend heavily to recruit Python engineers>>> Collections and Concurrent are both tool classes used by Java to assist and – designed to complete certain special and easily repeatable tasks,Or some relatively solved problems. In Java, the commonly used data structures are basically divided into three categories: Map, List, and Set. Thread-safe collection The Collection framework introduced in JDK 1.2 is a highly flexible framework for representing object collections – it uses the basic interfaces List, Set and Map. Multiple implementations of each collection – HashMap, Hashtable, TreeMap, WeakHashMap, HashSet, TreeSet, Vector, ArrayList, LinkedList, etc. – are provided through the JDK. Some of these collections are already thread-safe – Hashtable and Vector – through synchronized packaging factories – Collections.synchronizedMap(), synchronizedList() and synchronizedSet() – xff0c;The rest of the collection can be rendered thread-safe. The java.util.concurrent package adds several new thread-safe collection classes – ConcurrentHashMap, CopyOnWriteArrayList and CopyOnWriteArraySet. The purpose of these classes is to provide high-performance, highly scalable, thread-safe versions of basic collection types. Thread collections in java.util still have some drawbacks. For example, when iterating over a lock, you usually need to keep the lock in the collection, otherwise you risk throwing a ConcurrentModificationException. (This feature is sometimes…
Java collection List, Set, Map, Queue, Deque
Collections are a very important part of the foundation of Java,Java provides a very rich collection API,Understand the characteristics of each collection,How to use the correct collection in various scenarios “Very important” is also the most basic quality of a Java programmer. Overall understanding The most basic and most commonly used collections in Java are mainly “Set” and “List” ;Map,Queue,Deque. Collection is the top-level interface of the collection List,Set,Queue. Collection inherits from the Iterable interface to implement traversal of elements. The relationship between each class or interface is as shown in the following class diagram: List List is an ordered collection interface, inherited from Collection, has three implementation classes,ArrayList,LinkedList, ;Vector, each has its own characteristics. ArrayList,The bottom layer is implemented through arrays,Elements have subscripts,Randomly reading elements through subscripts is highly efficient,The query time complexity is O(1). The disadvantage is that “when inserting or deleting elements”, “need to move elements” or when the array storage space is insufficient, “array expansion” is relatively expensive. In summary, ArrayList is suitable for finding and traversing elements but not for inserting and deleting elements. And ArrayList is thread-unsafe. LinkedList,The bottom layer is implemented through a linked list,Elements have no subscripts,Suitable for dynamic insertion and deletion…
Relevant commands for PHP to operate RedisLIST, SET, HASH (1)
Please visit the blog to move Related commands for PHP to operate Redis LIST, SET, HASH (1) Related commands for PHP to operate Redis KEY, String (2) Related commands for PHP to operate Redis ordered sets (Sorted Set) (3)