0

私は Python のバックグラウンドを持っており、現在 Python プログラムを Java に移植しています。問題を解決するための最善のアプローチについて提案が必要です。

もともと、私は Python でタプルのリストを作成しました:

loft = [('india',1),('accepts',1),('narendra',1), ('modi',1),('manmohan',1),('singh',1),('sonia gandhi',1),('rajkot',1),('sharma',1),('raja',1),('india',2),('manmohan',2),('singh',2),('nepal',2),('prime minister',2),('meeting',2),('economy',2),('manmohan',3),('narendra',3),('modi',3),('gupta',3),('rajkot',3),('patel',3),('singh',3),('rajiv',3),('aajtak',3),('manmohan',4),('nepal',4),('bahadur',4),('king',4),('meeting',4),('economy',4),('wife',4),('plane',4)]

(india,accepts はキーワード、数字はデータベースから取得した ID です)。今、適用します:

di = {}
for x,y in ll:
     di.setdefault(x,[]).append(y)
newdi = {}

私のリストは辞書になります:

di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}

Java 部分:

    public void step1() throws SQLException{

      Connection con= new Clustering().connect();

      Statement st = con.createStatement();
      Statement st1 = con.createStatement();
      ResultSet rs = st.executeQuery("select uid from url where artorcat=1");

      ArrayList<Tuples> allkeyword = new ArrayList<Tuples>();
      long starttime = System.currentTimeMillis();

      while (rs.next()) {
        int id = rs.getInt("uid");
        String query = "select tags.tagname from tags left join tag_url_relation on tags.tid=tag_url_relation.tid where tag_url_relation.uid="+id;
        ResultSet rs1 = st1.executeQuery(query);
        while (rs1.next()){
          String tag = rs1.getString(1);

          //Creating an object t of type Tuples
          //and pass values to constructor
          Tuples t = new Tuples(id,tag);
          //adding the above tuple to arraylist allkeyword
          allkeyword.add(t);
        }//job done, now lets test by iterating
      }

      Iterator<Tuples> it = allkeyword.iterator();
      while(it.hasNext()){

        Tuples t = it.next();
        System.out.println(t.getId());
        System.out.println(t.getKeyword());
      }

      long endtime = System.currentTimeMillis();
      long totaltime = endtime-starttime;
      System.out.println("Total time:" + totaltime);
    }


And here is Tuples class : 

/**
 * 
 * 
 * Tuple class is created to create a multiple data type tuple. We are using this tuples object to retrieve keyword and 
 * id in step1 in Clustering.java.
 * @author akshayy
 *
 */


public class Tuples {
    int i;
    String s;


    public Tuples(int i, String s) {
        this.i= i;
        this.s=s;
    }


    public int getId(){
        return this.i;
    }

    public String getKeyword(){
        return this.s;      
    }


}

ここまでは順調ですね。キーワードと ID を含むタプル クラスの配列リストが作成されます。では、id でのキーワードの出現を見つける次のステップについてはどうでしょうか。「マンモハン」のようなものは、ID 1、2、3、4 などにあります。

di = {'manmohan': [1, 2, 3, 4], 'sonia gandhi': [1], 'raja': [1], 'india': [1, 2], 'narendra': [1, 3], 'patel': [3], 'sharma': [1], 'nepal': [2, 4], 'gupta': [3], 'singh': [1, 2, 3], 'meeting': [2, 4], 'economy': [2, 4], 'rajkot': [1, 3], 'prime minister': [2], 'plane': [4], 'bahadur': [4], 'king': [4], 'wife': [4], 'accepts': [1], 'modi': [1, 3], 'aajtak': [3], 'rajiv': [3]}

arraylistで同様のアイテムを見つけて上記のように並べ替えるための次のアプローチを教えてください。それとも、まったく別のものが必要ですか?

4

3 に答える 3

2

java.lang.Map インターフェースを見てください。あなたは本質的に構築しています

Map<String,List<Integer>> 

純粋な Collections クラスを使用すると、contains や Collections.sort などのメソッドを使用できます (パフォーマンスが気になる場合は、必要に応じて独自の並べ替えアルゴリズムを検討してください)。

Map の反復処理は、新しい Java 開発者にとってそれほど簡単ではありませんが、KeySet を反復処理し、各反復ポイントでマップを取得してから、値 (この場合はリスト) に対して contains を実行できます。

Integer bar = whatever you are evaluating
Map<String, List<Integer>> fooMap = new HashMap<String, List<Integer>>();
... build your map ...
for(String key:fooMap.keySet()){
    if(fooMap.get(key).contains(bar)){
        ...logic when found...  
    }
}
于 2013-04-29T13:32:50.750 に答える