首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >像Python Tuples一样对Java进行排序和合并

像Python Tuples一样对Java进行排序和合并
EN

Stack Overflow用户
提问于 2013-04-29 21:02:53
回答 3查看 1.1K关注 0票数 0

我来自Python背景,目前正在将我的Python程序移植到Java。我需要关于解决问题的最佳方法的建议。

最初,我用Python创建了一个元组列表:

代码语言:javascript
复制
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)]

(其中,印度,accepts是关键字,数字是从数据库中获取的id。)现在,应用:

代码语言:javascript
复制
di = {}
for x,y in ll:
     di.setdefault(x,[]).append(y)
newdi = {}

我的列表变成了一本字典:

代码语言:javascript
复制
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部分:

代码语言:javascript
复制
    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中关键字的匹配项。像'manmohan‘可以在id 1,2,3,4中找到,等等。

代码语言:javascript
复制
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中找到类似的项目,并像上面那样对它们进行排序。或者我需要一个完全不同的东西?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-04-29 21:31:15

您需要创建一个具有列表或设置值的Map。根据您的需要,您可以保留Tuples类,也可以单独使用String和Integer。

下面是一个示例:

代码语言:javascript
复制
// construct a map with string key (tag) and list of integers (ids) as the value
Map<String, List<Integer>> keywords = new HashMap<String, List<Integer>>();

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);

        // construct the List for this keyword
        if (!keywords.containsKey(tag)) {
            keywords.put(tag, new ArrayList<Integer>());
        } 
        keywords.get(tag).add(id);
    }
}

keywords将是一个与您的Python实现中的数据结构类似的数据结构:

代码语言:javascript
复制
List<Integer> manmohanList = keywords.get("manmohan"); // will get you a list containing the numbers 1,2,3,4
for (Integer id: manmohanList) {
    System.out.println(id); // prints 1,2,3,4
}
票数 1
EN

Stack Overflow用户

发布于 2013-04-29 21:32:51

看一下java.lang.Map接口。您实际上是在构建一个

代码语言:javascript
复制
Map<String,List<Integer>> 

使用纯集合类,您可以使用contains和Collections.sort等方法(关注性能,如果需要,您可以考虑自己的排序算法)

对于新的Java开发人员来说,迭代Map并非易事,但是您可以迭代KeySet,在每个迭代点对map执行get,然后对值执行contains,在本例中是一个列表。

代码语言:javascript
复制
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...  
    }
}
票数 2
EN

Stack Overflow用户

发布于 2013-04-29 21:49:35

与其为元组创建一个类,不如声明一个HashMap来存储字典、关键字和位置。比如

代码语言:javascript
复制
Map<String, ArrayList<Integer>> dictionary = new HashMap<String, ArrayList<Integer>>();

//Now before adding any new keyword to the map just check if it contains it or not.
while (rs1.next()){
   //Your
   //Old
   //Code
   if(dictionary.contains(tag)){
       id_list = dictionary.get(tag);
       id_list.add(id);
       dictionary.put(tag, id_list);
   }else{
        dictionary.put(tag, id);
   }
}

还没有测试它的打字错误。但我想你应该有个想法。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16279342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档