模拟HashSet

package chain;

/**
 * 单链表 节点(Map中的Entry<K,V>)
 * 
 * @author sd
 * 
 */
public class Node_Single {
	public String key;// 节点的值

	public Node_Single next;// 指向下一个的指针

	public Node_Single(String key) {// 初始化head
		this.key = key;
		this.next = null;
	}

	public Node_Single(String key, Node_Single next) {
		this.key = key;
		this.next = next;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public Node_Single getNext() {
		return next;
	}

	public void setNext(Node_Single next) {
		this.next = next;
	}

	@Override
	public String toString() {
		return "Node_Single [key=" + key + ", next=" + next + "]";
	}

}
package chain;

import java.io.IOException;

import net.sf.json.JSONArray;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * 单链表(单个数据组的单链表)
 * 
 * @author sd
 * 
 */
public class SingleList {
	transient static Node_Single[] table = new Node_Single[1];
	static int bucketIndex = 0;// 索引
	public static ObjectMapper mapper = new ObjectMapper();

	/**
	 * 添加一个元素
	 * 
	 * @param node
	 */
	public void addTolist(String key) {
		Node_Single e = table[bucketIndex];
		table[bucketIndex] = new Node_Single(key, e);
	}

	public static void main(String[] args) throws JsonGenerationException,
			JsonMappingException, IOException {
		SingleList sin = new SingleList();
		sin.addTolist("1");
		sin.addTolist("2");
		System.out.println(table[0]);
		System.out.println(JSONArray.fromObject(table));
		System.out.println(mapper.writeValueAsString(table));
	}

}
结果
Node_Single [key=2, next=Node_Single [key=1, next=null]]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/workspace/Test1/WebContent/WEB-INF/lib/slf4j-log4j12-1.7.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/workspace/Test1/WebContent/WEB-INF/lib/slf4j-log4j12-1.7.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
[{"key":"2","next":{"key":"1","next":null}}]
[{"key":"2","next":{"key":"1","next":null}}]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值