In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to apply java generics". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to apply java generics" can help you solve your doubts.
1. What is generics
Generics allow you to use an identity to represent the type of an attribute in a class or the return value and parameter type of a method when defining a class or interface. This type parameter will be determined when it is used (for example, inheriting or implementing this interface, declaring variables with this type, creating an object) (that is, passing in the actual type parameter, also known as the type argument).
Generics are a parameterized type.
2. Rules for the use of generics
Generics appear in JDK5.0 and cannot be used in previous versions
Generics need to be written in pairs.
The type of a generic type must be a reference type, not a basic data type.
If it is a basic data type, you must use its wrapper class. If there are multiple generic types, separate them with commas
3. Examples of generic applications
Example:
Public class DAO {private Map map = new HashMap (); public void save (String id,T entity) {map.put (id,entity);} public T get (String id) {return map.get (id);} public void update (String id,T entity) {map.put (id,entity);} public List list () {Collection values = map.values (); List list = new ArrayList () For (T value: values) {list.add (values);} return list;} public void delete (String id) {map.remove (id);}} public class DAOTest {public static void main (String [] args) {DAO userDAO = new DAO (); User user1 = new User (1LJ 12, "Zhang San"); User user2 = new User (2Jue 13, "Li Si") User user3 = new User (3jue 14, "Wang Wu"); userDAO.save ("1", user1); userDAO.save ("2", user2); userDAO.save ("3", user3); System.out.println (userDAO.get ("1")); userDAO.update ("1", new User (1Jing 18, "Zhang San")); List list = userDAO.list (); System.out.println (list) UserDAO.delete ("1"); System.out.println (userDAO.list ());}} read here, this article "how to apply java generics" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.