Person类:

package com.yfs.javase;

import java.util.Date;

public class Person implements Comparable {

	private String name;
private int age;
private char sex;
public static Date date = new Date(); @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + sex;
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex != other.sex)
return false;
return true;
} public Person() {
} public Person (String n) {
this.name = n;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public char getSex() {
return sex;
} public void setSex(char sex) {
this.sex = sex;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
} public Person(String name, int age, char sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
} @Override
public int compareTo(Object o) {
Person p = (Person)o;
return p.age - age;
} }

1.set

package com.yfs.javase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet; public class Demo1 { /**
* Set
*/
public static void main(String[] args) {
// LinkedHashSet lSet = null;//增加 删除
// HashSet hSet = null;//查找
// TreeSet tSet = null;//排序
// useLinkedSet();
//useTreeSet();
useArrayList();
} public static void useArrayList() {
ArrayList list = new ArrayList();//必须覆盖equals方法
Person p = new Person("张三",20,'男');
list.add(p);
list.add(new Person("李四",23,'女'));
list.add(new Person("王五",22,'男'));
list.add(new Person("王Z",21,'女'));
list.add(p);
System.out.println("是否有张三:" + list.contains(p));//指向同一对象
System.out.println("是否有张三:" + list.contains(new Person("张三",20,'男')));
System.out.println("元素:" + list); } public static void useTreeSet() {
TreeSet tSet = new TreeSet();
tSet.add("fff");
tSet.add("ccc");
tSet.add("aaa");
tSet.add("ddd");
tSet.add("aaa");
System.out.println("元素个数: " + tSet.size());
System.out.println("元素: " + tSet); } public static void useLinkedSet() {
LinkedHashSet lSet = new LinkedHashSet();
lSet.add("fff");
lSet.add("ccc");
lSet.add("aaa");
lSet.add("ddd");
lSet.add("aaa");
System.out.println("元素个数: " + lSet.size());
System.out.println("元素: " + lSet); } }

2.Mycompare类

package com.yfs.javase;

import java.util.Comparator;
//外部比较器
public class Mycompare implements Comparator { @Override
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;
return p1.getAge() - p2.getAge();//使用年龄排序
} }

使用比较器:

package com.yema.javase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet; public class Demo2 { /**
* Set
*/
public static void main(String[] args) { //useLinkedSet();
useTreeSet();
// useArrayList();
} public static void useArrayList() {
ArrayList list = new ArrayList();//必须覆盖equals方法
Person p = new Person("张三",20,'男');
list.add(p);
list.add(new Person("李四",23,'女'));
list.add(new Person("王五",22,'男'));
list.add(new Person("王Z",21,'女'));
list.add(p);
System.out.println("是否有张三:" + list.contains(p));//指向同一对象
System.out.println("是否有张三:" + list.contains(new Person("张三",20,'男')));
System.out.println("元素:" + list); } public static void useTreeSet() {
// Mycompare com = new Mycompare();//使用比较器
// TreeSet tSet = new TreeSet(com);//必须覆盖hashcode equals方法
TreeSet tSet = new TreeSet();//必须覆盖hashcode equals方法
Person p = new Person("张三",20,'男');
tSet.add(p);
tSet.add(new Person("李四",23,'女'));
tSet.add(new Person("王五",22,'男'));
tSet.add(new Person("王Z",21,'女'));
tSet.add(p);
System.out.println("是否有张三:" + tSet.contains(p));//指向同一对象
System.out.println("是否有张三:" + tSet.contains(new Person("张三",20,'男')));
System.out.println("元素:" + tSet); } public static void useLinkedSet() {
LinkedHashSet lSet = new LinkedHashSet();//必须覆盖hashcode equals方法
Person p = new Person("张三",20,'男');
lSet.add(p);
lSet.add(new Person("李四",23,'女'));
lSet.add(new Person("王五",22,'男'));
lSet.add(new Person("王Z",21,'女'));
lSet.add(p);
System.out.println("是否有张三:" + lSet.contains(p));//指向同一对象
System.out.println("是否有张三:" + lSet.contains(new Person("张三",20,'男')));
System.out.println("元素:" + lSet); } }

3.泛型

package com.yfs.javase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random; public class Demo3 { /**
* 泛型
*/
public static void main(String[] args) {
// useArrayList();
useCollection(); } private static void useCollection() {
// 指定集合放入对象类型
Collection<Person> col = new HashSet<Person>();
col.add(new Person("张三", 20, '男'));
col.add(new Person("李四", 23, '女'));
col.add(new Person("王五", 22, '男'));
col.add(new Person("王Z", 21, '女'));
// list.add(new Random()); 其他类型对象不能放入 System.out.println("元素:" + col);
// Person p = (Person)list.get(4);
// Object obj = list.get(4);
// 取值不考虑类型 迭代器泛型
Iterator<Person> it = col.iterator();
while (it.hasNext()) {
Person p = it.next();
System.out.println(p);
} } public static void useArrayList() {
// 指定集合放入对象类型
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("张三", 20, '男'));
list.add(new Person("李四", 23, '女'));
list.add(new Person("王五", 22, '男'));
list.add(new Person("王Z", 21, '女'));
// list.add(new Random()); 其他类型对象不能放入 System.out.println("元素:" + list);
// Person p = (Person)list.get(4);
// Object obj = list.get(4);
Person p = list.get(2);// 取值不考虑类型 } }

4.HashMap

package com.yfs.javase;

import java.util.HashMap;
import java.util.Map;
import java.util.Random; public class MapDemo1 { /**
* @param args
*/
public static void main(String[] args) {
Map map = new HashMap();
map.put(1, new Person());//put添加对象key value
map.put(2, new Random());
map.put("zhangsan", "1388888888");
map.put("lisi", "139999999");
map.put("zhangsan", "1300000000");
System.out.println("元素个数 : " + map.size());
System.out.println("元素: " + map);
System.out.println(map.get(1));
System.out.println(map.get("lisi"));
System.out.println("是否有zhangsan key :" + map.containsKey("zhangsan"));
System.out.println("是否有139999999号码:" + map.containsValue("139999999")); } }

5.Map(Iterator)实体、key的集合

package com.yfs.javase;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; public class MapDemo2 { /**
* @param args
*/
public static void main(String[] args) {
//useMap();
//useMapEntry();
//Date d = Person.date;
useKey(); } public static void useMapEntry() {
Map<String,String> map = new HashMap<String,String>();
map.put("zhangsan", "1388888888");
map.put("lisi", "139999999");
map.put("zhang", "1300000000");
map.put("wangwu", "1311111111");
map.put("Tom", "1583333333333");
map.put("Jack", "133333333333"); Set<Map.Entry<String, String>> set = map.entrySet();//===================不同 for(Iterator<Map.Entry<String, String>> it = set.iterator(); it.hasNext();){
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
String value= entry.getValue();
System.out.println(key + " === > " + value);
} } public static void useKey() {
Map<String,String> map = new HashMap<String,String>();
map.put("zhangsan", "1388888888");
map.put("lisi", "139999999");
map.put("zhang", "1300000000");
map.put("wangwu", "1311111111");
map.put("Tom", "1583333333333");
map.put("Jack", "133333333333");
System.out.println("元素个数 : " + map.size());
System.out.println("元素: " + map); //所有key 集合
System.out.println("==========key + value 集合==========");
Set set = map.keySet();//========================================不同
for(Iterator<String> it = set.iterator(); it.hasNext();){
String key = it.next();//keyjihe
System.out.print(key + " +++ ");
String value = map.get(key);// mapjihe
System.out.print(value + "\n");
}
}
}

java新手笔记31 集合实现类的更多相关文章

  1. java新手笔记30 集合

    1.set/list package com.yfs.javase; import java.util.ArrayList; import java.util.Collection; import j ...

  2. Java学习笔记之---集合

    Java学习笔记之---集合 (一)集合框架的体系结构 (二)List(列表) (1)特性 1.List中的元素是有序并且可以重复的,成为序列 2.List可以精确的控制每个元素的插入位置,并且可以删 ...

  3. java学习笔记07--日期操作类

    java学习笔记07--日期操作类   一.Date类 在java.util包中定义了Date类,Date类本身使用非常简单,直接输出其实例化对象即可. public class T { public ...

  4. Java学习笔记31(IO:Properties类)

    Properties类,表示一个持久的j集,可以存在流中,或者从流中加载 是Hashtable的子类 map集合的方法都能用 用途之一:在开发项目中,我们最后交给客户的是一个编译过的class文件,客 ...

  5. 【原】Java学习笔记028 - 集合

    package cn.temptation; import java.util.HashSet; import java.util.Set; public class Sample01 { publi ...

  6. 【原】Java学习笔记026 - 集合

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:从三国演义中 ...

  7. Java学习笔记之集合

    集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java -- 面向对象 -- 操作很多对象 -- 存储 -- 容器(数组和StringBuffer) -- 数组而数组的长度固定 ...

  8. java学习笔记之日期日历类

    java学习笔记之日期日历 Date日期类概述: 表示特定的瞬间,精确到毫秒 Date类的构造方法: 1.空参数构造方法 Date date = new Date(); 获取到当前操作系统中的时间和日 ...

  9. java学习笔记之集合家族2

    集合体系 一.数据结构 List集合储存数据结构 <1>堆栈结构 特点:先进后出 <2>队列结构 特点:先进先出 <3>数组结构 特点:查询快,增删慢 <4& ...

随机推荐

  1. Amazon DynamoDB 概览

    1. 什么是Amazon DynamoDB DynamoDB 是一种快速.全面受管的 NoSQL 数据库服务,它能让用户以简单并且经济有效地方式存储和检索任何数据量,同时服务于任何程度的请求流量.所有 ...

  2. Delphi 7 升级到 Delphi 2010 总结

    1 字符串 >>string =unicodeString 字母的处理要定义AnsiString了 >>PChar =PWidechar >>str='普通汉字' ...

  3. 动态SQL使用小结

    1.什么是动态SQL? 静态 SQL:静态 SQL 语句一般用于嵌入式 SQL 应用中,在程序运行前,SQL 语句必须是确定的,例如 SQL 语句中涉及的列名和表名必须是存在的.静态 SQL 语句的编 ...

  4. 在 Fedora 里安装自带的 MATE和 cinnamon

    参见  http://wiki.mate-desktop.org/download#fedora安装方法: yum groupinstall mate-desktop yum groupinstall ...

  5. js自动提交按钮

    document.forms['alipaysubmit'].submit(); <form id='alipaysubmit' name='alipaysubmit' action='' me ...

  6. tomcat 详解五 tomcat页面设置访问权限

    转自:http://blog.knowsky.com/191233.htm 在web应用中,对页面的访问控制通常通过程序来控制,流程为:登录 -> 设置session -> 访问受限页面时 ...

  7. jboss加密敏感信息

    默认情况下,我们配置在domain.xml或host.xml文件中的信息都是明文,对一些敏感信息就显得安全性不够,可以使用jboss提供的vault机制来进行加密 下面的内容来自 http://www ...

  8. hdu 4340 树状DP

    思路:我们定义两个数组,ant[Maxn][2],bob[Maxn][2].ant[i][0]表示还未确定哪个城市被全费用占领,ant[i][1]表示确定了哪个城市被全费用占领.那么ant[i][0] ...

  9. 命令行创建Windows窗体应用程序

    csc:(C Sharp Compiler) 类似于 javac (java Compiler) 命令行的编译工具 位置:C:\Windows\Microsoft.NET\Framework\v4.0 ...

  10. Java Concurrency - ScheduledThreadPoolExecutor

    The Executor framework provides the ThreadPoolExecutor class to execute Callable and Runnable tasks ...