使用 HashSet 清除重复值。列出 returns 个空值或 returns 个重复值

Use HashSet to weed out duplicate values. List returns either empty or returns duplicate values

我正在尝试使用 HashSetArrayList 中删除重复值,这样即使城市名称相同,也不会多次返回......列表返回为空或仍然显示重复值。希望有人能告诉我我的代码中的错误在哪里,这样就不会返回重复的值...

我正在使用这个作为参考:

Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set); ,但无法弄清楚如何让它在我的代码中工作。知道这可能是一个简单的修复,但我一直在尝试它,但仍然没有恢复正常...

谁能告诉我哪里错了?到目前为止,根据每个人的说法,代码看起来应该可以工作......虽然它不是......

SearchCityFragment

public class SearchCityFragment extends Fragment {

    private List<Post> mPostList;
    private Set<Post> mPostSet;
    private RecyclerView mRecyclerView;
    private CityAdapter mCityAdapter;

    private EditText mSearchBar;
    private RelativeLayout mRelativeLayout;
    private Activity mActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_city, container, false);

        mRelativeLayout = view.findViewById(R.id.relative_layout_11);
        mRelativeLayout.setVisibility(View.VISIBLE);

        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);

        mPostList = new ArrayList<>();
        mCityAdapter = new CityAdapter(getContext(), mPostList);
        mRecyclerView.setAdapter(mCityAdapter);

        mSearchBar = mActivity.findViewById(R.id.search_bar);
        mSearchBar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                searchCity(s.toString().toLowerCase());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        return view;
    }

    private void searchCity(String s) {
        Query query = FirebaseDatabase.getInstance().getReference("Posts").orderByChild("city").startAt(s).endAt(s + "\uf8ff");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mPostList.clear();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Post post = snapshot.getValue(Post.class);
                    if (s.length() == 0) {
                        mPostList.clear();
                        mRelativeLayout.setVisibility(View.VISIBLE);
                    } else {
                        mRelativeLayout.setVisibility(View.GONE);
                        mPostList.add(post);
                        mPostSet = new HashSet<>(mPostList);
                        mPostList.clear();
                        mPostList.addAll(mPostSet);
                        readCity();
                    }
                }

                mCityAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    private void readCity() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (mSearchBar.getText().toString().equals("")) {
                    mPostList.clear();
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Post post = snapshot.getValue(Post.class);
                        mPostList.add(post);
                        mPostSet = new HashSet<>(mPostList);
                        mPostList.clear();
                        mPostList.addAll(mPostSet);
                    }

                    mCityAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof Activity) {
            mActivity = (Activity) context;
        }
    }
}

如果您只想显示 post 和搜索到的城市,并且只想显示一个这样的 post,您可以在数据库中查询包含该城市的 post,您不需要创建任何 arraylist 或 hashmap。

                          private void searchCity(String s) {
                               FirebaseDatabase.getInstance()
                                     .getReference("Posts")
                                     .orderByChild("city")
                                     .startAt(s)
                                     .limitToFirst(1).
                                     .addValueEventListener(new ValueEvnetListener){
                                     @Override
                                     public void onDataChange(DataSnapshot 
                                     dataSnapshot) {
                                     // do your rhing
                                      }
                                    @Override
                               public void onCancelled(DatabaseError databaseError) {}
                                    } ;
}