Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2727

using TreeMap how to return the Object ?

Newsgroups comp.lang.java.help
Date 2013-06-26 22:30 -0700
Message-ID <f11f9858-f870-42af-a3a3-e9fdb945ba41@googlegroups.com> (permalink)
Subject using TreeMap how to return the Object ?
From moonhkt <moonhkt@gmail.com>

Show all headers | View raw


Hi All


How to using  searchKey return object eleHost values? 


cat eleHost.java

  public class eleHost {
       String ip;
       String host1;
  
       public eleHost (String a, String b) {
            this.ip = a;
            this.host1 = b;
       }
    /*   public String toString () { 
          return ip + "|"  + host1;
       } */
  }

cat clsHosts.java

import java.io.*;
import java.util.*;
import java.util.regex.*;
import java.lang.String.*;
import java.text.*;

public class clsHosts {
   // Instance Variables 
   // http://kerflyn.wordpress.com/2011/05/20/how-treemap-can-save-your-day/ 
   TreeMap ipx = new TreeMap () ;  /* a sorted and navigable map */
   String ifn = "/etc/hosts" ;
   
   // Constructor  
   public clsHosts ()  {
   }
   // Methods 
   public void process_hosts () {
      File ihost = new File(ifn); 
      String delimscfg = "\\s+";  /** multi-space */
      String aline ;
      int i  = 0 ;
      try {
         BufferedReader br1 = new BufferedReader(new FileReader(ihost));
          while ((aline = br1.readLine()) != null) { 
            String [] tokens = aline.split(delimscfg); 
            try {
                if ( ! tokens[0].trim().startsWith("#")) { 
                   /*  System.out.print("-->");  
                    System.out.print(tokens[0].trim());
                    System.out.print(" ");
                    System.out.print(tokens[0].trim().substring(0));
                    System.out.println("<--"); */
                    ipx.put(tokens[0].trim(),new eleHost(tokens[1].trim(),"x"));  
                 }  
            } catch (ArrayIndexOutOfBoundsException e) {
                continue;
            }
          }
       } catch (IOException e) { 
          System.out.println(e);
      }
   }
   /*  Print all data */
   public void printAll () { 
      Set set = ipx.entrySet();
      Iterator i = set.iterator();
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry) i.next();
         System.out.printf("%-18s %-25s\n" , me.getKey() , me.getValue());
      }  
    }
   /* get Hostname by IP address */
   public String getHost (String ip) {
     Set set = ipx.entrySet ();
     Iterator i = set.iterator ();
     while (i.hasNext()) {
          Map.Entry me = (Map.Entry) i.next ();
          if (me.getKey().equals(ip)) { 
              return me.getValue().toString();
          }
     }
     return null;
   }
   public void searchKey (String ip) {
      System.out.println( ipx.get(ip));
   }
}

cat getip.java

class getip {

  public static void main (String args[]) throws Exception {
     clsHosts v = new clsHosts(); 
     String rtn ;
     v.process_hosts();
     v.printAll();
     rtn = v.getHost("21xxxxxxx");
     System.out.println("rtn=" + rtn);
     v.searchKey("21xxxxxxx");
  }

}

Back to comp.lang.java.help | Previous | NextNext in thread | Find similar | Unroll thread


Thread

using TreeMap how to return the Object ? moonhkt <moonhkt@gmail.com> - 2013-06-26 22:30 -0700
  Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-06-27 11:19 -0400
    Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-06-27 13:15 -0400
    Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-07-01 10:49 -0400
  Re: using TreeMap how to return the Object ? Lew <lewbloch@gmail.com> - 2013-06-28 10:42 -0700
    Re: using TreeMap how to return the Object ? Patricia Shanahan <pats@acm.org> - 2013-06-28 19:13 -0700
      Re: using TreeMap how to return the Object ? Gene Wirchenko <genew@telus.net> - 2013-07-01 14:16 -0700

csiph-web