java中的集合框架
时间 2021-01-03
标签
java
集合框架
集合框架如何使用
关于集合框架的代码
一、集合框架:如果不知道程序运行时需要多少对象,或者需要更复杂的存储对象,可以使用java的集合框架。
。
三 特点:
1、Collection接口存储一组不唯一,无序的对象
2、List接口存储一组不唯一,有序(插入顺序)的对象
3、Set接口存储一组唯一,无序的对象。
4、Map接口存储一组健值对象,提供了Key到value的映射。一个映射不能包含重复的健,每个健只能映射一个值。
5、ArrayList实现了长度可变的数组,在内存中分配连续的空间。遍历元素和随机访问效率高。但是增加和删除效率低。
Linkedlist采用链表式存储方式,插入删除效率高,单查询效率相对较低
在Java的学习工具API中,展示了很多list set map 的方法,大家可以参考学习一下。中文的网址: http://www.yq1012.com/api 。
在这里,我们着重来学习一下 ,如何遍历这些接口和类。遍历list set 和Map的几种方式。
List接口的ArrayList和linkedlist的遍历输出
public static void main(String[]args){
List <String> list=new ArrayList<String>();
list.add("小猫");
list.add("小狗");
list.add("小猪");
list.add("小虎");
list.add("小熊");
// 第一种遍历方式
for(int i=0;i<list.size();i++){
System.out.print(list.get(i));
}
System.out.print("**************");
// 第二种遍历方式
for (String str : list) {
System.out.print(str);
}
System.out.print("**************");
// 第三种遍历
Iterator<String> is=list.iterator();
while(is.hasNext()){
System.out.print(is.next());
}
List <String> list1=new LinkedList<String>();
list1.add("小花");
list1.add("小明");
list1.add("小红");
list1.add("小帅");
list1.add("小东");
// 第一种遍历方式
for(int i=0;i<list1.size();i++){
System.out.print(list1.get(i));
}
System.out.println("**************");
// 第二种遍历方式
for (String str : list1) {
System.out.print(str);
}
System.out.println("**************");
// 第三种遍历
Iterator<String> it=list1.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
Map接口实现类HashMap的遍历
1通过keyset()方法
public class MapText {
public static void main(String[]args){
Map<String,String> map =new HashMap<String,String>();
map.put("name", "张小花");
map.put("sex", "女");
map.put("年龄", "27");
map.put("爱好", "学习");
// 第一种遍历
Set<String> set= map.keySet();
Iterator <String> it= set.iterator();
while(it.hasNext()){
String mid= it.next();
System.out.println(mid);
String m=map.get(mid);
System.out.println(m);
}
System.out.println("**********");
// 第二种遍历
for (String str : set) {
System.out.println(str);
System.out.println(map.get(str));
}
}
2通过entryset()方法
注:在集合中传入对象
public class Student {
private String name;
private String sex;
private String age;
private String id;
public Student(String name, String sex, String age, String id) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public String getSex() {
return sex;
}
public String getAge() {
return age;
}
public String getId() {
return id;
}
测试类
public class StudentText {
public static void main(String[] args) {
HashMap<String,Student> p=new HashMap<String,Student>();
p.put("一", new Student("张小花","女","27","001"));
p.put("二", new Student("小明","男","24","002"));
p.put("三",new Student("小帅","女","27","003"));
Set<Entry<String, Student>> set= p.entrySet();
Iterator<Entry<String, Student>> it = set.iterator();
while(it.hasNext()){
Entry<String, Student> d = it.next();
System.out.println(d.getKey());
System.out.println(d.getValue().getAge()+d.getValue().getName()+
d.getValue().getSex()+d.getValue().getId());
}
}
Set接口实现类Hashset的遍历。注:set没有get方法
public class SetText { public static void main(String[]args){ Set<String> set=new HashSet<String>(); set.add("苹果"); set.add("鸭梨"); set.add("香蕉"); set.add("西瓜"); set.add("菠萝"); // 第一种遍历(foreach遍历输出) for (String str : set) { System.out.println(str); } System.out.println("********"); // 第二种遍历 Iterator<String> is= set.iterator(); while(is.hasNext()){ System.out.println(is.next()) ; }