Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common collection classes in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

本篇内容介绍了"java中的常用集合类有哪些"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、Set集合

其主要实现类有HashSet、TreeSet存放对象的引用,不允许有重复对象。

实例代码:

public class SetTest { public static void main(String[] args) { Set set=new HashSet(); //添加数据 set.add("abc"); set.add("cba"); set.add("abc");//故意重复 set.add(123); set.add(true); System.out.println("集合元素个数:"+set.size()); //遍历出集合中每一个元素 Iterator it=set.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }

通过java的equals()方法判别。如果有特殊需求须重载equals()方法。

1.HashSet(),调用对象的hashCode()方法,获得哈希码,然后再集合中计算存放对象的位置。通过比较哈希码与equals()方法来判别是否重复。所以,重载了equals()方法同时也要重载hashCode()方法。

相关视频教程推荐:java在线学习

2.TreeSet(),继承ShortedSet接口,能够对集合中对象排序。默认排序方式是自然排序,但该方式只能对实现了Comparable接口的对象排序,java中对Integer、Byte、Double、Character、String等数值型和字符型对象都实现了该接口。

如果有特殊排序,须重载该接口下的compareTo()方法或通过Comparator接口的实现类构造集合。

二、List集合

其主要实现类有LinkedList、ArrayList,前者实现了链表结构,后者可代表大小可变的数组。

List的特点是能够以线性方式储蓄对象,并允许存放重复对象。List能够利用Collections类的静态方法sort排序。sort(List list)自然排序;sort(List listm,Comparator codddmparator)客户化排序。

实例代码:

List:线性集合接口,有序;

ArrayList:动态数组[可变尺子的动态数组];

LinkedList:链表结构的集合。

public class ListTest { //ArrayList static void testOne(){ List list=new ArrayList(); //添加数据 list.add("abc"); list.add("cba"); list.add(123); list.add(0,"fist"); //查看集合尺子 System.out.println("存放"+list.size()+"个元素"); list.remove(0);//删除第一个元素 //查看集合中是否包含cba if(list.contains("cba")){ System.out.println("包含元素cba"); } //取出集合中第二个元素 System.out.println("第二个元素是:"+list.get(1)); //取出集合中所有元素 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } //LinkedList static void testTwo(){ LinkedList list=new LinkedList(); //添加元素 list.add("aaaa"); list.add(123123); list.addFirst("1111111"); list.addLast("2222222"); list.add("33333333"); System.out.println("元素个数:"+list.size()); //取出第三个元素 System.out.println("第三个元素是:"+list.get(2)); //第一个元素 System.out.println("第一个元素:"+list.getFirst()); System.out.println("最后一个元素:"+list.getLast()); //删除第一个元素 list.removeFirst(); for (Object object : list) { System.out.println(object); } } public static void main(String[] args) { //testOne(); testTwo(); } }

三、Map集合。

其主要实现类有HashMap、TreeMap。Map对值没有唯一性要求,对键要求唯一,如果加入已有的健,原有的值对象将被覆盖。

HashMap类按照哈希算法来存取键对象,可以重载equals()、hashCode()方法来比较键,但是两者必须一致。TreeMap,可自然排序,也可通过传递Comparator的实现类构造TreeMap。

Map:键值对存储结构的集合,无序。

实例代码:

public class MapTest { public static void main(String[] args) { //实例化一个集合对象 Map map=new HashMap(); //添加数据 map.put("P01", "zhangSan"); map.put("P02", "Lucy"); map.put("PSex", "男"); map.put("PAge", "39"); map.put("PAge", "22");//key,重复会被后面的覆盖 //判断是否有一个key为PSex if(map.containsKey("PSex")){ System.out.println("存在"); } System.out.println("集合大小:"+map.size()); System.out.println("输出key为PAge的值:"+map.get("PAge")); //遍历出Map集合中所有数据 Iterator it=map.keySet().iterator(); while(it.hasNext()){ String key=it.next().toString(); System.out.println("key="+key+",value="+map.get(key)); } /* Set set=map.keySet();//取出map中所有的key并封装到set集合中 Iterator it=set.iterator(); while(it.hasNext()){ String key=it.next().toString(); System.out.println("key="+key+",value="+map.get(key)); } */ } }

"java中的常用集合类有哪些"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report