How to give key more than one value in LinkedHashMap - java

I have a graph, and i want to give each node different value, because i want to calculate every node's betweenness ,predecessor and so on,
but i think if i use map, the former value will be replaced. how can i figure out that ? Following is my code. I try to use map
LinkedHashMap<Vertex<E>, Double> Betweenness = new LinkedHashMap<>();
for(Vertex<E> vertex : graph.vertexSet())
{
Betweenness.put(vertex,0.0);
}
for(Vertex<E> vertex : graph.vertexSet())
{
//Create stack to keep track of vertices in order to calculate value of flow
StackADT<Vertex<E>> processingStack = new LinkedStack<Vertex<E>>();
LinkedHashMap<Vertex<E> ,List<Vertex<E>>> Predecessor = new LinkedHashMap<>();
for(Vertex<E> predecessorofVertex : graph.vertexSet())
{
List<Vertex<E>> PredecessorList = new LinkedList<>();
Predecessor.put(predecessorofVertex, PredecessorList);
}
}

Related

Getting a random element from a Collection

in java I would like to be able to maintain my Collection of fishes sorted by species at all time (hence the use of a HashMap) while being able to pick a random element from all species except one with constant time complexity. For example the following code does the job but with O(number of elements) complexity :
import java.util.*;
HashMap<String, ArrayList<Fish>> fishesBySpecies = new HashMap<>();
// Insert some fishes...
// Fish has a String attribute that describes its species
// Now we want to pick a random Fish that isn't from the unwantedSpecies
String unwanted = "unwanted species";
ArrayList<Fish> wantedSpecies = new ArrayList<>();
for (String species : fishesBySpecies.keySet()) {
if (!Objects.equals(species, unwanted)) {
wantedSpecies.addAll(fishesBySpecies.get(species));
}
}
// Finally !
int randomIndex = new Random().nextInt(wantedSpecies.size());
Fish randomElement = wantedSpecies.get(randomIndex);
Any idea how to do this with constant time complexity if possible ? Thanks !
What you are performing is filtering, and when filtering you have to check each element whether they need to be taken out or not. You could try to use alphabetical sorting on the keys and stop filtering once the key is alphabetically larger than your filtering (unwanted) key.
Your code can also be thoroughly shortened by using java streams:
HashMap<String, ArrayList<Fish>> fishesBySpecies = new HashMap<>();
// Insert some fishes...
// Fish has a String attribute that describes its species
// Now we want to pick a random Fish that isn't from the unwantedSpecies
String unwanted = "unwanted species";
fishesBySpecies.keySet().stream() // Get the keyset and create a stream out of it
.filter(key -> !key.equalsIgnoreCase(unwanted)) // If key is not equal to unwanted then leave it in else remove it
.forEach(filteredKey ->
wantedSpecies.addAll(fishesBySpecies.get(filteredKey))); // For each key that was left in, we fetch the fishes
OR
fishesBySpecies.keySet().stream() // Get the keyset and create a stream out of it
.forEach(key ->
{
if(!key.equalsIgnoreCase(unwanted))
{
wantedSpecies.addAll(fishesBySpecies.get(unwanted));
}
}
); // iterate and filter at the same time. Faster.
The only way I can think of would consist in maintaining an ArrayList<Fish> as well as the map you already have. There is a drawback though: adding or removing fishes would be slightly more complex:
Map<String, List<Fish>> fishesBySpecies = new HashMap<>();
List<Fish> wantedFishes = new ArrayList<>();
//...
public void addFish(String species, Fish fish) {
List<Fish> speciesFishes = fishesBySpecies.get(species);
if (speciesFishes == null) {
speciesFishes = new ArrayList<>();
fishesBySpecies.put(species, speciesFishes);
}
speciesFishes.add(fish);
// also maintain the list of wanted fishes
if (!unwantedSpecies.equals(species)) {
wantedFishes.add(fish);
}
}

Can compare values from Map<Integer, ArrayList<String>> with ArrayList<String>

I created a Map<Integer, ArrayList<String>> map and I would like to compare each value in map with one ArrayList<String> likeList and get key if they match. I will bring the key to use later.
I tried to run my code like this, but it doesn't work because it returns nothing:
for (int key : map.keySet()) {
if(map.get(key).equals(likeList)){
index = key;
Log.d("IndexN", String.valueOf(index));
}
}
Then, I tried this:
int index = 0;
for (Map.Entry<Integer, ArrayList<String>> entry : map.entrySet()) {
if(entry.getValue().equals(likeList)){
index = entry.getkey();
}
}
Do you have any idea?
Add a list of the key to store all match
List<Integer> indices = new ArrayList<>();
for (int key : map.keySet()) {
if (map.get(key).equals(likeList)) {
indices.add(key);
}
}
It does not return index when I try the code above.
From this comment, I understood that as soon as you find a match in the map, the index should be recorded and further processing should be stopped. In other words, either there is only one match of likeList in the map or you want to find the first match of likeList in the map. If yes, you need to break the loop as soon as the match is found (shown below).
for (int key : map.keySet()) {
if (map.get(key).equals(likeList)) {
Log.d("IndexN", String.valueOf(index));
break;
}
}
Note that this will give you the same value, each time you execute it, only when the map has only one match of likeList or the map is a LinkedHashMap. If it is a HashMap and it has more than one matches of likeList, you may get a different value each time you execute it because a HashMap does not guarantee the order of its entries.
However, if there can be multiple matches of likeList in the map and you want to log all the matches as well as get the list of the corresponding keys, you can do it as follows:
List<Integer> indexList = new ArrayList<>();
for (int key : map.keySet()) {
if (map.get(key).equals(likeList)) {
Log.d("IndexN", String.valueOf(index));
indexList.add(key);
}
}
// Display the list of corresponding keys
System.out.println(indexList);

The implementation of Map implies that each key may contains only 1 value... But i need that 1 key could store <Any Big Number> values

For example... Adjacency list realiszation
public class Vertex {
String name;
boolean visited;
public Vertex(String name) {
this.name=name;
visited=false;
}
public int hashCode() {
return name.hashCode();
}
public boolean equals(Object ob) {
return hashCode()==ob.hashCode();
}
public String toString() {
return name;
}
}
The main class
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) {
PrintWriter pw=new PrintWriter(System.out);
Map<Vertex,Vertex> m=new HashMap();
m.put(new Vertex("a"), new Vertex("b"));// a ---> b
m.put(new Vertex("a"), new Vertex("c"));// a ---> c
m.put(new Vertex("a"), new Vertex("d"));// a ---> d
pw.println("All vertex from: ");
for (Vertex vert_from:m.keySet()) {
pw.print(vert_from+" ");
}
pw.println();
pw.println("All vertices to: ");
for (Vertex vert_to:m.values()) {
pw.print(vert_to+" ");
}
pw.close();
}
}
It outputs:
All vertex from:
a
All vertices to:
d
But i need that "All vertices to: b c d"
How can I fix that?
A Map indeed stores a single value per key. You could, however, store a collection in value, say a Set:
Map<Vertex, Set<Vertex>> m = new HashMap<>();
Set<Vertex> set = new HashSet<>();
set.add(new Vertex("b"));
set.add(new Vertex("c"));
set.add(new Vertex("d"));
m.add (new Vertex("a"), set);
Alternatively, you can use one of the common implementations of this concept, such as Apache Commons Collections' MultiValueMap or Guava's HashMultiMap.
What you are asking for is called a "Multi Map".
If you are using Java 8 then this is quite neat, first you need a Map<Vertex, Collection<Vertex>>. I don't know what properties you need from the Collection, that you will have to investigate yourself.
As you have overridden equals and hashCode (incorrectly, but a valiant attempt), I will assume that you want to have the items unique by name. I will also assume that order matters, so LinkedHashSet seems a good choice.
final Map<Vertex, Collection<Vertex>> graph = new HashMap<>();
Now, to add an item to the Map we need to first ensure that the Collection for that key is not null. This is exactly what the new Map.computeIfAbsent comes in.
final Vertex a = new Vertex("a");
graph.computeIfAbsent(a, v -> new LinkedHashSet<>()).add(new Vertex("b"));
graph.computeIfAbsent(a, v -> new LinkedHashSet<>()).add(new Vertex("c"));
graph.computeIfAbsent(a, v -> new LinkedHashSet<>()).add(new Vertex("d"));
So what this does is, when inserting a into the Map, if the Collection for that key is null, computes a new value for it.
Now to get all values for a key:
Collection<Vertex> values = graph.get(a);
You could wrap the Map<Vertex, Collection<Vertex>> in some sort of Graph class to hide the implementation details and to have neater code:
class Graph {
final Map<Vertex, Collection<Vertex>> graph = new HashMap<>();
public void put(final Vertex key, final Vertex value) {
graph.computeIfAbsent(key, k -> new LinkedHashSet<>()).add(value);
}
public Collection<Vertex> get(final Vertex key) {
return Optional.ofNullable(graph.get(key)).orElse(Collections.EMPTY_SET);
}
}
This also deals with returning an empty collection instead of null if a key is not present in the Map. Depending on your use case you might also want to wrap the returned Collection with Collections.unmodifiableCollection to prevent unwanted modifications:
public Collection<Vertex> get(final Vertex key) {
return Optional.ofNullable(graph.get(key))
.map(Collections::unmodifiableCollection)
.orElse(Collections.EMPTY_SET);
}
You could also use a Guava Multimap if you aren't averse to external libraries.
Using a Multimap for your problem, it could be written like that:
public static void main(String[] args) {
PrintWriter pw=new PrintWriter(System.out);
ListMultimap<Vertex,Vertex> m= ArrayListMultimap.create();
Vertex a = new Vertex("a"); // it's better to create each object once
Vertex b = new Vertex("b");
Vertex c = new Vertex("c");
Vertex d = new Vertex("d");
m.put(a,b);// a ---> b
m.put(a,c);// a ---> c
m.put(a,d);// a ---> d
pw.println("All vertex from: ");
for (Vertex vert_from:m.keySet()) { //exactly the same as in your code
pw.print(vert_from+" ");
}
pw.println();
pw.println("All vertices to: ");
for (Vertex vert_to:m.values()) { //exactly the same as in your code
pw.print(vert_to+" ");
}
pw.close();
}
To use Guava, just download the latest jar from here and add it to your libraries.
Explanation:
By definition, each java Map has a single key and a single value.
However, you can use a Collection (like a List), or an Array for value. This way, your Map will be defined like that:
Map<Vertex, List<Vertex>> m = new HashMap<>();
Each time you want to add an element value to the list of vertex key, you can do it that way:
List<Vertex> list = m.get(key);
if (list == null) {
list = new ArrayList<>();
}
list.add(value);
An easier way, is to use Guava's Multimaps. It is the same as a Map, but the value is a Collection. So, an ArrayListMultimap is quite what I described above. The way to use it, though is much simpler:
ListMultimap<Vertex, Vertex> m = ArrayListMultimap.create();
m.put(key, value1);
m.put(key, value2); //adds value2 to the key, which also contains value1
....

How to tell if a List contains a Map key

I am trying to write a while loop that will continue to iterate until the nodes list does not have a certain key in a it's map. My code looks like this:
List<Map<Integer, Integer>> nodes = new LinkedList<Map<Integer, Integer>>();
List<Integer> parent = new LinkedList<Integer>();
.
.
.
while (parent != null) {
int vertex = parent.remove(0);
while(//The problem )
}
}
I will be pulling the integer from parent and placing it into vertex and will be using vertex to find the key in the nodes. What would the call look like to find the integer in nodes?
This might help
for(int vertex: parent) {
for(Map<Integer, Integer) entry : nodes) {
if(entry.contains(vertex) {
//the map entry has the key, write your logic and return
return entry
}
}
}
I'm not exactly sure if that's what you want but it might.
List<Map<Integer, Integer>> nodes = new LinkedList<Map<Integer, Integer>>();
LinkedList<Integer> parent = new LinkedList<Integer>();
// ^ or Queue to use .poll() which removes the first item
Integer parentItem;
while ((parentItem = parent.poll()) != null) {
// check if that item is somewhere in the maps
boolean inMaps = false;
for(Map<Integer, Integer> map : nodes) {
if (map.containsKey(parentItem)) {
inMaps = true;
break;
}
}
// if it is not do something special, maybe "return" or "break;"
if (!inMaps) {
// do something.
}
}

Recursive Table/Row Generator

I'm having a tough time wrapping my head around the following situation. The best way to explain may be by example
I have a Map<Column,Set<Row>> object.
Let's say it contains the following data:
ColumnA['abc','def']
ColumnB['efg','hij','klm']
ColumnC['nop']
ColumnD['qrs','tuv','wxy','zzz']
I am trying to generate the following output:
Row1[abc,efg,nop,qrs]
Row2[abc,efg,nop,tuv]
Row3[abc,efg,nop,wxy]
Row4[abc,efg,nop,zzz]
Row5[abc,hij,nop,qrs]
Row6[abc,hij,nop,wxy]
etc...
So in this case there would be 24 rows total.
However, the number of columns and rows are both dynamic. I feel like this needs to be recursively done somehow but I'm not sure where to start.
Any help would be appreciated.
Update - I made a Tree structure that seems to work.
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
Set<DefaultMutableTreeNode> curNodes = new HashSet<DefaultMutableTreeNode>();
curNodes.add(root);
final Set<Column> keys = map.keySet();
for (final Column key : keys) {
final Set<Row> rowSet = map.get(key);
Set<DefaultMutableTreeNode> tmpNodes = new HashSet<DefaultMutableTreeNode>();
for (final Row row : rowSet) {
DefaultMutableTreeNode curNode = new DefaultMutableTreeNode();
curNode.setUserObject(row);
tmpNodes.add(curNode);
for (DefaultMutableTreeNode n : curNodes) {
n.add(curNode);
}
}
curNodes = tmpNodes;
}
I hope this is not some student's homework.
First to keep the order of the map's keys the same, use a SortedMap, like TreeMap.
Furthermore in your initial map every Row contains just a single value like 'abc'.
Recursion here is a depth-first traversal. The hard thing is that a map has not a
natural traversal. For the rest have todos/candidates and dones/result; do a step changing data and afterwards restore them.
Here I use the more known List, but a Stack would be nicer.
public List<Row> generateRows(SortedMap<Column, Set<Cell>> map) {
List<Row> done = new ArrayList<Row>();
List<Column> columnsToDo = new LinkedList<Column>(map.keySet());
List<Cell> partialRow = new LinkedList<Cell>();
generateRowsRec(map, columnsToDo, partialRow, done);
return done;
}
void generateRowsRec(SortedMap<Column, Set<Cell>> map, List<Column> columnsToDo, List<Cell> partialRow, List<Row> done) {
if (columnsToDo.isEmpty()) {
done.add(new Row(partialRow));
return;
}
Column firstColumn = columnsToDo.remove(0); // Step A
for (Cell cell : map.get(firstColumn)) {
partialRow.add(cell); // Step B
generateRowsRec(map, columnsToDo, partialRow, done);
partialRow.remove(partialRow.size() - 1); // Unstep B
}
columnsToDo.add(0, firstColumn); // Unstep A
}

Categories

Resources