集合Collection的方法,java的ArrayList以及LinkedList都是继承自Collection,继承的结构:

Collection<--List<--Vector 
Collection<--List<--ArrayList
Collection<--List<--LinkedList
Collection<--Set<--HashSet
Collection<--Set<--HashSet<--LinkedHashSet
Collection<--Set<--SortedSet<--TreeSet
Map<--SortedMap<--TreeMap
Map<--HashMap

  

  Collection方法包括: removeAll(), retainAll(), containsAll() , toArray(), 前三个要传Collection作为参数;

/*
Map ==>> [ HashMap, TreeMap ]
Collection ==>>
==>> Set [HashSet, TreeSet]
==>> List [ArrayList, LinkedList]
*/
import java.util.*;

public class TestCollection {
public static void main(String[] args) {
String apple = "apple";
String banana = "banana";
String pear = "pear";
Collection<String> list = new ArrayList<String>();
//Method add;
list.add( apple );
list.add( banana );
list.add( pear );
//use Interator;
Iterator<String> it = list.iterator();
while( it.hasNext() ) {
String str = it.next();
System.out.println( str );
};
/*
method removeAll;
@param Collection;
*/
Collection<String> del = new ArrayList<String>();
del.add(pear);
list.removeAll( del );
System.out.println( list );
/*
method retainAll;
@param Collection;
*/
del.remove(0);
del.add( apple );
list.retainAll( del );
System.out.println( list );
/*
method containsAll;
@param Collection;
*/
list.add( pear );
Boolean flag = list.containsAll( del );
System.out.println( flag );
/*
method toArray;
*/
//use Object[]; when i use String[] , it will not be ok;
Object[] arr = list.toArray();
for(int i=0; i<arr.length; i++) {
System.out.println( arr[i] );
}
}
}

 

  泛型的使用方法:

//the T means is "String";
public class ArrayClass <T>{
private T[] arr;
public void setArr(T[] arr) {
this.arr = arr;
};
public T[] getArr() {
return arr;
};

public static void main(String[] args) {
ArrayClass<String> arr = new ArrayClass<String>();
String[] tempArr = {"0","1","2","3"};
arr.setArr( tempArr );
System.out.println( arr.getArr() );
for( int i = 0; i < arr.getArr().length; i++ ) {
System.out.println( arr.getArr()[i] );
};
}
}

 

  

public class OverClass<T>{
private T over;
public T getOver(){
return over;
};
public void setOver(T over){
this.over = over;
};
public static void main(String[] args) {
OverClass<String> oc = new OverClass<String>();
oc.setOver("hehe");
System.out.println( oc.getOver() );

OverClass<Boolean> ocb = new OverClass<Boolean>();
ocb.setOver(false);
System.out.println( ocb.getOver() );
}
}

 

 

public class MutiOverClass<T1,T2>{
private T1[] arrString;
private T2 bValue;
public void setVal(T1[] arrs, T2 b) {
this.arrString = arrs;
this.bValue = b;
};
public String getVal() {
return (arrString.toString()+bValue.toString());
}
public static void main(String[] args) {
MutiOverClass<String, Boolean> muti = new MutiOverClass<String, Boolean>();
String[] tempArr = {"0","1","2","3"};
muti.setVal(tempArr,false);
System.out.println( muti.getVal() );
}
}

 

  我们可以对泛型的数据进行预定义,使用extends,语法有点像继承啊, 我勒个去;

import java.util.*;

public class LimitClass <T extends List>{
private T t;
//@Override
public T get () {
return t;
}
public void set (T list) {
this.t = list;
}
public static void main(String[] args) {
LimitClass lc = new LimitClass<ArrayList>();
System.out.println(lc.get());
ArrayList<String> tempAL = new ArrayList<String>();
tempAL.add("1");
lc.set( tempAL );
System.out.println(lc.get());
}
}

 

  HashMap的泛型使用方法, 实现首先要引入HashMap这个类,相当于JS中的闭包啊:

import java.util.HashMap;
public class MutiHashMap <K,V>{
public HashMap<K,V> map = new HashMap<K,V>();
public void putMap (K k, V v){
map.put(k, v);
};
public V getVByK(K k) {
return map.get(k);
};
public static void main(String[] args) {
MutiHashMap muti = new MutiHashMap<Integer,String>();
muti.map.put(1,"hehe");
System.out.println( muti.map.get(1) );

muti.putMap(4, "nono");
System.out.println( muti.getVByK(4) );
}
}

 

  

  枚举类型的使用,使用枚举的好处有:

  1:枚举可以限定取值范围,所有的内容只能从指定范围中取得,

  2:。。。。

  3:。。。。

  4:。。。。

interface Constants{
public static final int Constants_A = 1;
public static final int Constants_B = 2;
}

public class ConstantsTest {
//we can use enum to creates ..;
enum Constants1 {
Constants_A,
Constants_B
};

public static void doit(int val){
switch( val ) {
case 1 :
System.out.println("Constants_A ==>> val is" + val);
break;
case 2 :
System.out.println("Constants_B ==>> val is" + val);
break;
};
};
public static void doit1(Constants1 c){

switch( c ) {
case Constants_A :
System.out.println("Constants_A ==>> val is" + c);
break;
case Constants_B :
System.out.println("Constants_B ==>> val is" + c);
break;
};

};

public static void main (String[] args) {
ConstantsTest.doit(Constants.Constants_A);
ConstantsTest.doit(Constants.Constants_B);
ConstantsTest.doit1(Constants1.Constants_B);
ConstantsTest.doit1(Constants1.Constants_B);
System.out.println(Constants1.values()[0]);
System.out.println(Constants1.values()[1]);
};
}

 

  enum枚举类型可以定义自己的方法属性, enum相当于快速实例化构造函数, 直接访问对象的方法或者属性就可以了:

public class CopyOfEnumIndexTest {
//this is esay way to create instance, i guesss
enum Contants2 {
Contants_A("hehe"),
Contants_B("oooo"),
Contants_C("iiii"),
Contants_D(4);
private String description;
private int i = 1;

private Contants2 () {

};
private Contants2 (String description) {
this.description = description;
};
private Contants2 ( int i ) {
this.i += i;
};
private int getI () {
return this.i;
};
private String getDescription() {
return this.description;
};
}

public static void main(String[] args) {
for(int i = 0; i< Contants2.values().length ; i++) {
System.out.println( Contants2.values()[i].getI() );
System.out.println( Contants2.values()[i].getDescription() );
}
}
}

 

   通过java的net获取当前电脑的ip和主机名:

import java.net.*;
public class InetAddressDemo{
public static void main(String[] args) {
InetAddress intAddress;
try{
intAddress = InetAddress.getLocalHost();
String localname = intAddress.getHostName();
String localip = intAddress.getHostAddress();
System.out.println("name is : " + localname + "; ip is :" + localip);
}catch(UnknownHostException e) {
e.printStackTrace();
}
}
}

 

 

  服务器的SocketDemo和clientDemo, 服务器通过绑定本地的一个端口, 如果客户端连接这个端口, 服务端就返回信息, 客户端和服务端就可以进行交互

  服务器的代码, 绑定本地的4331端口, 通过一个无限循环等待客户端发送消息, 等待客户端的连接:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketDemo {
public static void main(String[] args) {
try{
ServerSocket serverSocket = null;
Socket clientSocket = null;
String str = "";
DataOutputStream out = null;
DataInputStream in = null;

serverSocket = new ServerSocket(4331);
System.out.println("wating fro clients connect~");

clientSocket = serverSocket.accept();
in = new DataInputStream( clientSocket.getInputStream() );
out = new DataOutputStream( clientSocket.getOutputStream() );

while(true) {
str = in.readUTF();
out.writeUTF(str);
System.out.println("get bytes : " + str);
//Thread.sleep(1000);
}
}catch( IOException e ) {
e.printStackTrace();
}
}
}

 

   客户端的代码:

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketDemo {
public static void main(String[] args) {
String str = null;
Socket clientSocket;
DataInputStream in = null;
DataOutputStream out = null;

try{
clientSocket = new Socket("127.0.0.1",4331);
in = new DataInputStream( clientSocket.getInputStream() );
out = new DataOutputStream( clientSocket.getOutputStream() );
out.writeUTF("I'm a client machine");
int i = 0;
while( true ) {
out.writeUTF(i++ + "");
str = in.readUTF();
System.out.println("send data " + str);
Thread.sleep(100);
}
}catch(Exception e) {
e.printStackTrace();
}
}
}

天道酬勤