Java教程

JAVA集合训练代码以及注释(自己看的)

本文主要是介绍JAVA集合训练代码以及注释(自己看的),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

LinkedList

package June11;
import java.util.*;
public class test {

	public static void main(String[] args) {
		List<String> list = new LinkedList<String>();
		list.add("123");
		list.add(0, null);
		for(int i = 0;i < list.size();i ++) {
			System.out.print(list.get(i) + " ");
		}
		
		//ListIterator<T> 是一个泛型。泛型T是不可以缺少的
		//list.listIterator()返回的是ListIterator迭代器
		//list.iterator()返回的是Iterator迭代器
		//两个迭代器的类型是不一样的,可以通过强制转换将iterator转换成listiterator
		ListIterator<String> iterator =  list.listIterator();
		iterator.next();
		iterator.add("12");
		for(int i = 0;i < list.size();i ++) {
			System.out.print(list.get(i) + " ");
		}
		//previous与remove的组合删除的是迭代器右边的元素
		iterator.previous();
		iterator.remove();
		for(int i = 0;i < list.size();i ++) {
			System.out.print(list.get(i) + " ");
		}
		//next与remove的组合删除的是迭代器左边的值
		iterator.next();
		iterator.remove();
		for(int i = 0;i < list.size();i ++) {
			System.out.print(list.get(i) + " ");
		}
		//反向遍历LinkedList
		iterator.add("111");
		while(iterator.hasPrevious()) {
			System.out.print(iterator.previous() + " ");
		}
		//正向反向同时遍历LinkedList
		//由于没有对LinkedList做修改,即不会出现ConcurrentModificationException异常
		ListIterator<String> iterator_error = list.listIterator();
		System.out.println();
		while(iterator.hasNext()) {
			iterator.next();
		}
		while(iterator_error.hasNext() && iterator.hasPrevious()) {
			System.out.println("iterator_error`s value is  " + iterator_error.next() + " iterator`s value is " + iterator.previous());
		}
	}

}

散列集的实现(个人认为大体是这么实现的)

package IteratorStudy;

import javax.swing.*;
import java.util.*;
import java.util.function.IntFunction;
import java.lang.*;

public class ReviewIterator {
    public static void main(String[] args){
        LinkedList<String>[] a = new LinkedList[100];
        for(int i = 0;i < 100;i ++) {
            a[i] = new LinkedList<>();
        }
        String[] s = new String[3];
        s[0] = "abc";
        s[1] = "abc";
        s[2] = "acd";
        for(int i = 0;i < s.length;i ++) {
            int code = 0;
            for(int j = 0;j < s[i].length();j ++) {
                code += (int)s[i].charAt(j) * (int)s[i].charAt(j);
            }
            int index = code % 100;
//            System.out.println(index);
            ListIterator<String> iter = a[index].listIterator();
            //System.out.println(iter.hasNext());
            if(iter.hasNext() == false) {
                iter.add(s[i]);
                continue;
            }
            boolean flag = false;
            while(iter.hasNext()) {
                if(iter.next().equals(s[i])){
                    flag = true;
                    break;
                }
            }
            if(flag == false) iter.add(s[i]);
        }
        for(int i = 0;i < 100;i ++) {
            ListIterator aIter = a[i].listIterator();
            while(aIter.hasNext()) {
                System.out.println(aIter.next() + " ");
            }
        }
    }
}

HashSet

package IteratorStudy;

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

public class StudyHashset {
    public static void main(String[] args){
        /*HashSet<Integer> a = new HashSet<Integer>(100,0.75F);

        Integer[] s = new Integer[10];
        for(int i = 0;i < s.length;i ++) s[i] = 0;
        s[1] = 123;
        s[2] = 234;
        s[3] = 123;

        for(int i = 0 ;i < s.length;i ++) {
            a.add(s[i]);
            System.out.println(s[i].hashCode());
        }

        Iterator iterator = a.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }*/
        String[] s1 = new String[10];
        HashSet<String> hashSet = new HashSet<String>();

        for(int i = 0;i < s1.length;i ++) s1[i] = new String();
        s1[0] = "aac";
        s1[1] = "abc";
        s1[2] = "acc";
        s1[3] = "adc";
        for(int i = 0;i < s1.length;i ++) {
                hashSet.add(s1[i]);
                System.out.println(s1[i].hashCode());
        }
        Iterator i1 = hashSet.iterator();
        while(i1.hasNext()) System.out.println(i1.next());
    }
}

TreeSet

package IteratorStudy;

import java.util.*;

public class StudyTreeSet {
    public static void main(String[] args){
        SortedSet<Integer> sortedSet = new TreeSet<>();

        sortedSet.add(1);
        sortedSet.add(2);
        sortedSet.add(3);

     //   System.out.println(sortedSet.first() + " " + sortedSet.last());

        NavigableSet<Integer> navigableSet = new TreeSet();
        navigableSet.add(1);
        navigableSet.add(1);
        navigableSet.add(2);
  //      System.out.println(navigableSet.higher(3));
        //      System.out.println(navigableSet.lower(2));
        Iterator iterator = navigableSet.descendingIterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

Queue

package IteratorStudy;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;
import java.util.concurrent.DelayQueue;

public class StudyQueue {
    public static void main(String[] args){
        Queue queue = new ArrayDeque();
        queue.add(1);
        queue.add(2);
        queue.offer(2);
        queue.offer(3);
  //      System.out.println(queue.offer(4));
        while(queue.isEmpty() == false){
   //         System.out.println(queue.peek());
            queue.remove();
        }


        Deque deque = new ArrayDeque();
        deque.offerFirst(1);
        deque.offerFirst(2);
        deque.offerLast(3);
        System.out.println(deque.peekFirst() + " " + deque.peekLast());
        while(deque.isEmpty() == false) System.out.println(deque.pollFirst());
    }
}

优先级队列

package IteratorStudy;

import java.util.PriorityQueue;

public class StudyPriorityQueue {
    public static void main(String[] args){
        PriorityQueue<String> priorityQueue = new PriorityQueue<>();

        priorityQueue.add("Abc");
        priorityQueue.add("121");

    /*    for(String item : priorityQueue) {
            System.out.println(item);
        }*/
        PriorityQueue<Integer> integerPriorityQueue = new PriorityQueue<>();
        integerPriorityQueue.add(1);
        integerPriorityQueue.add(2);
        integerPriorityQueue.add(-1);
        while(!integerPriorityQueue.isEmpty()) {
            System.out.println(integerPriorityQueue.poll());
        }
    }

}

Map三种视图

package IteratorStudy;

import java.util.*;

public class StudyMap {
    public static void main(String[] args){
        Map<String,Integer> map = new HashMap<>();

        map.put("a",65);
        map.put("b",66);
        //键集视图
        /*
        Set set = map.keySet();

        Iterator iterator = set.iterator();

        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
         */

        //值集
        /*
        Collection collection = map.values();
        Iterator iterator = collection.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        */

        //键/值对
        /*
        Set set = map.entrySet();

        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        */
    }
}

链接散列集

 Map<String,Integer> ch = new LinkedHashMap<>(128,0.75F,true){
            @Override
            protected boolean removeEldestEntry(Map.Entry<String, Integer> eldest) {
                return size() > 2;
            }
        };

        ch.put("1",2);
        ch.put("2",3);
        ch.put("3",4);

        Set set = ch.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }

轻量级集合包装器

package IteratorStudy;

import java.util.*;

public class StudyBaoZhuangQi {
    public static void main(String[] args){
       /* Tree[] tree = new Tree[2];
        System.out.println(tree[1]);
        if(tree[1] == null) System.out.println("YES");
        //将tree数组转换成实现了List接口方法的一个对象
        //这个对象的所有操作都是对原来数组的操作。
        //一切对于数组大小规模的操作都是不支持的,会抛出UnsupportedOperationException异常
        List<Tree> list = Arrays.asList(tree);

        Iterator iterator = list.iterator();
        */

        List<Integer> list1 = Collections.nCopies(4,2);
        Iterator iterator1 =list1.iterator();
 //       list1.set(1,3);
        list1.add(2);
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }

    }
}
class Tree{
    int a = 0;
    public Tree(int i){
        a = i;
    }
}


这篇关于JAVA集合训练代码以及注释(自己看的)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!