600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > List Set Map的特点及遍历方法

List Set Map的特点及遍历方法

时间:2021-10-11 01:34:31

相关推荐

List Set Map的特点及遍历方法

List、Set、Map

List与Set集合的区别List、Map、Set三个接口,存取元素时,各有什么特点SetListMap List、Set、Map遍历的实现先来个Employee实体类List遍历的三种方式Set遍历的两种方式Map的几种遍历

List与Set集合的区别

list,set都是继承了collection接口list与set的特点 list元素放入有顺序,可重复元素set元素放入无顺序 ,不可重复元素 set与list的对比 set检查元素效率低下,删除和插入的效率高,不会引起元素位置改变list和数组相似,list可实现动态增长,查找元素效率高,插入和检查的效率低,会引起其他元素位置改变

List、Map、Set三个接口,存取元素时,各有什么特点

Set

Set里面不允许有重复的元素,

存元素:add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法可成功加入该元素时,则返回true;当集合含有与某个元素equals相等的元素时,此时add方法无法加入该元素,返回结果为false。取元素:没法说取第几个,只能以Iterator接口取得所有的元素,再逐一遍历各个元素。

List

List表示有先后顺序的集合,

存元素:多次调用add(Object)方法时,每次加入的对象按先来后到的顺序排序,也可以插队,即调用add(int index,Object)方法,就可以指定当前对象在集合中的存放位置。取元素: 方法1:Iterator接口取得所有,逐一遍历各个元素方法 2:调用get(index i)来明确说明取第几个。

Map

Map是双列的集合,存放用put方法:put(obj key,obj value),每次存储时,要存储一对key/value,不能存储重复的key,这个重复的规则也是按equals比较相等。

取元素: 用get(Object key)方法根据key获得相应的value。也可以获得所有的key的集合,还可以获得所有的value的集合,还可以获得key和value组合成的Map.Entry对象的集合。

想了解更多请看:/areyouready/p/7580489.html

List、Set、Map遍历的实现

先来个Employee实体类

package conn;public class Employee{private int id;private String name;public Employee(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Employee{" +"id=" + id +", name='" + name + '\'' +'}';}}

List遍历的三种方式

package conn;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class TestList {public static void main(String[] args) {List<Employee> list = new ArrayList<>();list.add(new Employee(1,"张三"));list.add(new Employee(2,"张四"));list.add(new Employee(3,"张五"));//第一种遍历for循环System.out.println("第一种遍历for循环");for (int i = 0; i <list.size() ; i++) {Employee employee = list.get(i);System.out.println(employee.toString());}System.out.println("--------------------");//第二种迭代器System.out.println("第二种迭代器");Iterator<Employee> iterator = list.iterator();while (iterator.hasNext()){Employee next = iterator.next();System.out.println(next.toString());}System.out.println("--------------------");//第三种遍历for...eachSystem.out.println("第三种遍历foreach");for(Employee employee : list){System.out.println(employee);}}}

运行结果

Set遍历的两种方式

package conn;import java.util.HashSet;import java.util.Iterator;import java.util.Set;public class TestSet {public static void main(String[] args) {HashSet<Employee> set = new HashSet<>();set.add(new Employee(1,"张三"));set.add(new Employee(2,"张四"));set.add(new Employee(3,"张五"));//迭代遍历System.out.println("第一种:迭代遍历");Iterator<Employee> iterator = set.iterator();while (iterator.hasNext()) {Employee next = iterator.next();System.out.println(next);}System.out.println("--------------------");//foreach循环遍历System.out.println("第二种:foreach循环遍历");for (Employee employee : set) {System.out.println(employee);}}}

运行结果

Map的几种遍历

package conn;import java.util.*;public class TestMap {public static void main(String[] args) {Map<Integer,Employee> map = new HashMap();map.put(1,new Employee(1,"张三"));map.put(2,new Employee(2,"张四"));map.put(3,new Employee(3,"张五"));//第一种:通过Map.keySet遍历key和valueSystem.out.println("第一种:通过Map.keySet遍历key和value");for (Integer key : map.keySet()) {System.out.println("key= "+ key + " and value= " + map.get(key));}System.out.println("--------------------");//第二种:通过Map.entrySet使用iterator遍历key和value:System.out.println("第二种:通过Map.entrySet使用iterator遍历key和value:");Iterator<Map.Entry<Integer, Employee>> iterator = map.entrySet().iterator();while (iterator.hasNext()){Map.Entry<Integer, Employee> next = iterator.next();System.out.println("key= " + next.getKey() + " and value= " + next.getValue());}System.out.println("--------------------");// 第三种:推荐,尤其是容量大时System.out.println("第三种:通过Map.entrySet遍历key和value");for (Map.Entry<Integer, Employee> entry : map.entrySet()) {System.out.println("key= " + entry.getKey() + "and value= " + entry.getValue());}System.out.println("--------------------");//第四种:通过Map.values()遍历所有的value,但不能遍历keySystem.out.println("第四种:通过Map.values()遍历所有的value,但不能遍历key");for (Employee employee : map.values()) {System.out.println("value= " + employee);}}}

运行结果

他山之石,可以磨玉。小弟初来咋到,有不对之处,还望大牛指正。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。