Simple usage tutorial for Map, Set, and List in java (quick start)
Map, Set, List Common methods of List 1. Create List list = new ArrayList(); List list = new LinkedList(); //can also be used as a linked list List<List> list = new ArrayList(); 2. Traversal //Essentially, it is calling Iterator for(String s:list){ System.out.print(s); } 3. Convert List to Array //Both writing methods are possible. The first one is slightly more standardized, but it can only be an array of encapsulated type. Integer array[] = list.toArray(new Integer[list.size()]); Integer array[] = list.toArray(); //java8 writing method, you can use stream to realize direct conversion into basic type array int[] result = list.stream().mapToInt(i->i).toArray(); 4. Convert array to List //It doesn’t matter whether array[] is a package class or not. If it is not a package type, the system will automatically convert it to a package type. Integer array[] = {1,2,3}; intarray[] = {1,2,3}; List list = new ArrayList(Arrays.asList(array)); //There is a simple way to assign values to List directly by default. List list = new ArrayList(Arrays.asList(1,2,3)); 5. Merge two Lists List list1 = new ArrayList(); List list2 = new ArrayList(); list.addAll(list); //All elements of list2 are added to list1 Common methods of Set 1. Create Set Set set = new HashSet(); Set set = new…
Why are Java’s various collections unsafe (List, Set, Map) and alternatives?
We already know that there are various unsafe problems under multi-threading, and we all know the basic solutions to concurrency. Here we conduct an actual simulation of an error situation so that we can associate it with a specific production environment. 1. The insecurity of List 1.1 Question Look at a piece of code: public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i = 0; i { list.add(UUID.randomUUID().toString().substring(0,8)); System.out.println(list); },String.valueOf(i)).start(); } } The process is very simple. There are only three threads. The write operation of add is performed on the same list, and then the output is read. Output the results, execute it a few times, and you will be full of surprises. Well, when the situation is not serious, the normal operation here obviously ends, but the data has been read out before it has time to write. If you try increasing the number of threads, you may still see such a spectacle: An error was reported: Key exception: java.util.ConcurrentModificationException, which translates to concurrent modification exception. 1.2 Reasons There is no special processing in ordinary ArrayList collections. In multi-threaded situations, they can be accessed jointly. Then when multiple threads operate at the same…
Simple usage tutorials for Map, Set, and List in java (quick start)
Map, Set, List Common methods of List 1. Create List list = new ArrayList(); List list = new LinkedList(); //can also be used as a linked list List<List> list = new ArrayList(); 2. Traversal //Essentially, it is calling Iterator for(String s:list){ System.out.print(s); } 3. Convert List to Array //Both writing methods are possible. The first one is slightly more standardized, but it can only be an array of encapsulated type. Integer array[] = list.toArray(new Integer[list.size()]); Integer array[] = list.toArray(); //java8 writing method, you can use stream to realize direct conversion into basic type array int[] result = list.stream().mapToInt(i->i).toArray(); 4. Convert array to List //It doesn’t matter whether array[] is a package class or not. If it is not a package type, the system will automatically convert it to a package type. Integer array[] = {1,2,3}; intarray[] = {1,2,3}; List list = new ArrayList(Arrays.asList(array)); //There is a simple way to assign values to List directly by default. List list = new ArrayList(Arrays.asList(1,2,3)); 5. Merge two Lists List list1 = new ArrayList(); List list2 = new ArrayList(); list.addAll(list); //All elements of list2 are added to list1 Common methods of Set 1. Create Set Set set = new HashSet(); Set set = new…
Detailed explanation of conversion between javalist, set, map and arrays
Detailed explanation of conversion between java list, set, map, and arrays 1.list to set Set set = new HashSet( new ArrayList()); 2.set to list List list = new ArrayList( new HashSet()); 3. Convert array to list List stooges = Arrays.asList( “Larry” , “Moe” , “Curly” ); At this time, there are three elements in stooges. Note: The add operation cannot be performed on the list at this time, otherwise “java.lang.UnsupportedOperationException” will be reported. Arrays.asList() returns a List, and it is a fixed-length List, so it cannot be converted to an ArrayList and can only be converted to AbstractList The reason is that the asList() method returns the list form of an array. The returned list is just another view of the array, and the array itself does not disappear. Any operations on the list will eventually be reflected on the array. So not Support remove and add methods String[] arr = { “1” , “2” }; List list = Arrays.asList(arr); 4. Convert array to set int [] a = { 1 , 2 , 3 }; Set set = new HashSet(Arrays.asList(a)); 5.map related operations. Map map = new HashMap(); map.put(“1” , “a” ); map.put(‘2’ , ‘b’ ); map.put(‘3’ , ‘c’…
How to use Map, Set, and List in java projects
How to use Map, Set and List in java projects? I believe that many inexperienced people are at a loss to solve this problem. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem. Map, Set, List Common methods of List 1. Create List list = new ArrayList(); List list = new LinkedList(); //can also be used as a linked list List<List> list = new ArrayList(); 2. Traversal //Essentially, it is calling Iterator for(String s:list){ System.out.print(s); } 3. Convert List to Array //Both writing methods are possible. The first one is slightly more standardized, but it can only be an encapsulated type. array of Integer array[] = list.toArray(new Integer[list.size()]); Integer array[] = list.toArray(); //Java8 writing method can use stream to realize direct conversion into basic type array int[] result = list.stream().mapToInt(i->i).toArray(); 4. Convert array to List //It doesn’t matter whether array[] is a package class or not. If it is not a package type, the system will automatically convert it to a package type. Integer array[] = {1,2,3}; intarray[] = {1,2,3}; List list = new ArrayList(Arrays.asList(array)); //There is a simple way to assign values to List directly by default.…
Detailed explanation of list, set, map traversal and enhanced for loop in Java
Detailed explanation of list, set, map traversal and enhanced for loop in Java Java collection classes can be divided into three parts, namely List, Set, which are extended from the Collection interface, and Map type collections that are stored in the form of key-value pairs. Regarding the enhanced for loop, it should be noted that the array subscript value cannot be accessed using the enhanced for loop, and the related methods of Iterator are also used internally for collection traversal. If you only do simple traversal reading, the enhanced for loop will indeed reduce the amount of code. Set concept: 1. Function: used to store objects 2. Equivalent to a container, which contains a set of objects, each of which appears as an element of the collection 3. Java’s containers include collection classes and arrays. The difference is Differences and common implementation classes List interface: The list is ordered and the elements can be repeated Implementation class: ArrayList: dynamic array list LinkedList: Doubly linked list Set interface: The set is unordered and the elements cannot be repeated Implementation class:HashSet:Hash set TreeSet: Tree set internal sorting Map interface: Store data in the form of key-value pairs. Data-keys are not allowed to…