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…

JAVA07Object class, Date class, Calendar class, System class, packaging class, Collection, generics, List, Set, data structure, Collections

JAVA07Object class, Date class, Calendar class, System class, packaging class, Collection, generics, List, Set, data structure, Collections

1. Overview java.lang.Object class is in the java language The following class is the parent class of all classes. The other class Object is the root class of the class hierarchy. Each class uses Object as its superclass. All objects (including arrays) implement the methods of this class. It contains 11 methods. 1 package cn.itcast.demo01.demo01; 2 3 public class Demo01ToString { 4 public static void main(String[] args) { 5 //The Person class inherits the Object class by default, and you can use the toString method 6 //Create Person object 7 Person person = new Person(“张三”,19); 8 String s = person.toString(); 9 // Directly printing the name of the object is actually calling the toStirng method of the object person = person.toString 10 System.out.println(s);//cn.itcast.demo01.demo01.Person@75412c2f 11 System.out.println(person);//cn.itcast.demo01.demo01.Person@75412c2f 12 } 13 } 14 15 1 package cn.itcast.demo01.demo01; 2 3 public class Person { 4 private String name; //Private member variables 5 private int age;// Private member variable 6 //Add the corresponding construction method 7 8 9 public Person() { 10 } 11 // Directly printing the address value of the object is meaningless, you need to override the toString method 12 13 14 @Override 15 public String toString() { 16 // Return super.toString()…

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’…

Summary of the relationship between Collection, List, Set, and Map in Java

As a newbie to Java, individual contacts are a bit confusing, so I’ll summarize their relationship 1. Relationship Collection –List: stored in a specific order –ArrayList, LinkList, Vector –Set: cannot contain duplicate elements –HashSet, TreeSet Map –HashMap, HashTable, TreeMap 2. Explain separately Collection: Collection is a parent interface. List and Set are sub-interfaces inherited from it. Collection is the most basic collection interface. The Java SDK does not provide classes that directly inherit from Collection, but provides sub-interfaces that inherit from him. Classes, such as List and Set. All Collection classes used support an Iterator() method to traverse. List: The List interface is ordered and will accurately insert elements into the specified position. Unlike the Set interface below, the List interface allows the same elements ArrayList: implements a variable-sized array, allowing all elements to be synchronized, that is, there is no synchronization method LinkList: allows null elements, usually operates at the head or tail, so it is often used for stacks, queues and deques Vector: similar to ArrayList, but Vector is synchronized, and Stack inherits from Vector Set: is a Collection interface that does not contain repeated elements HashSet: cannot have duplicate elements, the bottom layer is implemented using HashMap…

How to use Map, Set, and List in java projects

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.…

Technology Sharing

Dark Horse Programmer – Java Basic Collection (1) Collection, set, list

——Java training, Android training, iOS training, .Net training, looking forward to communicating with you! —— /strong> There are many collections in Java, also called containers. The following figure shows the composition and classification of the collection framework. 1. Why do collection classes appear? Object-oriented languages ​​embody things in the form of objects, so in order to facilitate the operation of multiple objects, objects are stored. Collections are the most commonly used way to store objects. 2. Arrays and collections are both containers. What are the differences? Although arrays can also store objects, their length is fixed; collection lengths are variable. Basic data types can be stored in arrays, while collections can only store objects. 3. Characteristics of collection classes Collections are only used to store objects. The length of the collection is variable, and the collection can store different types of objects.      Collection Collection is a common interface in the collection framework. There are two sub-interfaces under it: List and Set. Affiliation: Collection                                                                                                                                                                                                                                               |–List//Elements are ordered and elements can be repeated. Because the collection system has indexes.                      |–Set//The elements are unordered and the elements cannot be repeated. The following is an overview of Collection: 1 import java.util.*; 2…

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…

Technology Sharing

Dark Horse Programmer – Java Basic Collection (1) Collection, set, list

——Java training, Android training, iOS training, .Net training, looking forward to communicating with you! —— /strong> There are many collections in Java, also called containers. The following figure shows the composition and classification of the collection framework. 1. Why do collection classes appear? Object-oriented languages ​​embody things in the form of objects, so in order to facilitate the operation of multiple objects, objects are stored. Collections are the most commonly used way to store objects. 2. Arrays and collections are both containers. What are the differences? Although arrays can also store objects, their length is fixed; collection lengths are variable. Basic data types can be stored in arrays, while collections can only store objects. 3. Characteristics of collection classes Collections are only used to store objects. The length of the collection is variable, and the collection can store different types of objects.      Collection Collection is a common interface in the collection framework. There are two sub-interfaces under it: List and Set. Affiliation: Collection                                                                                                                                                                                                                                               |–List//Elements are ordered and elements can be repeated. Because the collection system has indexes.                      |–Set//The elements are unordered and the elements cannot be repeated. The following is an overview of Collection: 1 import java.util.*; 2…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索