【问题标题】:Converting custom ArrayList to LinkedHashSet将自定义 ArrayList 转换为 LinkedHashSet
【发布时间】:2013-02-03 15:48:54
【问题描述】:

我有一个自定义的ArrayAdapter<Summary>,其中包含一个事件列表。 List<Summary> 中有重复值,所以我尝试将 List<Summary> 的值放入 LinkedHashSet<Summary> 但这会显示一个空白页。

如何将自定义 ArrayList 转换为 LinkedHashSet 以获取唯一数据?

Main.java:

LinkedHashSet<Summary> listToSet = new LinkedHashSet<Summary>();
final List<Summary> summaries = new ArrayList<Summary>();

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.events_summary, container, false);

    .......

    setListView(month, year, date_value);
    summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, listToSet);

    calendarSummary = (ListView) v.findViewById(R.id.calendarSummary);
    calendarSummary.setAdapter(summaryAdapter);

    return v;
}

public void setListView(int month, int year, int dv) {

        events = new HolidayEvents();
        _calendar = Calendar.getInstance(Locale.getDefault());
        int totalDays = _calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        for(int i = 1; i <= totalDays; i++){
            if(isHoliday(i, month, year, dv))
            {
                date = i + " " + getMonthForInt(month-1) + " " + year;

                for (Event event : events.eventDetails(this, month, i)) 
                {
                       summaries.add(new Summary(date, event.eventdetails));
                       listToSet.addAll(summaries);

                } 
            }
        }
}

ArrayAdapter.java:

public class SummaryAdapter extends ArrayAdapter<Summary>{

    Context context; 
    int layoutResourceId;
    LayoutInflater mInflater;
    LinkedHashSet<Summary> list = null;
    List<Summary> data = null;


    public SummaryAdapter(Context context, int layoutResourceId, LinkedHashSet<Summary> summaries) {
        super(context, layoutResourceId);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.list = summaries;
        data = new ArrayList<Summary>(list); //converting LinkedHashSet to List
        mInflater = LayoutInflater.from(context);
    }

 ....rest of the code retrieving data by using data.get(position) ...

【问题讨论】:

  • 1.您的代码中有语法错误(在 foreach 中)。 2. 为什么要在ArrayList 中添加Summary,然后将whole 列表添加到LinkedHashSet
  • @nkr,1. 我更正了错误。它只在代码中。 2、Summary要怎么加到LinkedHashSet上?
  • Summary summary = new Summary(...); summaries.add(summary); listToSet.add(summary);
  • listToSet.add(summary); 给出add 不适用于Summary的错误
  • @输入。你的问题的解决方案是什么?问题出在哪里?我的回答有帮助吗?

标签: java android arraylist linkedhashset


【解决方案1】:

您需要确保放入 Set 的类正确覆盖 Equals 和 hashCode 函数。

让我们看看 hashCode 没有被覆盖的情况:

import java.util.*;

public class Example {

    public static class Abc {
        protected String s;
        public Abc(String s) {
            this.s = s;
        }

        @Override
        public boolean equals(Object other) {
            if (other instanceof Abc) {
                return ((Abc) other).s.equals(this.s);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return (int) (Math.random() * Integer.MAX_VALUE);
        }

        public String toString() {
            return "Abc: " + this.s; 
        }
    }

    public static void main(String[] args) {

        ArrayList<Abc> ar = new ArrayList<>();

        ar.add(new Abc("a"));
        ar.add(new Abc("a"));
        ar.add(new Abc("a"));

        LinkedHashSet<Abc> lhs = new LinkedHashSet<>(ar);

        System.out.println("ar: " + ar);
        System.out.println("LinkedHashSet: " + lhs);

    }
}

这将产生:

ar: [Abc: a, Abc: a, Abc: a] LinkedHashSet:[Abc:a,Abc:a,Abc:a]

即使 equals 已正确实现。 我相信您可能需要仔细检查 HashCodes 和 Equals 的正确实现。

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2014-01-17
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 2019-02-26
    • 2013-09-22
    相关资源
    最近更新 更多