package cn.hncu.col.col;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

public class CollectionDemo {

public static void main(String[] args) {

Collection col = new ArrayList(); //addA(Object obj); --List的实现类
//Collection col = new HashSet(); //--Set的实现类--重复元素是加不进的,元素的排列序列由各元素的hashCode值来决定

//增
col.add(1);
col.add("abc");
col.add(100.123);
col.add("asd");
//col.add(1);//如果该Collection用的是Set的实现类,那么重复的元素是加不进的
col.add( new Person("Jack",22) );

//删除
//col.remove("abc");
//String str = new String("abc");
//col.remove(str);

//修改1(对于Set实现,这种方式可以。而List实现不行,顺序乱了)
//col.remove("abc");
//col.add("abc123");

//修改2
Object[] objs = col.toArray();
col.clear();
for(int i=0;i<objs.length;i++){
if(objs[i].equals("abc")){
objs[i] = "abc123";
}
col.add(objs[i]);
}

//查
//集合遍历---工具:Iterator
Iterator it = col.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}

}

}

----------------------------------------------

package cn.hncu.col.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//有序,允许重复。总的来讲: List中的功能是在Collecton的基础上还添加了“与下标索引”有关的操作
/*
* 1, List当中,元素添加的顺序和存入的顺序是一样的---跟hashCode没有关系
* 2, List当中有一些“与位置-index”有关的操作
* 3, List当中有一个ListIterator列表迭代器,既可以next(),也可以previous()----而Collection当中的Iterator只能next()
*/
public class ListDemo {

public static void main(String[] args) {
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
list.add(0,"d");//在基础上增加的---与index有关的操作

list.add(new Person("Rose",23));
list.add(new Person("Jack",25));
list.add(new Person("Jack",25));
list.add(new Person("Tom",22));

//删除
//list.remove("b");
//list.remove(2);

//修改
//list.set(3,"ccc");//修改第3个位置的元素
int index = list.indexOf("c");
list.set(index, "ccccc");

//遍历法1:用迭代器
// Iterator it = list.iterator();
// while(it.hasNext()){
// System.out.println(it.next());
// }
//遍历法2:利用与位置有关的操作,直接采用for循环
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}

}

}

----------------------------------------------------

package cn.hncu.col.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

//有序,允许重复。总的来讲: List中的功能是在Collecton的基础上还添加了“与下标索引”有关的操作
/*
* 1, List当中,元素添加的顺序和存入的顺序是一样的---跟hashCode没有关系
* 2, List当中有一些“与位置-index”有关的操作
*/
public class ListDemo2 {

public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
list.add(new Person("Jack",25));
list.add(new Person("Tom",22));

//※※LinkedList当中特有的方法
//list.addFirst("11111");
list.addLast("22222");
list.removeFirst();
//list.removeLast();
//getFirst()、getLast()

//遍历法2:利用与位置有关的操作,直接采用for循环
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}

}

------------------------------------------------------------

package cn.hncu.col.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class MyStack {
//private List list = new ArrayList();//允许重复,有序
private List list = new LinkedList();//允许重复,有序--中间元素有频繁的插入和删除,选这个

//入栈
public void push(Object obj){
list.add(obj);
}

//出栈
public Object pop(){
if(list.size()>0)
return list.remove( list.size()-1 );
else
return null;
}

//大小
public int size(){
return list.size();
}

public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push("a");
stack.push("b");
stack.push(new Person("Mike",20));
stack.push(100);

for (int i = 0; i < 5; i++) {
System.out.println(stack.pop());
}
}

}

----------------------------------------------------

package cn.hncu.col.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

//无序,不允许重复。总的来讲: Set中的全部功能都来自Collecton,自己没有添加新的功能
public class HashSetDemo {
/*
* 1,如果Person类中没写hashCode(),那么构造两个相同的Person对象也能加进Set集合,因此此时的HashCode是内存地址。而如果写了hashCode()方法,则第二个加不进。
* 2,set.add(e)方法执行时,内部会调用一次e对象的hashCode()方法(其实根据它的返回值来决定元素存放位置)
* 3,如果Person类中没写hashCode(),加入到集合中的这些元素的顺序是不确定的(因为此时各元素的位置由内存地址来决定的)
*/
public static void main(String[] args) {
Set set = new HashSet();
set.add(new String("Java"));
set.add( new Person("Jack",23) );//hashCode
set.add( new Person("Jack",23) );//加不进,相同的hashCode处,只能加入一个元素
set.add( new Person("Tom",23) );
set.add( new Person("张三",22) );
set.add(new Integer(100) );
set.add(new Double(100.123) );

//Set一般都是用迭代器(专门的查询组件)来遍历
Iterator it = set.iterator();
while(it.hasNext()){
Object obj = it.next();
//区分集合当中元素的类型 ---instanceof
if(obj instanceof Person){
System.out.println("Person对象:"+obj);
}else if(obj instanceof Integer || obj instanceof Double){
System.out.println("数值:"+obj);
}else{
System.out.println(obj);
}
}

}

}

--------------------------------------------------------------

package cn.hncu.col.sort.v1;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SortDemo1 {

public static void main(String[] args) {
//Set set = new HashSet();//HashSet是无法实现:自定义排序
Set set = new TreeSet();//TreeSet可以实现:自定义排序 ----凡是Tree都能实现自定义排序
//凡是插入TreeSet中的元素应该都是能够排序的,即要实现Comparable接口
//TreeSet中的add()方法被调用时,会自动让被添加元素去调用它的compareTo()方法以决定该元素的顺序
set.add(new Person("Jack",102));
set.add(new Person("Tom",23));
set.add(new Person("Mike",32));
set.add(new Person("张三",33));
set.add(new Person("Rose",24));

Iterator it = set.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}

}

--------------------------------------------------------------

package cn.hncu.col.sort.v2;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SortDemo2 {

public static void main(String[] args) {
//Set set = new HashSet();//HashSet是无法实现:自定义排序
Set set = new TreeSet();//TreeSet可以实现:自定义排序 ----凡是Tree都能实现自定义排序
//凡是插入TreeSet中的元素应该都是能够排序的,即要实现Comparable接口
//TreeSet中的add()方法被调用时,会自动让被添加元素去调用它的compareTo()方法以决定该元素的顺序

set.add("abc");
set.add("aaa");

set.add(new Person("Jack",102));
set.add(new Person("Tom",23));
set.add(new Person("Mike",32));
set.add(new Person("Mik",32));
set.add(new Person("张三",32));
set.add(new Person("Rose",24));

//set.add("abc");//因为String类中没有compartTo(Person)的方法,所以在这个地方添加不行。
//set.add("aaa");

Iterator it = set.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}

}

--------------------------------------------------------

package cn.hncu.col.sort.v3;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SortDemo3 {

public static void main(String[] args) {
//Set set = new TreeSet();//TreeSet是要排序的。如果是空参构造方法,那么顺序由被添加元素来决定(使用它的Comparable接口)---排序方法1
Comparator cmp= new MyCmp();
Set set = new TreeSet(cmp);//使用有Comparator参数的构造。利用cmp比较器来进行排序,不会使用被添加元素的Comparable接口
set.add("abc");
set.add("aaa");

set.add(new Person("Jack",102));
set.add(new Person("Tom",23));
set.add(new Person("Mike",32));
set.add(new Person("Mik",32));
set.add(new Person("张三",32));
set.add(new Person("Rose",24));

//set.add("abc");
//set.add("aaa");

Iterator it = set.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}

}

-------------------------------------------------

package cn.hncu.col.sort.v3;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

public class SortDemo4 {

public static void main(String[] args) {

//如果是TreeMap,是按key来排序的
Comparator cmp= new MyCmp();
Map map = new TreeMap(cmp);//使用有Comparator参数的构造。利用cmp比较器来进行排序,不会使用被添加元素的Comparable接口
// map.put(1, "abc");
// map.put(2, "aaa");

map.put(3, new Person("Jack",102));
map.put(4, new Person("Tom",23));
map.put(5, new Person("Mike",32));
map.put(6, new Person("Mik",32));
map.put(7, new Person("张三",32));
map.put(8, new Person("Rose",24));

map.put(9, "abc");
map.put(81, "aaa");

Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Entry en = (Entry) it.next();
System.out.println(en.getKey()+":"+en.getValue());
}
}

}

------------------------------------------------------------

****中文排序

package cn.hncu.col.sort.v4;

import java.util.Iterator;
import java.util.TreeMap;
import java.util.Map.Entry;

public class ChineseSort {
public static void main(String[] args) {
TreeMap map = new TreeMap( new MyCmp2() );
map.put("周平", new Person("周平",10));
map.put("张三", new Person("张三",22));
map.put("李四", new Person("李四",34));
map.put("一丁", new Person("一丁",3));

Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Entry en = (Entry) it.next();
System.out.println("key="+en.getKey()+",,,value="+en.getValue());
}
}
}

---==================--------

package cn.hncu.col.sort.v4;

import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
/**
* 中文排序的比较器
* @author <a href="mailto:729627398@qq.com">hncu_lzp</a>
* @version 1.0 2016-7-15
*/
public class MyCmp2 implements Comparator {
private Collator collator = Collator.getInstance();
@Override
public int compare(Object o1, Object o2) {

CollationKey key1 = collator.getCollationKey(o1.toString());
CollationKey key2 = collator.getCollationKey(o2.toString());
return key1.compareTo(key2);//把key1和key2交换一下,则是反序排序
}
}

----------------------------------------------------------------------

package cn.hncu.col.sort.v1;

public class Person implements Comparable{
private String name;
private int age;

public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}

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;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
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;
return true;
}

@Override
public String toString() {
return name + "," + age;
}

=====这里公用一个person类,有的要hashcode和equals方法,有的不需要。下面是因为要用到Tree。

//由该方法的返回值来决定元素this和o之后的大小
/**
* 计算:this - o
* 如果this>o 返回 >0的整数
* 如果this<o 返回 <0的整数
* 如果this==o 返回 0
*/
@Override
public int compareTo(Object o) {
//System.out.println("a.........");
//return 1; //后加的元素更大,按添加的次序依次排(输出)
//return -1; //后加的元素更小,按添加的次序的倒序排(输出)
Person p = (Person) o;
//return this.age - p.age;//按年龄升序
return p.age - this.age;//按年龄倒序
}

}

-----------------------------------------------------

package cn.hncu.col.sort.v1;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class SortDemo1 {

public static void main(String[] args) {
//Set set = new HashSet();//HashSet是无法实现:自定义排序
Set set = new TreeSet();//TreeSet可以实现:自定义排序 ----凡是Tree都能实现自定义排序
//凡是插入TreeSet中的元素应该都是能够排序的,即要实现Comparable接口
//TreeSet中的add()方法被调用时,会自动让被添加元素去调用它的compareTo()方法以决定该元素的顺序
set.add(new Person("Jack",102));
set.add(new Person("Tom",23));
set.add(new Person("Mike",32));
set.add(new Person("张三",33));
set.add(new Person("Rose",24));

Iterator it = set.iterator();
while(it.hasNext()){
Object obj = it.next();
System.out.println(obj);
}
}

}

--------------------

集合(Collection,set,list,map)的更多相关文章

  1. 5、数组和集合--Collection、Map

    一.数组:同一个类型数据的集合,其实他也是一个容器 1.数组的好处:可以自动给数组中的元素从0开始编号,方便操作这些数据 2.数组的定义: 在Java中常见: 格式1:  类型 [] 数组名 = ne ...

  2. Java集合排序及java集合类详解--(Collection, List, Set, Map)

    1         集合框架 1.1         集合框架概述 1.1.1         容器简介 到目前为止,我们已经学习了如何创建多个不同的对象,定义了这些对象以后,我们就可以利用它们来做一 ...

  3. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  4. collection(list,set,map)集合详解

    一:java集合的体系结构如下: Java集合大致分为Set.List.Queue.Map四个体系 .Collection: List和Set,Queue继承自Collection接口. |--Lis ...

  5. 「 深入浅出 」java集合Collection和Map

    本系列文章主要对java集合的框架进行一个深入浅出的介绍,使大家对java集合有个深入的理解. 本篇文章主要具体介绍了Collection接口,Map接口以及Collection接口的三个子接口Set ...

  6. Java集合 Collection、Set、Map、泛型 简要笔记

    集合 什么是集合 概念 对象的容器,实现了对对象常用的操作 和数组的区别 数组长度固定,集合长度不固定 数组可以存储基本类型和引用类型,集合只能存储引用类型 位置 java.util.*; Colle ...

  7. Guava库介绍之集合(Collection)相关的API

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  8. 【再探backbone 02】集合-Collection

    前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...

  9. Java 集合系列09之 Map架构

    概要 前面,我们已经系统的对List进行了学习.接下来,我们先学习Map,然后再学习Set:因为Set的实现类都是基于Map来实现的(如,HashSet是通过HashMap实现的,TreeSet是通过 ...

  10. java学习——集合框架(泛型,Map)

    泛型: ... Map:一次添加一对元素.Collection 一次添加一个元素. Map也称为双列集合,Collection集合称为单列集合. 其实map集合中存储的就是键值对. map集合中必须保 ...

随机推荐

  1. C++中为什么要用虚函数、指针或引用才能实现多态?

    原文链接:http://blog.csdn.net/zoopang/article/details/14071779 学过C++的都知道,要实现C++的多态性必须要用到虚函数,并且还要使用引用或者指针 ...

  2. Layout Resource官方教程(1)简介

    Layout Resource SEE ALSO Layouts A layout resource defines the architecture for the UI in an Activit ...

  3. Mysql表的七种引擎类型,InnoDB和MyISAM引擎对比区别总结

    InnoDB和MyISAM区别总结 我用MySQL的时候用的是Navicat for MySQL(Navicat for mysql v9.0.15注册码生成器)操作库.表操作的,默认的表就是Inno ...

  4. 处理Selection对象和Range对象——Word VBA中重要的两个对象

    处理Selection对象和Range对象——Word VBA中重要的两个对象 Word 开发人员参考Selection 对象代表窗口或窗格中的当前所选内容.所选内容代表文档中选定(或突出显示)的区域 ...

  5. dev combobox edit 怎么设置让选项清空

    dev combobox edit 怎么设置让选项清空 功能需求: 点击combobox edit1的选项A 使得 combobox edit2出现选项a: 然后再点击combobox edit1的选 ...

  6. POJ 1185 (状态压缩DP)

    中文题目,题意就不说了. 不得不说这是一道十分经典的状态压缩DP的题目. 思路: 通过分析可以发现,第i行的格子能不能放大炮仅与第i-1和i-2行的放法有关,而与前面的放法无关,因此,如果我们知道了i ...

  7. Unity3d 巫师3Ciri的渲染

    --wolf96 16/10/6

  8. 求正整数n所有可能的和式的组合(如;4=1+1+1+1、1+1+2、1+3、2+1+1、2+2

    作者:张小二 nyoj90 ,可以使用递归的方式直接计算个数,也可以通过把满足的个数求出来计数,因为在juLy博客上看到整数划分,所以重写了这个代码,就是列出所m的可能性,提交后正确.acmer的入门 ...

  9. openstack 镜像自动扩容 resize拉伸

    The simplest way to support this in your image is to install the cloud-utils package (contains the g ...

  10. mongodb 在windows上安装为服务

    // mongo 也是先安装扩展 在安装为服务 首先 先下载mongodb的压缩包 解压(一个bin文件夹 三个文件) 在目录下 新建立一文件夹 mongodb 然后将解压的内容放进去 然后在mong ...