All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zusmart.base.graph.DirectedGraph Maven / Gradle / Ivy

Go to download

提供基础的工具类及方法类,Logging,Scanner,Buffer,NetWork,Future,Thread

There is a newer version: 1.0.6
Show newest version
package com.zusmart.base.graph;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class DirectedGraph implements Iterable {

	private final Map> nodes = new HashMap>();

	public boolean addNode(T node) {
		if (nodes.containsKey(node)) {
			return false;
		}
		nodes.put(node, new HashSet());
		return true;
	}

	public boolean addEdge(T head, T foot) {
		if (!nodes.containsKey(head)) {
			this.addNode(head);
		}
		if (!nodes.containsKey(foot)) {
			this.addNode(foot);
		}
		nodes.get(head).add(foot);
		return true;
	}

	public Set getEdges(T head) {
		if (nodes.containsKey(head)) {
			return Collections.unmodifiableSet(nodes.get(head));
		} else {
			return Collections.unmodifiableSet(new HashSet());
		}
	}

	@Override
	public Iterator iterator() {
		return this.nodes.keySet().iterator();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy