天天看點

java容器排序_容器(周遊排序查找)

一、 1圖 1類(collections) 3知識點

6接口(hashset arraylist linkedlist hashmap iterator)

(容器位于Java.util包内)

collection Map

set list

HashSet LinkedList ArrayList HashMap

一個一個得裝 一對一對的裝

collection接口定義了存取一組對象的方法,其子接口set list分别定義存取方式。

例:

impor

java.util.*;

public

class Test{

public static void main(String[] args){

Collection c = new ArrayList();//父類對象指向子類對象,便于以後修改如把ArrayList改成

LinkedIist 下面的特定代碼則可以不用修改。

c.add("hello");

c.add(new

Integer(100));//注意括号中必須為對象而不能為基礎類型對象的值

System.out.println(c.size());

System.out.println(c);//調用toString方法

}

}

二、Iterator接口

所有實作了Collection接口的容器類都有一個iterator方法用以傳回一個實作了Iterator接口的對象。

Iterator對象稱作疊代器,用以友善的實作對容器内元素的周遊操作。

Iterator接口定義了如下方法:

boolean

hasNext(); //判斷遊标右邊是否有元素

Object

next(); //傳回遊标右邊的元素并将遊标移動到下一個位置

void

remove(); //删除遊标左面的元素,在執行完next之後該操作隻能執行一次

Java代碼

java容器排序_容器(周遊排序查找)

importjava.util.*;

publicclassTest {

publicstaticvoidmain(String[] args) {

Collection c =newHashSet();

c.add(newName("f1","l1"));

c.add(newName("f2","l2"));

c.add(newName("f3","l3"));

Iterator i = c.iterator();

while(i.hasNext()) {

//next()的傳回值為Object類型,需要轉換為相應類型

Name n = (Name)i.next();

System.out.print(n.

getFirstName_r()+" ");

}

}

}

**1.5增強for循環

1、對于周遊array和Collection相當簡單

2、不友善通路Array的下标值,與iterator相比不友善删除集合中的内容,除了簡單周遊讀出其中内容外,

不建議使用這種方法

import java.util.*;

public class EnhancedFor {

public static void

main(String[] args) {

int[] arr = {1, 2, 3, 4,

5};

//把arr中的元素拿出來放在i中

for(int i : arr) {

System.out.println(i);

}

Collection c = new

ArrayList();

c.add(new String("aaa"));

c.add(new String("bbb"));

c.add(new String("ccc"));

for(Object o : c) {

System.out.println(o);

}

}

}

三、Set List算法舉例

1、 public static void

main(String[] args){

Set s1 = new HashSet();

Set s2 = new HashSet();

s1.add("a");s1.add("b");s1.add("c");

s2.add("k");s2.add("a");s2.add("a");

Set s3 = new HashSet(s1);

s3.retainAll(s1);

Set s4 = new HashSet(s1);

s4.addAll(s2);

System.out.println(s3);

System.out.println(s4);

}

2、List l1 = new

LinkedList();

for(int i = 0;i<=5;i++){

l1.add("a"+i);

}

System.out.println(l1);

l1.add(3,"a100");

System.out.println(l1);

l1.set(6,"a200");

System.out.println(l1);

System.out.print((String)l1. get_r(2)+" ");

System.out.print(l1.indexof("a3"));

l1.remove(1);

System.out.println(l1);

常用算法:

List l1 = new LinkedList();

List l2 = new LinkedList();

for(int i=0;i<=9;i++){l1.add("a"+i);}

System.out.println(l1);

Collections.shuffle(l1);//随機排序

System.out,println(l1);

Collections.reverse(l1);//逆序

System.out,println(l1);

Collections.sort(l1);//排序

System.out,println(l1);

System.out.println(Collections.binarySearch(l1,"a5"));//2分法查找

3、comparable接口

comparable接口中隻有一個方法public int compareTo(object

o),是以在繼承了該接口的子類中需要重

寫compareTo方法。

import java.util.*;

public class BasicContainer {

public static void

main(String[] args) {

Collection c = new HashSet();

c.add("hello");

c.add(new Name("f1","l1"));

c.add(new Integer(100));

c.remove("hello");

c.remove(new Integer(100));

System.out.println

(c.remove(new Name("f1","l1")));

System.out.println(c);

}

}

//繼承Comparable接口

class Name implements Comparable {

private String firstName,lastName;

public Name(String firstName, String lastName)

{

this.firstName = firstName; this.lastName = lastName;

}

public String getFirstName_r()

{ return

firstName; }

public

String getLastName_r() { return

lastName; }

public String toString() { return firstName + " " + lastName; }

//對象在調用remove、contains方法時比較對象是否相等,對于自定義的類型需要重寫equals方法

//和hashCode方法以實作自定義對象相等規則。

public boolean equals(Object obj) {

if (obj instanceof Name) {

Name name = (Name) obj;

return (firstName.equals(name.firstName))

&&

(lastName.equals(name.lastName));

}

return super.equals(obj);

}

public int hashCode() {

return firstName.hashCode();

}

//重寫compareTo方法确定對象間的大小

public int compareTo(Object o) {

Name n = (Name)o;

int lastCmp =

l astName.compareTo(n.lastName);

return

(lastCmp!=0 ? lastCmp : firstName.compareTo(n.firstName));

}

}

4、

Map接口:

Map接口的類用來存儲鍵-值對,

Map接口的實作類有HashMap和TreeMap,鍵值對通過鍵辨別,是以鍵值

不能重複。

例:

import java.util.*;

public class TestMap {

public static void main(String args[]) {

Map m1 = new HashMap();

Map m2 = new TreeMap();

//m1.put("one",new

Integer(1));

m1.put("one", 1);

//m1.put("two",new Integer(2));

m1.put("two", 2);

//m1.put("three",new Integer(3));

m1.put("three", 3);

//m2.put("A",new Integer(1));

m2.put("A", 1);

//m2.put("B",new Integer(2));

m2.put("B", 2);

System.out.println(m1.size());

System.out.println(m1.containsKey("one"));

System.out.println

//(m2.containsValue(new Integer(1)));

(m2.containsValue(1));

if(m1.containsKey("two")) {

//int i =

((Integer)m1.get("two")).intValue();

int i = (Integer)m1.get("two");

System.out.println(i);

}

Map m3 = new HashMap(m1);

m3.putAll(m2);

System.out.println(m3);

}

}

運用泛型和自動打包解包後執行個體:

import java.util.*;

public class TestMap {

public static void main(String args[]) {

Map

Integer> m1 = new HashMap

Integer>();

m1.put("one", 1);

m1.put("two", 2);

m1.put("three", 3);

System.out.println(m1.size());

System.out.println(m1.containsKey("one"));

if(m1.containsKey("two"))

{

//int i = ((Integer)m1.

get_r("two")).intValue();

int i = m1. get("two");

System.out.println(i);

}

}

}

5,自動打包解包。

import java.util.*;

public class TestArgsWords {

//private static final Integer ONE = new Integer(1);

private

static final int ONE = 1;

public static void main(String args[]) {

Map m = new HashMap();

for (int i = 0; i < args.length;

i++) {

//Integer freq = (Integer) m.get(args[i]);

int freq = (Integer) m.get(args[i]) == null ? 0

: (Integer) m.get(args[i]);

//m.put(args[i],(freq == null? ONE : new

Integer(freq.intValue() + 1)));

m.put(args[i], freq==0 ? ONE : freq + 1);

}

System.out.println(m.size() + " distinct words

detected:");

System.out.println(m);

}

}

6、泛型 :在定義集合時同時定義集合中對象的類型。

import java.util.*;

public class BasicGeneric {

public static void main(String[] args) {

List c = new

ArrayList();//API中類後面跟有<>就可用泛型

c.add("aaa");

c.add("bbb");

c.add("ccc");

for(int i=0;

i

String s =

c.get(i);

System.out.println(s);

}

Collection

c2 = new HashSet();

c2.add("aaa"); c2.add("bbb");

c2.add("ccc");

for(Iterator it =

c2.iterator(); it.hasNext(); ) {

String s = it.next();

System.out.println(s);

}

}

}

class MyName implements

Comparable {

int age;

public int compareTo(MyName mn) {

if(this.age >

mn.age) return 1;

else if(this.age < mn.age) return -1;

else return 0;

}

}

讀單詞出現的次數:未使用和使用泛型比較:

import java.util.*;

public class TestArgsWords {

//private static final Integer ONE = new

Integer(1);

private static final int ONE = 1;

public static void main(String args[]) {

Map m = new HashMap();

for (int i = 0; i < args.length;

i++) {

//Integer freq = (Integer) m.get(args[i]);

int freq = (Integer) m.get(args[i]) == null ? 0 : (Integer)

m.get(args[i]);

//m.put(args[i],(freq == null? ONE : new

Integer(freq.intValue() + 1)));

m.put(args[i], freq==0 ? ONE : freq + 1);

}

System.out.println(m.size() + " distinct words

detected:");

System.out.println(m);

}

}

使用後

import java.util.*;

public class TestArgsWords2 {

private static final int ONE = 1;

public

static void main(String args[]) {

Map m = new

HashMap();

for (int i = 0; i < args.length; i++) {

if(!m.containsKey(args[i])) {

m.put(args[i], ONE);

}

else {

int freq = m.get(args[i]);

m.put(args[i], freq + 1);

}

}

System.out.println(m.size() + " distinct words detected:");

System.out.println(m);