一道阿里面试题,求高手解答

atbkw 2018-12-28 08:49:12
一个List放入多个person对象,每个person对象有name,age两个属性,现要求不准遍历List,取出name=张三,age=23的person对象
...全文
1968 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
110成成 2019-01-10
  • 打赏
  • 举报
回复
数据结构基本算法,递归。
nayi_224 2019-01-10
  • 打赏
  • 举报
回复
感觉这是个脑筋急转弯,虽然说了用List,但是List只是个接口,我完全可以写一个基于hash的list。至于碰撞问题,增大列表长度可以减少碰撞。如果内存无限,hash算法合适,并且数组长度无限,理论上可以解决碰撞。
package test.gt60;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test69 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Person69 p1 = new Person69("p1", 11);
		Person69 p2 = new Person69("p1", 11);
		Person69 p3 = new Person69("p2", 11);
		Person69 p4 = new Person69("p3", 11);
		Person69 p5 = new Person69("p4", 11);
		
		TestHashMapList list = new TestHashMapList();
		
		list.add(p1.toString(), p1);
		list.add(p2.toString(), p2);
		list.add(p3.toString(), p3);
		list.add(p4.toString(), p4);
		list.add(p5.toString(), p5);
		
		System.out.println(list.hashGet("p1_11"));
		
	}

}

class Person69{
	private String name;
	private int age;
	
	public Person69(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.name + "_" + this.age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

class TestHashMapList {

	private Map<String, List<Person69>> mapValue = new HashMap(2147483);
	
	public void add(String str, Person69 person){
		if(this.mapValue.containsKey(str)){
			this.mapValue.get(str).add(person);
		}else {
			List temp = new ArrayList(16);
			temp.add(person);
			this.mapValue.put(str, temp);
		}
	}
	
	public List<Person69> hashGet(String str){
		return this.mapValue.get(str);
	}
}
stacksoverflow 2019-01-10
  • 打赏
  • 举报
回复
这道题出得有问题,前提条件模糊并且看不出来要考察什么。
nayi_224 2019-01-10
  • 打赏
  • 举报
回复
indexOf还是算了吧
    /**
     * Returns the index of the first occurrence of the specified element
     * in this list, or -1 if this list does not contain the element.
     * More formally, returns the lowest index <tt>i</tt> such that
     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
     * or -1 if there is no such index.
     */
    public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
weixin_43191973 2019-01-10
  • 打赏
  • 举报
回复
stream get到了
rickylin86 2019-01-08
  • 打赏
  • 举报
回复

import java.util.ArrayList;


class Person{
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}


	@Override
	public String toString(){
		return String.format("Person[%s,%d]",name,age);
	}

	@Override
	public boolean equals(Object obj){
		if(!(obj instanceof Person)){
			return false;
		}
		Person person = (Person)obj;
		return person.name.equals(name) && person.age == age;
	}

	private String name;
	private int age;
}

public class Test{
	public static void main(String[] args){
		Person p1 = new Person("李四",28);
		Person p2 = new Person("张三",25);
		Person p3 = new Person("张三",23);
		Person p4 = new Person("王五",24);
		ArrayList<Person> people = new ArrayList<>();
		people.add(p1);
		people.add(p2);
		people.add(p3);
		people.add(p4);

		Person find = new Person("张三",23);

		int index = people.indexOf(find);
		if(index > -1){
			System.out.println("找到:" + people.get(index) + " 对应位置:" + index);
		}else{
			System.out.println("未找到:" + find);
		}
	}
}
ChivenZhang 2019-01-02
  • 打赏
  • 举报
回复
先序列化,再用objectinputstream获取目标
生财 2019-01-02
  • 打赏
  • 举报
回复
list[0]= new person(){ name ='张三' , age=5};
list[1]= new person(){ name ='XX' , age=5};
list[...]= new person(){ name ='XX' , age=5};



list[0] //
weixin_44353855 2019-01-01
  • 打赏
  • 举报
回复
看的不不懂的催化剂
rumlee 2019-01-01
  • 打赏
  • 举报
回复
List中如果是乱序的,而且不允许排序的话,除了遍历没有任何办法。或许有人说可以有***办法等等,但是在这种情况下,任何办法的本质都是遍历。 当然如果说允许排序或者允许增加一些额外的内存开销的话,那就另当别论了。
鲁JAVA8 2018-12-30
  • 打赏
  • 举报
回复
用递归和二分法。二分法比较高大上一些。
Tony-老师 2018-12-30
  • 打赏
  • 举报
回复
list又不是散列表,不要用hash算法了,而且还存在碰撞问题。先List按Person的age排序,排序后再二分查找,找到age等于23的数,如果找到,再比较name
十八道胡同 2018-12-29
  • 打赏
  • 举报
回复
引用 5 楼 执笔记忆的空白 的回复:
stream不也是遍历了么

我有个比较笨的方法,我写一个方法,用递归不就行了。


参考


这个方法 思路很巧妙
qq_39936465 2018-12-29
  • 打赏
  • 举报
回复
list设置hashcode ,用hashcode直接取数据
  • 打赏
  • 举报
回复
stream不也是遍历了么 我有个比较笨的方法,我写一个方法,用递归不就行了。 参考
maradona1984 2018-12-29
  • 打赏
  • 举报
回复
stream遍历就不是遍历了?
我翻了下源码,IteratorSpliterator这个里面就是集合类的迭代器

阿里会问这么low的问题?
OldBibi 2018-12-29
  • 打赏
  • 举报
回复
本质不都得遍历么,要么对数据做预处理
大苟姬 2018-12-29
  • 打赏
  • 举报
回复
我觉得问题不太对劲,
朽一 2018-12-29
  • 打赏
  • 举报
回复
写入文件,再打开找到
丶清浅旧时光 2018-12-29
  • 打赏
  • 举报
回复
我觉得楼主在问的时候,要标明两点1. 问题是什么 2.用什么语言
加载更多回复(11)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧