listandset实例

创建javabean对象

//创建person对象
package com.oop;

public class Person  {
	public String name;
	int scores;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getScores() {
		return scores;
	}
	public void setScores(int scores) {
		this.scores = scores;
	}
	public int getAge() {
		return age;
	}
	
	public void say() {
		System.out.println("唱山歌");
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", scores=" + scores + ", age=" + age + "]";
	}
	public void setAge(int age) {
		this.age = age;
	}
	

}

  实例:

package com.oop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ListAndSet {
	public static void main(String[] args) {
		/**
		 * list集合
		 * 可重复
		 * 可以通过for循环和增强for循环以及迭代器遍历
		 */
		List<String> list = new ArrayList<String>();
		list.add("111");
		list.add("111");
		list.add("111");
		list.add("111");
		System.out.println("----------list的个数---------");
		//list的大小
		System.out.println(list.size());
//		list.remove(1);//移除
		System.out.println("----------list普通循环遍历---------");
		for (int i = 0; i < list.size(); i++) {
			//遍历
			System.out.println(list.get(i));
		}
		System.out.println("----------list的迭代器---------");
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			System.out.println(string);
		}
		
		System.out.println("----------list的增强for---------");
		for (String string : list) {
			System.out.println(string);
		}
		/**
		 * hashset
		 * 特点:无序、无索引、不可重复
		 * 因为无序,不能通过下标的形式遍历,可以通过
		 */
		Set<String> set = new HashSet<String>();
		set.add("111");
		set.add("111");
		set.add("111");
		set.add("111");
		System.out.println("----------set的个数---------");
		System.out.println(set.size());
//		list.remove(1);
		System.out.println("----------set的遍历---------");
		System.out.println("----------迭代器--------");
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			System.out.println(string);
		}
		System.out.println("-----------增强for循环----------");
		for (String s : set) {
			System.out.println(s);
		}
		
		/**
		 * list可以存放对象
		 */
		List<Person> personlList = new ArrayList<Person>();
		Person person1 = new Person();
		Person person2 = new Person();
		person1.setAge(10);
		person1.setName("张三");
		person1.setScores(20);
		person2.setAge(40);
		person2.setName("李四");
		person2.setScores(30);
		personlList.add(person1);
		personlList.add(person2);
		//迭代器
		for (Iterator<Person> iterator = personlList.iterator(); iterator.hasNext();) {
			Person person = (Person) iterator.next();
			System.out.println(person);
		}
		for (Person person : personlList) {
			System.out.println(person);
		}
		
		/**
		 * Object里面可以放任何类型的数据
		 */
		List<Object> objectlList = new ArrayList<Object>();
		objectlList.add(1);
		objectlList.add("1");
		objectlList.add(person1);
		for (Iterator iterator = objectlList.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			System.out.println(object);
		}
		
		/**
		 * hashmap双列集合 一个键对应一个值;建不可重复,值可以重复
		 * put添加
		 * get获取value
		 */
	   Map<String, Object> map = new HashMap<String, Object>();
	   map.put("张三", 15);
	   map.put("李四", 18);
	   Set<String> sets = map.keySet();
	   for (String key : sets) {
		   System.out.println("key  "+ key+" value "+map.get(key));
	   }
	}
}

  

posted @ 2020-04-29 10:56  测开工程师成长之路  阅读(169)  评论(0编辑  收藏  举报