Basic Java Issues (4)

Source: Internet
Author: User

/* * 未使用泛型面对的问题 * 1、可以添加任何类型元素 * 2、类型转换出现ClassCastException异常 * * 在集合中使用泛型,规定添加元素的类型 */ @Test public void test01() { List l = new ArrayList(); l.add(123); l.add(222); l.add("abc"); l.add(new a("lisi", 23)); l.add(333);// java.lang.ClassCastException 异常// Collections.sort(l); List<Integer> l1 = new ArrayList<Integer>(); l1.add(123); l1.add(222);// l1.add("abc"); 不能添加// l1.add(new a("lisi", 23)); 不能添加 l1.add(333); System.out.println(l1); TreeMap<a, Integer> m = new TreeMap<a, Integer>(); m.put(new a("lisi",23), 10000); m.put(new a("zhangs",34), 20000); m.put(new a("lisi",55), 100000); m.put(new a("zhangliu",12), 2000); m.put(new a("wangw",45), 20000); Set<Map.Entry<a, Integer>> set = m.entrySet(); Iterator<Map.Entry<a, Integer>> i = set.iterator(); while (i.hasNext()) { System.out.println(i.next()); } }

Basic Java Issues (4)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.