1

I wrote a simple program which reads IpTables log results , remove 90% of the output , and what is left is:

192.168.1.1 152 192.168.1.1 17485 192.168.1.1 5592 

Where, the first column contains source IP , the second one destination Port. Those values are stored in a string. I would like to transfer the values from that string to a Hashtable, but I don't have any idea how.

Hashtable<String, String> IpDpt = new Hashtable<String, String>(); hmIpDpt.put(IP1,DPT1); hmIpDpt.put(IP1,DPT2); hmIpDpt.put(IP1,DPT3); hmIpDpt.put(IP2,DPT4); 
1
  • Use a HashMap instead..Hashtable was deprecated long ago... Commented Dec 3, 2013 at 12:47

5 Answers 5

4

if you transferred to hashtable then you will not have all the logs available because hashtable does not allows duplicate values and in your log there may be chances of duplicate values . so hashtable overrides values.

Sign up to request clarification or add additional context in comments.

2 Comments

This is not exactly an answer though.
Please add this as a hint in the comments section.
2

Try this program.

String info = "192.168.1.1 152"; Hashtable<String, List<String>> IpDpt = new Hashtable<String, List<String>>(); String[] values = info.split( " " ); String key = values[0]; String value = values[1]; if ( IpDpt.containsKey( key ) ) IpDpt.get( key ).add( value ); else IpDpt.put( key, new ArrayList<String>( Arrays.asList( value ) ) ); 

To check whether the key has at least 100 values

if(IpDpt.containsKey( key ) && IpDpt.get( key ).size() >=100) { // business logic here } 

3 Comments

I should be reading my results live from a string , and deciding whether one 1 IP address is checking more than 100 ports on my machine... So the values from the string should go the the hasmap and I should check wheter the key has at least 100 values.
You can put extra check to check if the key has at least 100 values. if(IpDpt.containsKey( key ) && IpDpt.get( key ).size() >100) { // business logic here }
Also you can replace List with Set if there are duplicate entries for the port number with same IP address. Like 192.168.1.1 152 entry is multiple times.
0

For every line you read, split the string on space like that:

 String line = "192.168.1.1 152"; String[] parts = line.split(" "); you_map.put(parts[0], parts[1]); 

That should do it.

1 Comment

But this does not allow n:m mappings of IPs to ports. Note that the same IP address might appear multiple times!
0

A Map cannot hold multiple values per key. So you should either use a third party library that offers something like a MultiMap, or use a Set to hold the values:

Map<String, Set<String>> ipToPortMap = new HashMap<>(); public void addEntry(String ip, String port) { Set<String> ports = ipToPortMap.get(ip); if (ports == null) { ports = new HashSet<>(); ipToPortMap.put(ip, ports); } ports.add(port); } 

Now just iterate over your input (maybe using a Scanner) and call addEntry.

Comments

0

Use Guava Multimap to allow duplicate key.

 Multimap<String,String> multimap=ArrayListMultimap.create(); //put your ips and port. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.