您的位置:首页 > 编程语言 > Java开发

java Collection 个人记录和记忆

2017-04-08 14:33 253 查看
import java.util.*;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;

/**
* Created by Kodulf on 2017/4/6.
*/
public class CollectionTest {

/**
* 苍老师mv(c collection,l list,s set,m,map,v vector
* 钻石无或配不上,咋啊双人床无或配不上,挨扁不用初中生
* list 你(list)瞎掰,挨g人是sb,来了挨g人服了。
* set,谁,嘿嘿等顺序,停车c大吼排嘘。
*
*
* 钻石无或配不上,咋啊双人床无或配不上,挨扁不用初中生
* c 钻石无或配不上,z增 增加add,addAll,s删除,remove,clear,无或 没有获得,p配 判断isEmpty,b不 遍历Iterator,s上 转数组
* 咋啊双人床无或配不上,遍历的时候不能够在遍历的里面使用collection进行增删,必须使用iterator进行
* 在Iterator 遍历里面,不要用collection的对象进行增删,挨扁不用初中生,(ai Iterator,b遍历,不用,collection进行 z增s删)
*
*
* list 你(list)瞎掰,挨锅人是sb,来了挨锅人服了。
* agrssb 瞎掰,挨锅人是sb。增删获更遍历都可以通过下标来的,ListIterator 可以在里面直接进行遍历的时候进行add和set。
* List: 是有顺序的,分为ArrayList(列表) 和 LinkedList(链表)
* List 都是有下表的,增删获更遍历都可以通过下标来的。arsgsb 阿仁是个sb。
* ListIterator 可以在里面直接进行遍历的时候进行add和set,
* LinkedList 来了挨锅人服了。linkedlist来了,add挨,get锅,remove人,fisrt服,last了
* list 里面判断是否包含的contains 是通过equals 来判断的,如果是String就是内容,如果是对象就是地址,基本数据类型就是值
*
* set,谁,嘿嘿,等顺序,停车c大吼排嘘。(车c compareTo,comparator)
* HashSet,TreeSet
* 首先并不是都是要比较equals的方法的,
* hashset 首先比较的是hashcode,如果hashcode相等,那么在判断equals
* treeset,比较的只有是compareTo,comparator,带有排序的功能,但是必须是相同的类,不然如果出现了Student和String,那么就不好辨识了。除非重写Student的compareto方法
* compareTo里面使用的是本地的减去被比较的,如果大于说明就是要排在被比较的后面,一般都是和前面的进行比较的,所以如果大于,那么输出的时候直接就是在后面了,如果将某一个类的compareTo设置为1那么后面添加的一定排在前面添加的后面
* 大吼排嘘,大的放在后面,treeset可以利用这点进行排嘘
*
*
*
*
*
* @param args
*/
public static void main(String[] args){

testCollection();

testList();

testAddAndRemoveIterator();

testAddAndRemoveListIterator();

testVector();

testLinkedList();

testMoNiDuiZhan();

testRemoveDuty();

testCeshiDuixiangEquals();

testHashSet();

testTreeSetWithComparator();

testHashSetAndTreeSet();

testHashOrder();

}

/**
* 注意了Collection, Collections 是不同的Collections 是工具类,可以用来进行排序的
* 钻石配不上,z增,s删除,p判断,b遍历,s数组
*/
private static void testCollection() {

Collection collection  = new ArrayList();
collection.add("aa");
collection.add("bb");
collection.add("cc");
collection.add(99);

System.out.println(collection);
Collection coll2 = new ArrayList();
coll2.add("dd");
coll2.add("ee");

collection.addAll(coll2);
System.out.println(coll2);
System.out.println(collection);

//删除
collection.remove(99);
coll2.clear();
System.out.println(collection);

//判断
System.out.println(collection.contains(99));
System.out.println(collection.isEmpty());
System.out.println(collection.size());

//遍历
Iterator iterator = collection.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
System.out.println(next);
}

//集合转数组
Object[] objects = collection.toArray();
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
}

/**
* list的特点,可以带下标,也可以通过下表遍历
*/
public static void testList(){
List col = new ArrayList();
col.add("aa");
col.add("bb");
col.add("cc");
col.add(0,"00");

System.out.println(col);

col.remove(0);
System.out.println(col);

col.set(2,"22");
System.out.println(col);

System.out.println(col.get(2));

System.out.println(col.subList(0,1));

Iterator iterator = col.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}

for (int i = 0; i < col.size(); i++) {
System.out.println(col.get(i));
}

while (!col.isEmpty()){
System.out.println(col.remove(0));
}
}

/**
* 测试list的iterator
*  在迭代的过程中使用集合对集合中的对象进行添加,删除,例如:col.remove("cc"); 修改会发生ConcurrentModificationException
*/
public static void testAddAndRemoveIterator(){
List col = new ArrayList();
col.add("aa");
col.add("bb");
col.add("cc");

Iterator iterator = col.iterator();
while (iterator.hasNext()){
Object next = iterator.next();
if("cc".equals(next)){
System.out.println(next);
try {
iterator.remove();
}catch (Exception e){
e.printStackTrace();
}
}else {
System.out.println(next);
}
}
System.out.println(col);
}

/**
*  在迭代的过程中使用集合对集合中的对象进行添加,删除,例如:col.remove("cc"); 修改会发生ConcurrentModificationException
*  相比于Iterator ListIterator 可以进行添加
*/
public static void testAddAndRemoveListIterator(){
List col = new ArrayList();
col.add("aa");
col.add("bb");
col.add("cc");

ListIterator listIterator = col.listIterator();
while (listIterator.hasNext()){
Object next = listIterator.next();
if("cc".equals(next)){
//在迭代的过程中使用集合对集合中的对象进行添加,删除,例如:col.remove("cc"); 修改会发生ConcurrentModificationException
//col.remove("cc");

listIterator.remove();
//相比于Iterator ListIterator 可以进行添加
listIterator.add("hahhahahah ");
}else {
System.out.println(next);
}
}

System.out.println(col);
}

public static void testVector(){
Vector v = new Vector();
v.add("haha");
v.add("hehe");
v.add("xixi");

Enumeration elements = v.elements();
while (elements.hasMoreElements()){
System.out.println(elements.nextElement());
}
}

public static void testLinkedList(){
LinkedList linkedList = new LinkedList();
linkedList.add("11");
linkedList.add("22");
linkedList.add("33");
linkedList.add("44");

linkedList.addFirst("first");
linkedList.addLast("last");
System.out.println(linkedList);

System.out.println(linkedList.getFirst());
System.out.println(linkedList.getLast());

linkedList.removeFirst();
linkedList.removeLast();
System.out.println(linkedList);

linkedList.offerFirst("offerFirst");
linkedList.offerLast("offerLast");

System.out.println(linkedList.peekFirst());
System.out.println(linkedList.peekLast());

Object o = linkedList.pollFirst();
linkedList.pollLast();
System.out.println(linkedList);
}

/**
* 模拟堆栈
*/
public static void testMoNiDuiZhan(){
Dui dui = new Dui();
dui.in("1");
dui.in("2");
dui.in("3");
dui.chu();
System.out.println(dui.linkedList);

Zhan zhan = new Zhan();
zhan.in("1");
zhan.in("2");
zhan.in("3");
zhan.chu();
System.out.println(zhan.linkedList);
}

/**
* 队,先进先出,addLast,getFirst
*/
static class Dui{
LinkedList linkedList = new LinkedList();

public Dui(){

}

public void in(Object object){
linkedList.addLast(object);
}

public Object chu(){
Object ret= new Object();
if(linkedList.size()>0)
ret = linkedList.removeFirst();
return ret;
}

}

/**
* 栈,先进后出,addLast,getLast
*/
static class Zhan{
LinkedList linkedList = new LinkedList();
public Zhan(){

}

public void in(Object object){
linkedList.addLast(object);
}

public Object chu(){
Object ret= new Object();
if(linkedList.size()>0)
ret = linkedList.removeLast();
return ret;
}
}

/**
* 去除重复的数据
*/
public static void testRemoveDuty(){
ArrayList arrayList = new ArrayList();
arrayList.add("hello world");
arrayList.add("bbbbbbbbbbb");
arrayList.add("bbbbbbbbbbb");
arrayList.add("bbbbbbbbbbb");

ArrayList newArrayList = quchu(arrayList);

System.out.println(newArrayList);

quchuwithIterator(arrayList);
System.out.println("测试final 修饰的对象");
System.out.println(arrayList);
}

/**
* 去除重复的
* @param arrayList
* @return
*/
private static ArrayList quchu(ArrayList arrayList) {
ArrayList newArrayList = new ArrayList();

for (int i = 0; i < arrayList.size(); i++) {
if(newArrayList.contains(arrayList.get(i))){

}else{
newArrayList.add(arrayList.get(i));
}
}
return newArrayList;
}

/**
* 去除重复的,如果使用Iterator 的话,那么原来的ArrayList 也会改变的的
* 这个就是值传递里面的对象,地址不变,但是里面的内容会改变的,原来的对象相当于是用final 修饰了,虽然地址不变,但是里面的内容是可以改变的
* @param arrayList
* @return
*/
private static ArrayList quchuwithIterator(ArrayList arrayList) {
ArrayList newArrayList = new ArrayList();

ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()){
Object next = listIterator.next();
listIterator.remove();
}
return newArrayList;
}

private static void testCeshiDuixiangEquals(){
ArrayList<Student> arrayList = new ArrayList();
arrayList.add(new Student(12,"Hellen"));
arrayList.add(new Student(13,"Hellen3"));
arrayList.add(new Student(14,"Hellen4"));
arrayList.add(new Student(15,"Hellen5"));
arrayList.add(new Student(12,"Hellen"));
arrayList.add(new Student(12,"Hellen"));

ArrayList quchu = quchu(arrayList);
System.out.println(quchu);

}

static class Student implements Comparable{

Student(int age,String name){
this.age  = age;
this.name = name;
}

int age;
String name;

/**
* 如果不重写hashcode,那么hashset里面添加的时候是有问题的,就是即使equals的方法重写了,但是没有重写hashcode 也是没有用到的
* 但是ArrayList的contains里面值判断equals的方法
* @return
*/
@Override
public int hashCode() {
System.out.println("hashcode");
int i = this.age + (int) (Math.random() * 100);
System.out.println(i);
return i;
}

@Override
public boolean equals(Object obj) {

boolean ret = false;

if(obj instanceof Student){
Student target = (Student) obj;
if(target.age == this.age && StringUtil.isNotEmpty(target.name)&&StringUtil.isNotEmpty(this.name)&&this.name.equals(target.name)){
ret = true;
}
System.out.println("比较里面的:"+(ret?"相同":"不相同"));
}
System.out.println(ret?"相同":"不相同");
return ret;
}

@Override
public String toString() {

return "age="+this.age+" name="+this.name;
}

@Override
public int compareTo(Object o) {
int ret = 0;
if(o instanceof Student){
Student o1 = (Student) o;
ret = this.age - o1.age;
if(ret == 0){
ret = this.name.compareTo(o1.name);
}
}
System.out.println(ret);
return 0;
}
}

/**
* 测试hashset,注意了,这里面如果使用上面的重写equals的Student的,那么还是不会过滤掉这里面的多余的Student,除非重写了hashcode的方法
* HashSet吗,hash吗,所以要判断has,
*/
public static void testHashSet(){
HashSet hashSet = new HashSet();

hashSet.add("hello");
hashSet.add("hello1");
hashSet.add("hello");
hashSet.add("hello");

hashSet.add(new Student(12,"hello12"));
hashSet.add(new Student(12,"hello12"));
hashSet.add(new Student(12,"hello12"));
hashSet.add(new Student(12,"hello12"));

System.out.println(hashSet);
}

/**
* 测试treeset,默认的会进行排序的
*/
public static void testTreeSet(){
TreeSet treeSet = new TreeSet();

treeSet.add("123456789");
treeSet.add("1234567892");
treeSet.add("1234567891");
treeSet.add("1234567895");
treeSet.add("1234567893");

System.out.println(treeSet);
}

/**
* Tree 相关的都是要进行比较的,两种方式:
* 1:是被比较的类实现了Comparable的接口,
* 2:自定义一个Comparator去实现Comparator
*/
public static void testTreeSetWithComparator(){
MyComparator myComparator = new MyComparator();

//方法一:被比较的类实现Comparable的接口
TreeSet treeSet = new TreeSet();

//TreeSet treeSet = new TreeSet(myComparator);

treeSet.add(new Student(12,"jack1"));
treeSet.add(new Student(12,"jack1"));
treeSet.add(new Student(12,"jack3"));
treeSet.add(new Student(12,"jack4"));
treeSet.add(new Student(12,"jack5"));

System.out.println(treeSet);
}

static class MyComparator implements Comparator<Student> {

@Override
public int compare(Student o1, Student o2) {

//首先比较age,然后比较name
int ret = 0;
ret = o1.age -o2.age;
if(ret ==0){
ret = o1.name.compareTo(o2.name);
}
return ret;
}

}

//
static void testHashSetAndTreeSet(){
HashSet hashSet = new HashSet();
hashSet.add("000");
hashSet.add("234");
hashSet.add("345");
hashSet.add("000");
hashSet.add(new Student(12,"12"));
hashSet.add(new Student(12,"122"));
hashSet.add(new Student(12,"1222"));
hashSet.add(new Student(12,"12222"));
hashSet.add(new Student(12,"122222"));
System.out.println(hashSet);

System.out.println("————————————————————");
TreeSet treeSet = new TreeSet();
//        treeSet.add("098");
//        treeSet.add("099");
//        treeSet.add("100");
//        treeSet.add("096");
//        treeSet.add("092");
treeSet.add(new Student(12,"222222"));
treeSet.add(new Student(12,"122"));
treeSet.add(new Student(12,"1222"));
treeSet.add(new Student(12,"12222"));
treeSet.add(new Student(12,"122222"));
System.out.println(treeSet);
}

/**
* 测试hash的排列的顺序
*/
private static void testHashOrder() {
TreeSet hashSet = new TreeSet();

hashSet.add("abc");
hashSet.add("1");
hashSet.add("12");
hashSet.add("890");
hashSet.add("bcd");
System.out.println(hashSet);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: